How to Deploy Laravel on VPS with PHP-FPM and Nginx
Introduction: Why Deploy Laravel on a VPS
Laravel is the world's most popular PHP framework, featuring a clean MVC architecture, a powerful ORM, and a comprehensive ecosystem that lets developers build production-grade web applications rapidly.
While shared hosting can run Laravel, it frequently hits limitations around Composer memory limits, cron jobs, queue workers, and WebSockets — all of which are fully controllable on a VPS you own and manage.
Deploying Laravel with PHP-FPM and Nginx delivers significantly better performance than Apache + mod_php. PHP-FPM manages a separate process pool from the web server, consuming less memory and handling more concurrent requests.
Prerequisites
Before starting, make sure you have the following ready:
- VPS running Ubuntu 22.04 LTS or Debian 12 (at least 1GB RAM recommended)
- Domain name with an A record already pointing to your VPS IP
- SSH access as root or a sudo user
- Basic knowledge of Linux command line
- A Laravel project to deploy, or you'll create a new one
| Software | Recommended Version | Notes |
|---|---|---|
| Ubuntu / Debian | 22.04 LTS / 12 | Primary OS |
| PHP | 8.3 | Laravel 11 requires PHP 8.2+ |
| Nginx | 1.24+ | Web server |
| MySQL / MariaDB | 8.0 / 11.x | Database |
| Composer | 2.x | PHP dependency manager |
| Certbot | latest | SSL Let's Encrypt |
Step 1: Install Nginx and PHP-FPM
Start by updating the package list and installing Nginx with PHP-FPM:
# Update package list
sudo apt update && sudo apt upgrade -y
# Install Nginx
sudo apt install nginx -y
# Add PPA for PHP 8.3 (Ubuntu)
sudo apt install software-properties-common -y
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update
# Install PHP 8.3 FPM and required extensions
sudo apt install php8.3-fpm php8.3-cli php8.3-mysql php8.3-xml \
php8.3-mbstring php8.3-curl php8.3-zip php8.3-bcmath \
php8.3-tokenizer php8.3-json php8.3-redis php8.3-gd -y
# Check PHP version
php -v
# Check PHP-FPM status
sudo systemctl status php8.3-fpm
sudo apt install lsb-release apt-transport-https and add the sury.org repository instead of the PPA.
Verify Nginx is running correctly:
# Start and enable Nginx
sudo systemctl start nginx
sudo systemctl enable nginx
# Check status
sudo systemctl status nginx
# Test config
sudo nginx -t
Open the firewall for HTTP and HTTPS:
sudo ufw allow 'Nginx Full'
sudo ufw allow OpenSSH
sudo ufw enable
sudo ufw status
Step 2: Install Composer
Composer is the PHP dependency manager that Laravel uses to manage all of its packages:
# Download Composer installer
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
# Verify hash (update hash value from https://getcomposer.org/download/)
php -r "if (hash_file('sha384', 'composer-setup.php') === 'HASH_HERE') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
# Install Composer globally
php composer-setup.php --install-dir=/usr/local/bin --filename=composer
# Remove installer
php -r "unlink('composer-setup.php');"
# Check version
composer --version
/usr/local/bin with sudo so all users can access it.
Step 3: Create Laravel Project
There are two approaches to deploying Laravel: creating a new project or cloning from an existing Git repository.
Option 1: Create a new project
# Navigate to web projects directory
cd /var/www
# Create a new Laravel project
sudo composer create-project laravel/laravel myapp --prefer-dist
# Or use the Laravel installer
composer global require laravel/installer
laravel new myapp
Option 2: Clone from a Git repository
# Clone project
cd /var/www
sudo git clone https://github.com/username/myapp.git myapp
# Enter the directory
cd /var/www/myapp
# Install dependencies
sudo composer install --optimize-autoloader --no-dev
# Create .env from .env.example
sudo cp .env.example .env
# Generate application key
sudo php artisan key:generate
Step 4: Configure Nginx Virtual Host for Laravel
Create an Nginx server block for your Laravel project:
sudo nano /etc/nginx/sites-available/myapp
Add the following configuration:
server {
listen 80;
listen [::]:80;
server_name yourdomain.com www.yourdomain.com;
root /var/www/myapp/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
add_header X-XSS-Protection "1; mode=block";
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 ~ \.php$ {
fastcgi_pass unix:/var/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;
}
# Gzip compression
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml;
gzip_min_length 1000;
# Client max body size
client_max_body_size 50M;
}
Enable the site and test:
# Create symlink
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
# Test config
sudo nginx -t
# Reload Nginx
sudo systemctl reload nginx
/public directory, not the project root, to prevent direct access to .env and other sensitive files.
Step 5: Configure .env and Database
Install MySQL and configure the database for Laravel:
# Install MySQL
sudo apt install mysql-server -y
# Secure MySQL installation
sudo mysql_secure_installation
# Log into MySQL
sudo mysql -u root -p
# Create database and user
CREATE DATABASE myapp_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'myapp_user'@'localhost' IDENTIFIED BY 'SecurePassword123!';
GRANT ALL PRIVILEGES ON myapp_db.* TO 'myapp_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Edit Laravel's .env file:
sudo nano /var/www/myapp/.env
APP_NAME="My Application"
APP_ENV=production
APP_KEY=base64:GENERATED_KEY_HERE
APP_DEBUG=false
APP_URL=https://yourdomain.com
LOG_CHANNEL=stack
LOG_LEVEL=error
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=myapp_db
DB_USERNAME=myapp_user
DB_PASSWORD=SecurePassword123!
CACHE_DRIVER=redis
QUEUE_CONNECTION=redis
SESSION_DRIVER=redis
REDIS_HOST=127.0.0.1
REDIS_PORT=6379
Run database migrations:
cd /var/www/myapp
php artisan migrate --force
# Or migrate with seed data
php artisan migrate --seed --force
Step 6: Set Permissions and Storage
Laravel requires write permissions for the storage and bootstrap/cache directories:
# Set ownership to www-data (Nginx/PHP-FPM user)
sudo chown -R www-data:www-data /var/www/myapp
# Set permissions
sudo find /var/www/myapp -type f -exec chmod 644 {} \;
sudo find /var/www/myapp -type d -exec chmod 755 {} \;
# storage and bootstrap/cache need write permission
sudo chmod -R 775 /var/www/myapp/storage
sudo chmod -R 775 /var/www/myapp/bootstrap/cache
# Add current user to www-data group (for development)
sudo usermod -aG www-data $USER
Create the storage link for public assets:
cd /var/www/myapp
php artisan storage:link
Optimize Laravel for production:
# Cache config, routes, views
php artisan config:cache
php artisan route:cache
php artisan view:cache
# Optimize autoloader
composer install --optimize-autoloader --no-dev
APP_DEBUG=false and APP_ENV=production. With APP_DEBUG=true, Laravel will display stack traces and sensitive information to end users.
Step 7: Install SSL with Let's Encrypt
Install a free SSL certificate using Certbot:
# Install Certbot
sudo apt install certbot python3-certbot-nginx -y
# Request SSL certificate
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
# Follow Certbot prompts:
# - Enter email for renewal notifications
# - Accept terms of service
# - Choose redirect HTTP to HTTPS (option 2 recommended)
Verify that auto-renewal works:
# Test renewal
sudo certbot renew --dry-run
# Check cron job Certbot created
sudo cat /etc/cron.d/certbot
# Or check systemd timer
sudo systemctl list-timers | grep certbot
After Certbot runs, the Nginx config is updated automatically. Verify with:
sudo cat /etc/nginx/sites-available/myapp
sudo nginx -t && sudo systemctl reload nginx
Step 8: Set Up Queue Worker with Supervisor
Laravel Queue workers must run continuously. Supervisor manages the process and restarts it automatically if it crashes:
# Install Supervisor
sudo apt install supervisor -y
# Create config file for Laravel queue worker
sudo nano /etc/supervisor/conf.d/laravel-worker.conf
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/myapp/artisan queue:work redis --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/myapp/storage/logs/worker.log
stopwaitsecs=3600
# Reload Supervisor config
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start laravel-worker:*
# Check status
sudo supervisorctl status
Configure Laravel Scheduler via cron job:
# Add cron job
sudo crontab -u www-data -e
# Add this line:
* * * * * cd /var/www/myapp && php artisan schedule:run >> /dev/null 2>&1
sudo apt install redis-server php8.3-redis -y and confirm QUEUE_CONNECTION=redis in .env for maximum performance.
Step 9: Testing and Troubleshooting
Basic Tests
# Test PHP-FPM
sudo php-fpm8.3 -t
# Test Nginx config
sudo nginx -t
# Test database connection
cd /var/www/myapp
php artisan db:monitor
# List routes
php artisan route:list
Common Issues and Fixes
| Issue | Cause | Fix |
|---|---|---|
| 500 Internal Server Error | APP_KEY empty / wrong permissions | php artisan key:generate or check ownership |
| Permission denied (storage) | Wrong ownership | chown -R www-data:www-data storage |
| Class not found | Composer autoload not run | composer dump-autoload |
| Database connection failed | Wrong config / MySQL not running | Check .env and systemctl status mysql |
| 404 Not Found | Nginx root pointing to wrong path | Verify root directive points to /public |
| White screen / blank page | APP_DEBUG=false with error | Check log at storage/logs/laravel.log |
Key Log Files
# Laravel application log
tail -f /var/www/myapp/storage/logs/laravel.log
# Nginx error log
tail -f /var/log/nginx/error.log
# PHP-FPM log
tail -f /var/log/php8.3-fpm.log
# Queue worker log
tail -f /var/www/myapp/storage/logs/worker.log
Deploy Check Script
#!/bin/bash
echo "=== Laravel Deploy Check ==="
echo "PHP version: $(php -v | head -1)"
echo "Nginx status: $(systemctl is-active nginx)"
echo "PHP-FPM status: $(systemctl is-active php8.3-fpm)"
echo "MySQL status: $(systemctl is-active mysql)"
echo "Redis status: $(systemctl is-active redis)"
echo "Supervisor status: $(systemctl is-active supervisor)"
echo ""
cd /var/www/myapp
echo "=== Laravel Status ==="
echo "APP_ENV: $(grep APP_ENV .env | head -1)"
echo "APP_DEBUG: $(grep APP_DEBUG .env | head -1)"
Comparison: Laravel vs Django vs Node.js for VPS
When selecting a framework for a VPS deployment, consider these factors:
| Factor | Laravel (PHP) | Django (Python) | Node.js (Express) |
|---|---|---|---|
| Recommended VPS RAM | 1GB+ | 1GB+ | 512MB+ |
| Deployment ease | Very easy | Moderate | Moderate |
| Built-in ORM | Eloquent (excellent) | Django ORM (excellent) | None (Sequelize/Prisma) |
| Queue system | Built-in (Horizon) | Celery (separate) | Bull/BullMQ (separate) |
| Performance | Good (PHP-FPM) | Very good | Excellent (async) |
| Hosting support | Widest | Moderate | Moderate |
| Developer pool | Very large | Moderate | Large |
| Learning curve | Low–moderate | Moderate | Moderate |
Summary: Laravel is the best choice for teams with a PHP background or those wanting an all-in-one ecosystem. Django suits projects that require data science or ML integration. Node.js excels for real-time applications and microservices.
FAQ
Q: Which PHP version does Laravel require?
Laravel 11 (latest) requires PHP 8.2 or higher. PHP 8.3 is recommended for the best performance. Check the latest requirements at laravel.com/docs.
Q: Should I use Apache or Nginx?
Nginx is recommended for production because it handles static files and concurrent connections better than Apache, uses less memory, and has a more straightforward Laravel configuration. That said, Apache works fine too and Laravel ships with an .htaccess file ready to use.
Q: Is Redis required?
Redis is not required but is strongly recommended for production because it handles cache, sessions, and queues far more efficiently than the file or database drivers. Without Redis, you can use CACHE_DRIVER=file and QUEUE_CONNECTION=database as alternatives.
Q: Composer install runs out of memory — what do I do?
Add temporary swap memory: sudo fallocate -l 1G /swapfile && sudo chmod 600 /swapfile && sudo mkswap /swapfile && sudo swapon /swapfile. Alternatively, prefix the command with COMPOSER_MEMORY_LIMIT=-1.
Q: Nginx shows 403 Forbidden — how do I fix it?
Check three things: (1) the root directive points to Laravel's /public folder, (2) index index.php is present in the config, and (3) chown -R www-data:www-data /var/www/myapp has been run.
Q: Old cache persists after a new deploy — what should I do?
Run these artisan commands after every deploy: php artisan config:clear && php artisan cache:clear && php artisan route:clear && php artisan view:clear. Then rebuild with php artisan optimize.
Q: Which user should I run Artisan commands as?
Run as www-data so generated files have the correct ownership: sudo -u www-data php artisan migrate. Alternatively, add your user to the www-data group.