Log inSign up

Run a Laravel application behind Nginx and PHP-FPM

To deploy Laravel with Nginx, install PHP-FPM with the extensions Laravel needs, put the code under /var/www/app.example.net, point the Nginx root at its public folder and send PHP requests to the PHP-FPM socket. Give the web user write access only to storage and bootstrap/cache, fill in .env with APP_ENV=production and APP_DEBUG=false, then run queue workers under systemd and the scheduler from cron.

Topic: PHP and web stacks · Tested on: Ubuntu 24.04 and 22.04, Debian 12 and 13 · Updated 2026-12-03

What does the server need for Laravel?

Laravel needs a recent PHP with a handful of extensions, Composer, a database and a web server. Start from a working LEMP stack or install the pieces now.

sudo apt update
sudo apt install -y nginx mariadb-server php-fpm php-cli php-mysql \
  php-mbstring php-xml php-curl php-zip php-bcmath php-intl unzip git
php -v
ls /run/php/

Ubuntu 24.04 ships PHP 8.3, Debian 12 ships 8.2 and Debian 13 ships 8.4. Current Laravel releases require PHP 8.2 or newer, so Ubuntu 22.04 (PHP 8.1) needs a newer PHP first; the PHP upgrade guide covers that. The ls /run/php/ output gives the socket name you will use later, for example php8.3-fpm.sock. Install Composer with the checked installer as well.

How do you prepare the database and the code?

Create one database and one user that can only touch it, then fetch the application as the deploy user.

sudo mariadb -e "CREATE DATABASE laravel CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
sudo mariadb -e "CREATE USER 'laravel'@'localhost' IDENTIFIED BY 'change-this-long-secret';"
sudo mariadb -e "GRANT ALL ON laravel.* TO 'laravel'@'localhost';"

sudo mkdir -p /var/www/app.example.net
sudo chown deploy:www-data /var/www/app.example.net
cd /var/www/app.example.net
git clone https://git.example.net/team/app.git .
composer install --no-dev --optimize-autoloader --no-interaction

For a brand new project, replace the clone with composer create-project laravel/laravel .; Composer picks the newest Laravel release that matches the PHP on the server. Keep the repository private and use a deploy key if it is hosted elsewhere.

Which permissions does Laravel need?

The code should belong to deploy and be read-only for the web server, while storage and bootstrap/cache must be writable by PHP-FPM, which runs as www-data on Ubuntu and Debian.

cd /var/www/app.example.net
sudo chown -R deploy:www-data .
sudo find . -type f -exec chmod 640 {} \;
sudo find . -type d -exec chmod 750 {} \;
sudo chmod -R ug+rwX storage bootstrap/cache
sudo find storage bootstrap/cache -type d -exec chmod g+s {} \;

The setgid bit keeps new files in the www-data group, so log files written by a worker stay readable for Artisan commands you run as deploy. Never use chmod 777. More detail on modes and groups is in the file permissions guide.

What goes into the production .env file?

The .env file holds secrets and environment settings; it must exist on the server only and never in Git.

cp .env.example .env
php artisan key:generate
chmod 640 .env
nano .env
APP_NAME=App
APP_ENV=production
APP_DEBUG=false
APP_URL=https://app.example.net
LOG_CHANNEL=daily
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=laravel
DB_PASSWORD=change-this-long-secret
QUEUE_CONNECTION=database
CACHE_STORE=database
SESSION_DRIVER=database

Recent skeletons default to SQLite, so set DB_CONNECTION explicitly. With debug off, errors go to storage/logs instead of the browser. Then build the schema and the caches:

php artisan migrate --force
php artisan storage:link
php artisan optimize

optimize caches config, routes, views and events. After any later change to .env, run php artisan optimize again, because cached config ignores the file.

How should the Nginx server block look?

Point root at public, route every unknown path to index.php, and allow PHP to run only for that front controller.

server {
    listen 80;
    listen [::]:80;
    server_name app.example.net;
    root /var/www/app.example.net/public;
    index index.php;
    charset utf-8;
    client_max_body_size 20m;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }
    error_page 404 /index.php;

    location ~ ^/index\.php(/|$) {
        fastcgi_pass unix:/run/php/php8.3-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_hide_header X-Powered-By;
    }
    location ~ /\.(?!well-known).* { deny all; }
}

