PortGate

Blueprints · Laravel & PHP

Production 40 min · Ubuntu 24.04 / Nginx / PHP 8.4

Laravel 12 / 13 bare-metal production stack

Nginx, PHP 8.4-FPM, Redis on localhost, a queue worker, the scheduler cron, and a deploy script that switches the current symlink after migrations.

Live config

Baked-in protections

Step 1 · bash

Packages and deploy user

/root/portgate-blueprints/laravel-packages.sh

Uses the distro PHP 8.4 package when it exists, otherwise the Ondřej PPA. The default FPM pool is disabled.

#!/bin/sh
set -eu
export DEBIAN_FRONTEND=noninteractive
apt-get update
apt-get install -y nginx redis-server supervisor curl ca-certificates
if ! apt-cache show php8.4-fpm >/dev/null 2>&1; then
  apt-get install -y software-properties-common
  add-apt-repository -y ppa:ondrej/php
  apt-get update
fi
apt-get install -y php8.4-fpm php8.4-cli php8.4-mbstring php8.4-xml php8.4-curl php8.4-mysql php8.4-zip php8.4-bcmath php8.4-redis
id "deploy" >/dev/null 2>&1 || useradd --create-home --shell /bin/bash "deploy"
install -d -m 755 -o "deploy" -g "deploy" "/var/www/app/releases" "/var/www/app/shared"
if [ -f /etc/php/8.4/fpm/pool.d/www.conf ]; then
  mv /etc/php/8.4/fpm/pool.d/www.conf /etc/php/8.4/fpm/pool.d/www.conf.disabled
fi

Step 2 · nginx

Nginx site

/etc/nginx/sites-available/app.example.com.conf

Blocks dotfiles, .env, .git, and the storage directory. PHP is passed to the local pool.

server {
    listen 80;
    server_name app.example.com;
    root /var/www/app/current/public;
    index index.php;
    client_max_body_size 20m;
    server_tokens off;

    location ~ /\.(?!well-known).* { deny all; }
    location = /.env { deny all; }
    location ^~ /.git { deny all; }
    location ^~ /storage { deny all; }

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_pass 127.0.0.1:9084;
    }
}

Step 3 · ini

PHP-FPM pool and scheduler

/etc/php/8.4/fpm/pool.d/app.conf

pm.max_children is 8 for a small VPS. Raise it when you have measured the worker RSS.

[app]
user = deploy
group = deploy
listen = 127.0.0.1:9084
pm = dynamic
pm.max_children = 8
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
php_admin_flag[expose_php] = off
php_admin_value[display_errors] = off

Step 4 · bash

Worker, cron, and deploy script

/root/portgate-blueprints/laravel-enable.sh

The deploy script is written and not executed. Database {{DB_NAME}} is created empty so the first migrate has a target.

#!/bin/sh
set -eu
cat > /etc/supervisor/conf.d/app-queue.conf <<'EOF'
[program:app-queue]
command=php /var/www/app/current/artisan queue:work --sleep=1 --tries=3 --max-time=3600
user=deploy
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/var/www/app/shared/queue.log
EOF
echo '* * * * * deploy php /var/www/app/current/artisan schedule:run >> /var/www/app/shared/scheduler.log 2>&1' > /etc/cron.d/app-schedule
chmod 644 /etc/cron.d/app-schedule
ln -sfn "/etc/nginx/sites-available/app.example.com.conf" /etc/nginx/sites-enabled/app.example.com.conf
rm -f /etc/nginx/sites-enabled/default
nginx -t
systemctl reload nginx
systemctl enable --now php8.4-fpm
systemctl reload php8.4-fpm
systemctl enable --now supervisor
supervisorctl reread || true
supervisorctl update || true
install -m 755 /dev/null /root/portgate-blueprints/laravel-deploy.sh
cat > /root/portgate-blueprints/laravel-deploy.sh <<'EOF'
#!/bin/sh
set -eu
RELEASE="$(date +%Y%m%d%H%M%S)"
BASE="/var/www/app"
git clone --depth 1 "https://github.com/example/app.git" "$BASE/releases/$RELEASE"
ln -sfn "$BASE/shared/.env" "$BASE/releases/$RELEASE/.env"
composer install --no-dev --optimize-autoloader --no-interaction --working-dir "$BASE/releases/$RELEASE"
php "$BASE/releases/$RELEASE/artisan" migrate --force
ln -sfn "$BASE/releases/$RELEASE" "$BASE/current"
php "$BASE/current/artisan" config:cache
php "$BASE/current/artisan" queue:restart
systemctl reload php8.4-fpm
EOF
chmod 755 /root/portgate-blueprints/laravel-deploy.sh
echo "Deploy script written. Run it after shared/.env exists. Database name: app"