Laravel #Laravel #Devops #Nginx #Postgresql

Building a production-ready Laravel application on a VPS

By CodeThere Super Admin

A step-by-step guide to deploying a Laravel application on a Linux VPS with nginx, PostgreSQL, queue workers, and a zero-downtime deployment pipeline.

Deploying a Laravel application to a VPS does not have to be complicated, but there are enough moving parts that it is easy to get wrong the first time. This guide covers the complete stack: nginx, PHP-FPM, PostgreSQL, queue workers, and a deployment workflow that minimises downtime.

Server prerequisites

Start with a fresh Ubuntu 22.04 or Debian 12 VPS. Create a non-root user, lock down SSH, and install the required packages:

apt update && apt install -y nginx php8.3-fpm php8.3-cli php8.3-pgsql php8.3-mbstring php8.3-xml php8.3-curl php8.3-zip php8.3-redis composer postgresql

Database setup

Create a dedicated PostgreSQL user and database for the application. Never use the postgres superuser in production:

sudo -u postgres createuser --pwprompt appuser
sudo -u postgres createdb --owner=appuser appdb

nginx configuration

Point the document root at public/ and send all requests through index.php. Set up TLS with Let's Encrypt via Certbot:

server {
    listen 443 ssl http2;
    server_name codethere.com;
    root /var/www/codethere/public;
    index index.php;

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

    location ~ \.php$ {
        fastcgi_pass unix:/run/php/php8.3-fpm.sock;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME realpath_rootfastcgi_script_name;
    }
}

Queue workers with systemd

Create a systemd service so the queue worker restarts automatically after a deploy:

[Unit]
Description=Laravel queue worker
After=network.target

[Service]
User=www-data
WorkingDirectory=/var/www/codethere
ExecStart=/usr/bin/php artisan queue:work --tries=3 --sleep=3
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target

Zero-downtime deploys

Use symlink-based deploys (Laravel Envoyer style) or a simple shell script that pulls the repo, runs migrations, clears caches, and reloads PHP-FPM without taking the site offline.

Related articles