Apps

Deploy Laravel to Ubuntu with NGINX, PHP-FPM, a queue worker and the scheduler

In this article10 sections

A Laravel application that works on your laptop is not yet a production deployment. Production means the right web server configuration, files owned by the right user, secrets kept out of the web root, caches built, a queue worker that restarts when it dies, a scheduler that runs every minute, and a way to release a new version without breaking the old one halfway.

This guide builds that on Ubuntu 24.04 with NGINX and PHP-FPM, step by step. It assumes you already have a server with SSH key access, a firewall that allows ports 80 and 443, and a domain pointing at it. The examples use app.example.com and a user called deploy; change them to suit.

1. Install the stack

Ubuntu 24.04 ships PHP 8.3. Install it with the extensions Laravel needs, plus NGINX, Composer and a database:

sudo apt update
sudo apt install -y nginx php8.3-fpm php8.3-cli php8.3-mysql php8.3-mbstring php8.3-xml php8.3-curl php8.3-zip php8.3-bcmath php8.3-intl php8.3-gd unzip git mariadb-server composer
php -v
composer --version

Check what your Laravel version requires in composer.json (the php constraint). If it needs a newer PHP than the distribution provides, use a maintained PHP package repository rather than an old version, and plan the upgrade.

Create the database and a user with a strong password, limited to that database:

sudo mysql -e "CREATE DATABASE app CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
sudo mysql -e "CREATE USER 'app'@'localhost' IDENTIFIED BY 'use-a-long-random-password';"
sudo mysql -e "GRANT ALL PRIVILEGES ON app.* TO 'app'@'localhost';"

2. A release-folder layout

Do not deploy by copying files over the live site. Use one folder per release and a symlink that points at the current one:

/var/www/app/
    releases/
        2026-10-29-1015/     <- one folder per deploy
    shared/
        .env                 <- secrets, never in git
        storage/             <- logs, cache, uploads: survive deploys
    current -> releases/2026-10-29-1015

Create the structure:

sudo mkdir -p /var/www/app/releases /var/www/app/shared/storage
sudo chown -R deploy:www-data /var/www/app

The shared folder holds anything that must outlive a release. Copy your storage skeleton there once (storage/app, storage/framework/{cache,sessions,views}, storage/logs) and create the .env file in shared/.

3. The .env file and permissions

Set the production values:

APP_ENV=production
APP_DEBUG=false
APP_URL=https://app.example.com

APP_DEBUG=false matters: with debug on, an error page can print environment values, including passwords. Generate the application key once with php artisan key:generate --show and paste it into .env.

Lock the file down:

sudo chown deploy:www-data /var/www/app/shared/.env
sudo chmod 640 /var/www/app/shared/.env

Only deploy and the web group can read it, and the world cannot. PHP-FPM must be able to write to storage and bootstrap/cache, and nothing else:

sudo chmod -R ug+rwX /var/www/app/shared/storage
sudo chgrp -R www-data /var/www/app/shared/storage
sudo find /var/www/app/shared/storage -type d -exec chmod g+s {} +

The g+s makes new files inherit the group. Avoid chmod 777: it is never the answer, and it is the first thing a security review flags.

4. The NGINX server block

The web root must be the public folder, never the project root. Otherwise .env, vendor and the source are one URL away from the internet.

sudo nano /etc/nginx/sites-available/app.example.com
server {
    listen 80;
    server_name app.example.com;
    root /var/www/app/current/public;
    index index.php;

    add_header X-Content-Type-Options "nosniff" always;
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header Referrer-Policy "strict-origin-when-cross-origin" always;

    client_max_body_size 16m;

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

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.3-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
    }

    location ~ /\.(?!well-known) {
        deny all;
    }

    location ~* \.(css|js|png|jpg|jpeg|gif|svg|ico|woff2)$ {
        expires 30d;
        access_log off;
        add_header Cache-Control "public";
    }
}

Two details deserve attention. $realpath_root resolves the current symlink, so PHP’s opcode cache sees the real release folder. Without it, a deploy can leave PHP serving stale files from the old release. And location ~ /\.(?!well-known) { deny all; } blocks every dotfile (.env, .git) while still allowing the .well-known folder that certificate issuing needs.

Enable it, test, reload, and get a certificate:

sudo ln -s /etc/nginx/sites-available/app.example.com /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx
sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d app.example.com

5. The deploy script

A deploy must be repeatable, so write it down once. Save this as /var/www/app/deploy.sh and make it executable:

