All posts

Blog

How to Deploy a Laravel App on a VPS with Nginx (Production Guide)

Illustrated cover for the Developers guide "How to Deploy a Laravel App on a VPS with Nginx (Production Guide)"

Laravel makes building PHP applications pleasant, and php artisan serve makes running them locally effortless. Production is a different story. You need a real web server, the right file permissions, cached configuration, background queue workers, a scheduler and HTTPS, and a missing piece in any of those can cause problems that only appear after launch.

This guide shows you how to deploy a Laravel app on a VPS running Ubuntu with Nginx and PHP-FPM. Commands target a typical Ubuntu server; adjust the PHP version numbers to match what you install.

Before you begin

You'll need:

  • A VPS with a sudo user, SSH keys and a firewall. See first steps on a new Linux VPS.
  • A domain pointing to the server's IP with an A record.
  • Your Laravel app in a Git repository.

Check the server requirements in the official Laravel deployment documentation for your Laravel version. Recent releases require a modern PHP 8 version plus extensions such as mbstring, xml, curl, zip, bcmath and a database driver.

Step 1: Install Nginx, PHP and a database

If you haven't set up a stack yet, our LEMP stack on Ubuntu guide covers Nginx, MySQL and PHP-FPM in detail. In short:

sudo apt update
sudo apt install nginx mysql-server unzip git
sudo apt install php-fpm php-cli php-mysql php-mbstring php-xml php-curl php-zip php-bcmath php-intl

Ubuntu's default repositories ship one PHP version per release. If your Laravel version needs a newer PHP than your Ubuntu release provides, you'll need a newer Ubuntu or a trusted third-party PHP repository.

Install Composer by following the instructions on getcomposer.org, which include a checksum verification step. Copy the current commands from that page rather than from old tutorials, because the installer hash changes with each release.

Create a database and a dedicated user:

CREATE DATABASE laravel_app;
CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY 'a-strong-password';
GRANT ALL PRIVILEGES ON laravel_app.* TO 'laravel_user'@'localhost';
FLUSH PRIVILEGES;

Step 2: Get the code onto the server

Clone your repository into a directory such as /var/www/laravel-app:

sudo mkdir -p /var/www/laravel-app
sudo chown $USER:$USER /var/www/laravel-app
git clone git@github.com:you/your-app.git /var/www/laravel-app
cd /var/www/laravel-app

For a private repository, add a read-only deploy key to the repository rather than using your personal key. For automated releases later, see our guide on deploying a website with Git.

Install production dependencies:

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

If your front end uses Vite, build assets with npm ci && npm run build, either on the server or in CI before deploying.

Step 3: Configure the environment

cp .env.example .env
php artisan key:generate

Edit .env and set at least:

APP_NAME="Your App"
APP_ENV=production
APP_DEBUG=false
APP_URL=https://example.com

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_app
DB_USERNAME=laravel_user
DB_PASSWORD=a-strong-password

APP_DEBUG=false is critical. With debug on, error pages can expose environment details, including credentials, to anyone who triggers an error.

Only run key:generate once for a new install. Regenerating the key on an existing app invalidates sessions and makes previously encrypted data unreadable.

Step 4: Set file permissions

The web server needs to write to storage and bootstrap/cache, and nothing else:

sudo chown -R $USER:www-data /var/www/laravel-app
sudo find /var/www/laravel-app -type f -exec chmod 644 {} \;
sudo find /var/www/laravel-app -type d -exec chmod 755 {} \;
sudo chmod -R ug+rwx /var/www/laravel-app/storage /var/www/laravel-app/bootstrap/cache

Avoid chmod 777. It "fixes" permission errors by letting anyone on the server write to your files.

Step 5: Run migrations and link storage

php artisan migrate --force
php artisan storage:link

--force is required in production, because Laravel asks for confirmation before running migrations there. Always have a database backup before migrating a live app.

Step 6: Configure Nginx

The most important detail: Nginx's root must point to Laravel's public directory, never the project root. Otherwise your .env and source code could be downloadable.

Create /etc/nginx/sites-available/laravel-app, based on the example in Laravel's deployment docs:

server {
    listen 80;
    server_name example.com www.example.com;
    root /var/www/laravel-app/public;

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

    index index.php;
    charset utf-8;

    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;
    }
}

Change the PHP-FPM socket path to match your PHP version (ls /run/php/ shows it). Enable the site:

sudo ln -s /etc/nginx/sites-available/laravel-app /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx

Step 7: Add HTTPS

sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.com

Our Let's Encrypt with Certbot guide covers renewals and troubleshooting.

Step 8: Optimize for production

Laravel can cache configuration, routes, views and events. The single command is:

php artisan optimize

After you change .env or config files, run it again (or php artisan optimize:clear first), because cached config ignores later edits to .env. Also make sure PHP's OPcache is enabled, which it usually is by default with PHP-FPM.

Step 9: Run queue workers with Supervisor

If your app dispatches jobs (emails, notifications, imports), you need a long-running worker. Supervisor keeps it alive:

sudo apt install supervisor

Create /etc/supervisor/conf.d/laravel-worker.conf:

[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/laravel-app/artisan queue:work --sleep=3 --tries=3 --max-time=3600
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
user=www-data
numprocs=2
redirect_stderr=true
stdout_logfile=/var/www/laravel-app/storage/logs/worker.log
stopwaitsecs=3600

Load it:

sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start "laravel-worker:*"

Workers keep your code in memory, so restart them on every deploy with php artisan queue:restart.

Step 10: Enable the scheduler

Laravel's scheduler needs one cron entry that runs every minute. Edit the crontab for the user that owns the app (sudo crontab -u www-data -e) and add:

* * * * * cd /var/www/laravel-app && php artisan schedule:run >> /dev/null 2>&1

Our cron jobs guide explains the syntax if you need different timing for other tasks.

A repeatable deploy routine

Once the server is set up, each release looks like this:

cd /var/www/laravel-app
php artisan down
git pull origin main
composer install --no-dev --optimize-autoloader
php artisan migrate --force
php artisan optimize
php artisan queue:restart
php artisan up

Put it in a script so nobody skips a step. For zero-downtime releases, look at tools that deploy into separate release folders and switch a symlink.

Wrapping up

To deploy a Laravel app on a VPS reliably, point Nginx at public, keep APP_DEBUG off, give write access only to storage and bootstrap/cache, cache your config, run queue workers under Supervisor, add the scheduler cron and enable HTTPS. Script the release process, and every future deploy becomes a quick, predictable routine.