When traffic to your Laravel application starts to grow, shared hosting often feels slow and limited. The solution: move to a VPS. This article guides you through deploying Laravel to an Ubuntu VPS using Nginx and PHP-FPM, step by step, perfect for those using a VPS for the first time.
Prerequisites
- A VPS running Ubuntu (e.g. 22.04) with root/sudo SSH access.
- A domain already pointed (A record) to the VPS IP.
- Your Laravel project (ideally already in a Git repository).
Step 1: Update the Server & Install Basic Packages
sudo apt update && sudo apt upgrade -y
sudo apt install -y nginx git unzip curl
Step 2: Install PHP & Laravel Extensions
Install PHP-FPM along with the extensions Laravel needs:
sudo apt install -y php-fpm php-cli php-mysql php-mbstring \
php-xml php-bcmath php-curl php-zip php-gd
Check the PHP version to use later in the Nginx configuration:
php -v
Step 3: Install Composer
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
Step 4: Pull the Application Code
cd /var/www
sudo git clone https://github.com/user/project.git laravel_app
cd laravel_app
composer install --optimize-autoloader --no-dev
Step 5: Configure .env
cp .env.example .env
php artisan key:generate
nano .env # fill in APP_URL, DB_DATABASE, DB_USERNAME, DB_PASSWORD, etc.
Set APP_ENV=production and APP_DEBUG=false. Then run the migrations:
php artisan migrate --force
Step 6: Set Permissions
Give folder ownership to the Nginx user (www-data) and write permissions to the folders that need them:
sudo chown -R www-data:www-data /var/www/laravel_app
sudo chmod -R 775 /var/www/laravel_app/storage /var/www/laravel_app/bootstrap/cache
Step 7: Create the Nginx Server Block
Create a new configuration file:
sudo nano /etc/nginx/sites-available/laravel_app
Fill it with the following configuration (adjust the domain and PHP-FPM version):
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
root /var/www/laravel_app/public;
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
Note that the root line points to the public folder, and fastcgi_pass must match your PHP-FPM version.
Step 8: Enable the Site & Reload Nginx
sudo ln -s /etc/nginx/sites-available/laravel_app /etc/nginx/sites-enabled/
sudo nginx -t # test the configuration
sudo systemctl reload nginx
Your application should now be accessible via the domain.
Step 9: Install Free HTTPS (Let's Encrypt)
sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com
Certbot automatically configures the SSL certificate and renews it periodically.
Step 10: Production Optimization
php artisan config:cache
php artisan route:cache
php artisan view:cache
Quick Troubleshooting
- 502 Bad Gateway: the
fastcgi_passpath is wrong — check the PHP-FPM socket name (ls /var/run/php/). - 500 / white screen: check the
storagepermissions & the log instorage/logs/laravel.log. - 403 Forbidden: make sure
rootpoints to thepublicfolder, not the project root.
Conclusion
Deploying Laravel to an Ubuntu VPS with Nginx gives you far greater performance and control than shared hosting. The keys are: install PHP-FPM + extensions, point the Nginx root to the public folder, set permissions, then install HTTPS. Once you're used to it, this process can be completed in 15–20 minutes for subsequent deployments.