#!/usr/bin/env bash
set -euo pipefail
umask 002

APP=/var/www/app
REL="$APP/releases/$(date +%Y-%m-%d-%H%M)"
[email protected]:your-org/your-app.git

git clone --depth 1 --branch main "$REPO" "$REL"
cd "$REL"

composer install --no-dev --optimize-autoloader --no-interaction

ln -sfn "$APP/shared/.env" "$REL/.env"
rm -rf "$REL/storage"
ln -sfn "$APP/shared/storage" "$REL/storage"

php artisan migrate --force
php artisan config:cache
php artisan route:cache
php artisan view:cache

ln -sfn "$REL" "$APP/current"

sudo systemctl reload php8.3-fpm
php artisan queue:restart

# keep the five newest releases
ls -1dt "$APP"/releases/* | tail -n +6 | xargs -r rm -rf
echo "Deployed $REL"

Two notes. umask 002 makes files created by the deploy user group-writable, so PHP-FPM (running as www-data) can still write to anything it shares with the deploy. And the script calls sudo systemctl reload php8.3-fpm, so give the deploy user exactly that one right, and nothing broader: create /etc/sudoers.d/deploy with deploy ALL=(root) NOPASSWD: /usr/bin/systemctl reload php8.3-fpm, edited with sudo visudo -f /etc/sudoers.d/deploy.

What the order protects you from: dependencies are installed and caches are built before the symlink moves, so visitors never see a half-built release. php artisan migrate --force runs non-interactively in production. If the build fails, the script stops (set -e) and current still points at the working release. To roll back, point the symlink at the previous folder and reload PHP-FPM:

ln -sfn /var/www/app/releases/<previous> /var/www/app/current && sudo systemctl reload php8.3-fpm

A caution on migrations: a migration that drops or renames a column can break the old release the moment it runs. For anything destructive, split it over two deploys (add the new, switch the code, remove the old later).

6. A queue worker that stays up

If your application uses queued jobs (mail, notifications, imports), something must run php artisan queue:work all the time and restart it when it stops. systemd does this cleanly:

sudo nano /etc/systemd/system/app-queue.service
[Unit]
Description=Laravel queue worker
After=network.target mariadb.service

[Service]
User=www-data
Group=www-data
WorkingDirectory=/var/www/app/current
ExecStart=/usr/bin/php artisan queue:work --sleep=3 --tries=3 --max-time=3600
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable --now app-queue
sudo systemctl status app-queue --no-pager

--max-time=3600 makes the worker exit cleanly every hour, and Restart=always brings it back, which protects against slow memory growth. php artisan queue:restart in the deploy script tells running workers to finish their current job and exit, so the new code is picked up. Follow the worker’s log with journalctl -u app-queue -f.

7. The scheduler

Laravel’s task scheduler needs exactly one cron entry that runs every minute. Run it as the same user as the web application and the queue worker (www-data here), so that log and cache files are never owned by someone who cannot update them:

sudo crontab -u www-data -e
* * * * * cd /var/www/app/current && php artisan schedule:run >> /dev/null 2>&1

Check that it is running with php artisan schedule:list, and make sure the scheduler’s own jobs write somewhere you will look. A silent scheduler is one of the most common reasons a “working” application quietly stops sending reminders.

8. After the first deploy: verify

curl -sI https://app.example.com | head -5
curl -s -o /dev/null -w "%{http_code}\n" https://app.example.com/.env
tail -n 30 /var/www/app/shared/storage/logs/laravel.log
sudo systemctl is-active app-queue php8.3-fpm nginx

You want 200 for the site, 404 or 403 for /.env, no fresh errors in the log, and three services reporting active. Also check that the application sends mail and processes a test job end to end, because “the page loads” proves very little.

9. Backups and logs

  • Database: a nightly mysqldump --single-transaction to a location off the server, with a restore test.
  • Files: shared/storage/app (uploads) and shared/.env. The code is in git; the secrets and the uploads are not.
  • Logs: set the log channel to daily in config/logging.php so a single file does not grow without limit.

When it is worth getting help

A first Laravel deployment is a good learning project. A deployment that a business depends on (payments, customer data, scheduled jobs that matter) benefits from someone who has done it many times and will also set up backups, monitoring and a rollback you have actually tried.

I set up and look after Laravel and WordPress applications on Ubuntu and NGINX. You can see my profile on Upwork and describe your application, or read about WordPress and Laravel care. If you need a server to run it on, SiteHarbour offers VPS hosting.