Deploy Laravel on AWS
Get a Laravel application onto EC2 with Nginx, PHP-FPM, a queue worker and the scheduler.
A Laravel deployment needs four running pieces: the web server, PHP-FPM, a queue worker, and cron for the scheduler. Miss the last two and the application looks fine until something needs to happen in the background.
Point Nginx at the public directory#
The document root is public, never the project root. Getting this wrong exposes your .env file to the internet.
server {
listen 80;
server_name example.com;
root /var/www/app/public;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ /\.(?!well-known).* {
deny all;
}
}Run the queue under Supervisor#
queue:work is a long-running process. It will exit, and something has to restart it.
[program:app-worker]
command=php /var/www/app/artisan queue:work --sleep=3 --tries=3 --max-time=3600
autostart=true
autorestart=true
user=www-data
numprocs=2
redirect_stderr=true
stdout_logfile=/var/log/app-worker.log
stopwaitsecs=3600One cron entry runs the whole schedule#
* * * * * cd /var/www/app && php artisan schedule:run >> /dev/null 2>&1Cache configuration on deploy#
These commands turn dozens of file reads per request into one. Run them on every deploy, after the code changes.
php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan migrate --forceCheck before you finish#
Confirm APP_DEBUG=false, that storage is writable, that the queue is processing, and that a scheduled task has actually run.