Save it as /etc/nginx/sites-available/app.example.net, link it and test:

sudo ln -s /etc/nginx/sites-available/app.example.net /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx
sudo ufw allow 'Nginx Full'
sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d app.example.net

Adjust the socket name to the PHP version you saw earlier. The DNS record must already point at the server for the certificate step; see DNS records and Certbot.

How do you run queue workers with systemd?

A systemd unit keeps queue:work running, restarts it after a crash and starts it at boot. Create /etc/systemd/system/laravel-queue.service:

[Unit]
Description=Laravel queue worker for app.example.net
After=network.target mariadb.service

[Service]
User=www-data
Group=www-data
WorkingDirectory=/var/www/app.example.net
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 laravel-queue
systemctl status laravel-queue
journalctl -u laravel-queue -f

Workers keep the app in memory, so they do not see new code until they restart. Add php artisan queue:restart to every deploy; the worker finishes its current job and systemd brings up a fresh one. For several parallel workers, turn the unit into a template (laravel-queue@.service) and enable laravel-queue@1, laravel-queue@2 and so on. The systemd guide explains unit options further.

How do you run the scheduler and set the timezone?

Laravel's scheduler needs one cron entry that calls schedule:run every minute; the app decides which tasks are due.

sudo crontab -u www-data -e
# add this line
* * * * * cd /var/www/app.example.net && php artisan schedule:run >> /dev/null 2>&1

php artisan schedule:list

Running it as www-data keeps file ownership consistent with the web requests. The application timezone lives in config/app.php under 'timezone'. The safest choice is to keep it at UTC, store all timestamps in UTC and convert to a user's zone when displaying. If the line reads env('APP_TIMEZONE', 'UTC'), set APP_TIMEZONE in .env instead of editing the file. A single job can run on local time with ->timezone('Europe/Berlin') in the schedule definition. The server clock should be synced either way; see timezone and time sync. Clear and rebuild caches with php artisan optimize after changing it.

How do you deploy updates and fix common errors?

A repeatable deploy looks like this, run as deploy inside the project:

php artisan down
git pull --ff-only
composer install --no-dev --optimize-autoloader --no-interaction
php artisan migrate --force
php artisan optimize
php artisan queue:restart
php artisan up
  • 500 error with an empty page: read storage/logs/laravel-*.log; most often storage is not writable or APP_KEY is empty.
  • 502 Bad Gateway: the fastcgi_pass socket does not match the installed PHP-FPM version, or the service is stopped (systemctl status php8.3-fpm).
  • Every route except the home page returns 404: the try_files line is missing or root does not end in /public.
  • Old config values persist: run php artisan config:clear, then optimize.

Plans that fit this guide

Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.

All prices
PlanFits becauseSizeMonthlyOrder from
A1-2one small Laravel app with MariaDB1 vCPU, 2 GB RAM, 50 GB NVMe$7.20/mo$43.20 for 6 months
A2-4room for queue workers and caching2 vCPU, 4 GB RAM, 100 GB NVMe$14.40/mo$43.20 for 3 months
F2-4higher clock speed for busy request paths2 vCPU, 4 GB RAM, 128 GB NVMe$14.40/mo$43.20 for 3 months

Questions about Deploy Laravel with Nginx

Should the web root be the project folder or public?

Always public. Pointing Nginx at the project root would expose .env, logs and source code.

Can I use Redis for queues and cache?

Yes. Install Redis and the php-redis extension, then set QUEUE_CONNECTION=redis and CACHE_STORE=redis. See the Redis guide.

Do I need Supervisor for queue workers?

No. systemd is already on the server and handles restarts, logs and boot start, so one unit file replaces a Supervisor config.

How much memory does a small Laravel app need?

A low-traffic app with a database and one worker runs comfortably on 2 GB. Add RAM as worker count and PHP-FPM children grow.

Why does the scheduler not run my task?

Check that the cron line exists for www-data, that the path is right and that php artisan schedule:list shows the task with the expected next run time.

Is it safe to leave APP_DEBUG on for a while?

No. Debug pages reveal environment values and stack traces to anyone. Keep it false on public servers and read the log files instead.