How to Deploy a Laravel App on a VPS (Nginx, PHP-FPM and Cron)

Deploy a Laravel app on an Ubuntu 24.04 VPS step by step: Nginx, PHP-FPM, MySQL, free SSL, cron, systemd queue workers and a one-command deploy script.

AD Admin PenduCoder 7 min read
How to Deploy a Laravel App on a VPS (Nginx, PHP-FPM and Cron)
On this page

Shared hosting is fine for a first project, but sooner or later a real Laravel app needs a VPS: queue workers, scheduled tasks, Redis, SSH access and predictable performance. This guide walks through a complete, production-ready setup on a fresh Ubuntu 24.04 server — Nginx, PHP-FPM, MySQL, Composer, SSL, queues and cron — and finishes with a simple script for deploying updates.

Every command is copy-paste ready. Replace example.com, deploy and myapp with your own domain, user and project name.

What you need

  • A VPS with at least 1 GB RAM (2 GB recommended) running Ubuntu 24.04 — from DigitalOcean, Hetzner, Vultr, Linode or similar.
  • A domain with an A record pointing to the server's IP address.
  • Your Laravel project in a Git repository (GitHub, GitLab or Bitbucket).

Step 1: Secure the server

Log in as root, create a normal user with sudo rights, and turn on the firewall.

ssh root@YOUR_SERVER_IP
adduser deploy
usermod -aG sudo deploy
rsync --archive --chown=deploy:deploy ~/.ssh /home/deploy   # copy your SSH key

ufw allow OpenSSH
ufw allow 'Nginx Full'   # run after installing Nginx in step 2
ufw enable

From now on, log in as deploy. Disabling password login in /etc/ssh/sshd_config (PasswordAuthentication no) blocks most brute-force attacks.

Step 2: Install Nginx, PHP and extensions

Ubuntu 24.04 ships with PHP 8.3, which current Laravel versions support.

sudo apt update && sudo apt upgrade -y
sudo apt install -y nginx git unzip curl \
  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 php8.3-redis

Install Composer globally:

curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
composer --version

Step 3: Create the database

sudo apt install -y mysql-server
sudo mysql_secure_installation
sudo mysql
CREATE DATABASE myapp CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'myapp'@'localhost' IDENTIFIED BY 'a-long-random-password';
GRANT ALL PRIVILEGES ON myapp.* TO 'myapp'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Never use the MySQL root user in your app. A dedicated user limited to one database contains the damage if credentials ever leak.

Step 4: Get the code onto the server

sudo mkdir -p /var/www/myapp && sudo chown deploy:www-data /var/www/myapp
cd /var/www/myapp
git clone git@github.com:you/myapp.git .
composer install --no-dev --optimize-autoloader
cp .env.example .env
php artisan key:generate

For a private repository, add a deploy key: run ssh-keygen -t ed25519 as the deploy user and add the public key to the repository's deploy keys (read-only).

Step 5: Configure the .env file

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

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_DATABASE=myapp
DB_USERNAME=myapp
DB_PASSWORD=a-long-random-password

QUEUE_CONNECTION=database
LOG_CHANNEL=daily

APP_DEBUG=false is not optional. With debug on, any error page can reveal file paths, queries and environment values to visitors — one of the most common misconfigurations in the OWASP Top 10.

Then run migrations, link storage and set permissions:

php artisan migrate --force
php artisan storage:link
sudo chown -R deploy:www-data storage bootstrap/cache
sudo chmod -R ug+rwx storage bootstrap/cache
php artisan optimize   # caches config, routes, views and events

Step 6: Configure Nginx

Create /etc/nginx/sites-available/myapp:

server {
    listen 80;
    server_name example.com www.example.com;
    root /var/www/myapp/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 ~ \.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;
    }
}
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
sudo rm /etc/nginx/sites-enabled/default
sudo nginx -t && sudo systemctl reload nginx

The root points to public/ — never the project folder — so .env, vendor/ and your source code can never be downloaded. The last location block denies hidden files such as .git.

Step 7: Free SSL with Let's Encrypt

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

Certbot edits your Nginx config to serve HTTPS, redirects HTTP to HTTPS and sets up automatic renewal. Test renewal with sudo certbot renew --dry-run.

Step 8: Run the scheduler with cron

Laravel's task scheduler needs one cron entry that runs every minute:

crontab -e
# add this line:
* * * * * cd /var/www/myapp && php artisan schedule:run >> /dev/null 2>&1

Step 9: Keep queue workers running with systemd

Queue workers must run permanently and restart if they crash. Create /etc/systemd/system/myapp-worker.service:

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

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

[Install]
WantedBy=multi-user.target
sudo systemctl daemon-reload
sudo systemctl enable --now myapp-worker
sudo systemctl status myapp-worker

--max-time=3600 restarts the worker every hour, which prevents slow memory leaks. Supervisor works just as well if you prefer it.

Step 10: Deploy updates with one script

Save this as deploy.sh in the project root and run bash deploy.sh whenever you push changes:

#!/usr/bin/env bash
set -e
cd /var/www/myapp

php artisan down --retry=15
git pull origin main
composer install --no-dev --optimize-autoloader
php artisan migrate --force
php artisan optimize
php artisan queue:restart
sudo systemctl reload php8.3-fpm
php artisan up

echo "Deployed $(git rev-parse --short HEAD)"

If you build front-end assets with Vite, add npm ci && npm run build before optimize (or build in CI and commit or upload the public/build folder). queue:restart makes workers load the new code after their current job.

Troubleshooting common errors

ProblemLikely cause and fix
500 error, blank pageCheck storage/logs/laravel-*.log. Usually permissions on storage or a missing APP_KEY.
502 Bad GatewayPHP-FPM is not running or the socket path is wrong: sudo systemctl status php8.3-fpm.
404 on every route except homeMissing try_files ... /index.php?$query_string in Nginx.
.env changes ignoredConfig is cached: run php artisan optimize:clear then php artisan optimize.
Jobs never runWorker not running, or QUEUE_CONNECTION=sync.
Uploaded images 404Run php artisan storage:link.

Production checklist

  • ☐ APP_ENV=production and APP_DEBUG=false
  • ☐ Nginx root points to public/; hidden files denied
  • ☐ HTTPS active and auto-renewing
  • ☐ Firewall enabled; SSH password login disabled
  • ☐ Cron for the scheduler and a systemd service for workers
  • ☐ Daily database backups stored off the server — and a test restore
  • ☐ Uptime monitoring and error tracking

FAQ

How much does it cost to host a Laravel app on a VPS?

A small app runs well on a VPS in the $5–12 per month range. Add managed backups or a separate database server as traffic grows.

Should I use a control panel or server manager?

Tools like Laravel Forge, Ploi or RunCloud automate exactly these steps for a monthly fee. Doing it manually once is the best way to understand what they do — and to debug them later.

Is Docker better?

Docker gives repeatable environments and is great for teams, but adds complexity. For a single app on a single server, the setup above is simpler to run and debug.

What next?

Your app is live — now make it fast. Log slow queries with our slow query snippet, add a dynamic sitemap for SEO, and read System Design Basics for what to do when traffic grows.

Found this useful? Share it

AD

Admin PenduCoder

Full-stack developer and educator. Writes about Laravel, Next.js, applied AI and the unglamorous parts of shipping software.

Get in touch →
Chat with us