A Step-by-Step Guide to Setting Up Supervisord for Laravel in Ubuntu
Tags: ubuntu, laravel, supervisord
Published: 2023-03-16 08:19:42

Supervisord is a process management tool that can help you manage processes in your Ubuntu system. It's especially useful if you need to run long-running processes, such as those involved in background tasks, cron jobs, or queue workers. Laravel, a popular PHP framework, relies heavily on background tasks and queue workers to handle tasks such as sending emails, processing uploaded files, and more. In this article, we'll guide you through the process of setting up Supervisord in Ubuntu for Laravel.


### Step 1: Install Supervisord

The first step is to install Supervisord on your Ubuntu system. You can do this using the apt package manager:

```
> sudo apt-get update
> sudo apt-get install supervisor
```
This will install Supervisord and its dependencies on your system.


### Step 2: Create a Supervisor Configuration File

Next, you need to create a configuration file for Supervisor. This file will contain information about the processes that you want to manage with Supervisord.

Create a new configuration file with the following command:

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

This will open a new file in the Nano text editor. In this file, you can specify the configuration for the Laravel worker process. Here's an example configuration:

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

This configuration sets up a Laravel worker process that will run the **queue:work** command with a sleep time of 3 seconds and a maximum of 3 retries. It will run as the **www-data** user and write its logs to **/var/log/laravel-worker.log**.

You can adjust these settings to fit your needs. Make sure to replace **/var/www/your_project/artisan** with the actual path to your Laravel application's **artisan** command.

Save and close the file by pressing **Ctrl + X**, then **Y**, then **Enter**.

### Step 3: Reload Supervisor

Now that you've created your Supervisor configuration file, you need to reload Supervisor to apply the changes:

```
> sudo supervisorctl reread
> sudo supervisorctl update
```

This will read your new configuration file and update Supervisor's configuration.

### Step 4: Start the Laravel Worker

Finally, you can start the Laravel worker process using the following command:

```
> sudo supervisorctl start laravel-worker:*
```

This will start the Laravel worker process and any child processes that it spawns.

You can check the status of the Laravel worker process using the following command:

```
> sudo supervisorctl status
```

This will show you the status of all processes that Supervisor is managing, including your Laravel worker process.


### Conclusion

In this article, we've shown you how to set up Supervisord in Ubuntu for Laravel. By following these steps, you can manage your Laravel worker processes with ease and ensure that they run reliably and consistently. Remember to update your configuration file and reload Supervisor whenever you make changes to your Laravel worker process.