How to Deploy Laravel with Supervisor and Nginx

Deploying a Laravel app might sound scary at first, but I promise it’s not rocket science. When I first tried, I had no idea what I was doing. My site loaded super slow, and queue jobs kept failing. But once I learned how to use Supervisor and Nginx, everything clicked.

In this guide, I’ll walk you through how to deploy your Laravel app using Nginx as the web server and Supervisor to keep your background workers running. Don’t worry—I’ll explain what these tools are and how they help. Plus, I’ll throw in a few puns, because who says deployment can’t be fun?


What’s This All About?

Let’s start with some quick definitions:

  • Laravel: A popular PHP framework that helps you build web apps. It has tools for routing, security, and more.
  • Nginx (pronounced “engine-x”): A web server that delivers your website to visitors. It’s like the delivery driver for your site.
  • Supervisor: A program that makes sure your background jobs keep running. If something crashes, Supervisor restarts it. Like a babysitter for your apps.

These three tools work really well together. Laravel handles the code, Nginx handles the traffic, and Supervisor handles the workers.


Why Use Supervisor?

Laravel comes with a feature called queues. Queues let you delay tasks like sending emails or processing files. This keeps your app speedy. But for queues to work, you need a worker running in the background.

That’s where Supervisor comes in. It keeps that worker alive—forever and ever. If the worker stops (it happens), Supervisor starts it again. No drama, no panic.

I once forgot to set up Supervisor, and none of my jobs ran for a whole day. Oops.


What You’ll Need

Before we jump in, here’s what you’ll need ready:

  • A Linux server (I use Ubuntu).
  • A Laravel app already on the server.
  • A working PHP and MySQL/MariaDB setup.
  • Composer (a PHP tool) installed.
  • Nginx and Supervisor installed.

If you don’t have them yet, I’ll show you how to install them below.


Step 1: Set Up Laravel

Let’s say your Laravel app is in this folder:

/var/www/my-laravel-app

Make sure the folder belongs to the web server user:

sudo chown -R www-data:www-data /var/www/my-laravel-app

Also, set the right permissions:

sudo chmod -R 755 /var/www/my-laravel-app

Then go into the project folder and run:

cd /var/www/my-laravel-app
composer install
php artisan migrate
php artisan config:cache

If you’re using .env, make sure your database info is correct. Laravel can be picky if anything’s off.


Step 2: Install and Configure Nginx

To install Nginx:

sudo apt update
sudo apt install nginx

Now, we need to create a config file for your Laravel site.

sudo nano /etc/nginx/sites-available/laravel

Paste this example config:

server {
    listen 80;
    server_name yourdomain.com;

    root /var/www/my-laravel-app/public;

    index index.php index.html;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}

Make sure to change yourdomain.com and the PHP version to match your server.

Now enable the site:

sudo ln -s /etc/nginx/sites-available/laravel /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx

If your site loads in the browser, congrats—Nginx is working.


Step 3: Set Up Queue Workers with Supervisor

If your Laravel app uses queues (check your code for dispatch()), you’ll need a worker.

Install Supervisor:

sudo apt install supervisor

Then create a config file for Laravel’s queue:

sudo nano /etc/supervisor/conf.d/laravel-worker.conf

Paste this in:

[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/my-laravel-app/artisan queue:work --sleep=3 --tries=3
autostart=true
autorestart=true
user=www-data
numprocs=1
redirect_stderr=true
stdout_logfile=/var/log/supervisor/laravel-worker.log

Then update Supervisor and start the worker:

sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start laravel-worker:*

Want to check the log?

tail -f /var/log/supervisor/laravel-worker.log

You’ll see output whenever a job runs. It’s like watching Laravel work out at the gym.


Benefits I Noticed

After using Supervisor and Nginx together, I saw these improvements:

  • My site loaded faster, even when lots of users visited.
  • Jobs like emails and reports didn’t slow down the site.
  • Crashed workers came back on their own.

It felt like having an automatic repair crew on call 24/7.


Handy Reminders

Here are three little things I wish someone told me sooner:

  • Don’t forget .env: Laravel needs the right environment settings, especially for database and queues.
  • Log files grow fast: Clean up /var/log/supervisor/ once in a while.
  • Restart Supervisor if you change queue settings: sudo supervisorctl restart laravel-worker:*

Common Problems (and Fixes)

Here are some hiccups I hit and how I solved them:

  • 403 Forbidden: Check folder permissions. Make sure Nginx can read them.
  • Queue not running: Make sure Supervisor started the worker and that Laravel is using the right queue driver (.env should say QUEUE_CONNECTION=database or redis).
  • PHP errors in Nginx: Double-check your PHP version and Nginx config.

Troubleshooting is kind of like playing detective—except your suspects are always the config files.


How This Stack Compares

Here’s a quick look at using Supervisor + Nginx vs. Apache or just running php artisan serve:

Stack Pros Cons
Laravel + Nginx + Supervisor Fast, reliable, good for production Slightly more setup
Laravel + Apache Easier to start with Slower under heavy load
php artisan serve Great for testing Not for production use

I like Nginx + Supervisor because it feels stable. Like a steady bike instead of a unicycle.


Wrapping Things Up (Without Tying Knots)

Setting up Laravel with Supervisor and Nginx made my deployments smoother. It takes a bit of effort at first, but after that, your app runs on auto-pilot.

If you’re building anything with queues, I highly recommend using Supervisor. And if you’re still using artisan serve in production, I gently beg you—stop. It’s like using a butter knife to cut a steak.

Leave a Reply