Laravel is the most popular PHP framework. This guide covers a complete production deployment: PHP-FPM, Nginx, Composer dependencies, environment configuration, and the essential Laravel-specific optimization commands.
Prerequisites
- PHP installed with required extensions (see How to Install PHP on Ubuntu & Debian)
- Composer installed
- Nginx installed
- MySQL/MariaDB or PostgreSQL installed
Step 1 — Transfer Your Application
rsync -avz --exclude 'node_modules' --exclude 'vendor' ./myapp/ deploy@YOUR_SERVER_IP:/var/www/myapp/
Step 2 — Install PHP Dependencies
cd /var/www/myapp
composer install --no-dev --optimize-autoloader
Step 3 — Configure the Environment File
cp .env.example .env
nano .env
Set your database credentials, APP_URL, and:
APP_ENV=production
APP_DEBUG=false
Never leave APP_DEBUG=true in production — it exposes sensitive stack traces and configuration to visitors on error pages.
Step 4 — Generate the Application Key
php artisan key:generate
Step 5 — Run Database Migrations
php artisan migrate --force
Step 6 — Set Correct Permissions
sudo chown -R www-data:www-data /var/www/myapp
sudo chmod -R 755 /var/www/myapp/storage /var/www/myapp/bootstrap/cache
Step 7 — Configure Nginx
sudo nano /etc/nginx/sites-available/myapp
server {
listen 80;
server_name myapp.example.com;
root /var/www/myapp/public;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx
Step 8 — Cache Configuration for Production Performance
php artisan config:cache
php artisan route:cache
php artisan view:cache
Step 9 — Add HTTPS
sudo certbot --nginx -d myapp.example.com
Step 10 — Set Up the Scheduler (If Your App Uses Laravel's Task Scheduling)
sudo crontab -e
* * * * * cd /var/www/myapp && php artisan schedule:run >> /dev/null 2>&1
Deploying Updates
cd /var/www/myapp
git pull
composer install --no-dev --optimize-autoloader
php artisan migrate --force
php artisan config:cache
php artisan route:cache
php artisan view:cache
Common Errors
500 error with no useful detail — check Laravel's own log:
tail -f /var/www/myapp/storage/logs/laravel.log
"The stream or file ... could not be opened" — a storage/cache directory permission issue; re-run the chown/chmod commands from Step 6.
Changes to .env not taking effect — clear the config cache: php artisan config:clear, then re-cache if needed.
Best Practices
- Always set
APP_DEBUG=falseandAPP_ENV=production - Run the artisan cache commands after every deployment for production performance
- Point the web root specifically at Laravel's
publicdirectory, never the project root
Related Articles
- How to Install PHP on Ubuntu & Debian
- How to Install Composer for PHP Dependency Management
- How to Install and Secure MySQL 8 on Ubuntu & Debian
