DEV Community

Cover image for Running Laravel Queue Workers on a Server with Supervisor
Seye Yemi-Olowolabi
Seye Yemi-Olowolabi

Posted on

Running Laravel Queue Workers on a Server with Supervisor

Introduction

Laravel, one of the most popular PHP frameworks, provides a powerful way to manage and process background tasks through its queue system. To ensure that your queue workers run continuously and recover from failures automatically, it's common practice to use Supervisor, a process control system.

In this guide, I will walk you through the steps of setting up Supervisor to run the php artisan queue:work command for your Laravel project. This will help you manage and monitor your queue workers effectively.

Prerequisites

Before you begin, make sure you have the following prerequisites:

  • A Laravel project set up on your server.

Step 1: Install Supervisor

1.1. If Supervisor is not already installed on your server, you can install it using your package manager. For example, on Ubuntu, you can install it with the following command:

sudo apt-get install supervisor

Step 2: Create a Supervisor Configuration File

2.1. Navigate to your Laravel project directory, where you want to run the queue worker. You should be in the root directory of your Laravel project.

2.2. Create a Supervisor configuration file for your Laravel queue worker. You can do this with a text editor or a command-line text editor like nano. In this example, we'll create a file called laravel-worker.conf:
nano /etc/supervisor/conf.d/laravel-worker.conf

2.3. Add the following configuration to the file, adjusting the paths and settings according to your Laravel project:

[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php artisan queue:work
directory=/path/to/your/laravel/project
user=www-data
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/path/to/your/laravel/project/storage/logs/worker.log

I will also show you the one on my personal website for more clarity of what it should look like

[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /www/wwwroot/mywebsite.com/artisan queue:work
directory=/www/wwwroot/mywebsite.com
user=www-data
autostart=true
autorestart=true
redirect_stderr=true
stdout_logfile=/www/wwwroot/mywebsite.com/storage/logs/worker.log

Step 3: Update Supervisor Configuration

3.1. After creating the Supervisor configuration file, update Supervisor to read the new configuration and start the worker:

supervisorctl reread
supervisorctl update
supervisorctl start laravel-worker:*

Step 4: Check Worker Status

4.1. You can check the status of the Laravel queue worker by running:

supervisorctl status
It should show the status of the laravel-worker program, indicating that it is running.

Stopping the Worker

To stop or kill the php artisan queue:work process managed by Supervisor, you can use the supervisorctl command:

  1. Navigate to your Laravel project directory.

  2. Use supervisorctl to stop the php artisan queue:work process, replacing laravel-worker with your actual program name:

supervisorctl stop laravel-worker:*

  1. Confirm that the process has been stopped by checking its status:

supervisorctl status

The status should indicate that the laravel-worker program is no longer running.

Conclusion

Using Supervisor to manage your Laravel queue workers ensures that they run continuously and are automatically restarted in case of failures. This setup helps you process background tasks reliably, ensuring that your Laravel application stays responsive and efficient.

With this guide, you can set up Supervisor to run php artisan queue:work effortlessly, improving the reliability and stability of your Laravel application.

Thank you for reading, Happy Hacking :)

Top comments (0)