Logics Guru

Deploy Laravel on AWS

Get a Laravel application onto EC2 with Nginx, PHP-FPM, a queue worker and the scheduler.

1 min read 3 views Advanced

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.

Text/etc/nginx/sites-available/app
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.

Text/etc/supervisor/conf.d/worker.conf
[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=3600

One cron entry runs the whole schedule#

Text
* * * * * cd /var/www/app && php artisan schedule:run >> /dev/null 2>&1

Cache configuration on deploy#

These commands turn dozens of file reads per request into one. Run them on every deploy, after the code changes.

Text
php artisan config:cache
php artisan route:cache
php artisan view:cache
php artisan migrate --force

Check before you finish#

Confirm APP_DEBUG=false, that storage is writable, that the queue is processing, and that a scheduled task has actually run.

Mustasim Ali

Mustasim Ali

Senior Software Engineer & Technical Lead

Full-stack engineer working in PHP and Laravel since 2019. I lead a development team building web and mobile products, and spend most of my time in Laravel, Node.js, Vue and React against MySQL and MongoDB. Logics Guru is where I write up the things I had to work out the hard way — the architecture decisions, the debugging sessions, and the small utilities I kept rebuilding until I put them somewhere permanent. Everything here is what I actually use.

Related

Redis

Configure Redis Caching in Laravel

Set Redis up as the cache and queue driver, and understand when tags help and when they do not.

Mustasim Ali 1 min Intermediate