- Authors
- Name
- Nguyễn Đức Xinh
- Published on
- Published on
Running Laravel Queue with Supervisor on Ubuntu 22.04: A Step-by-Step Guide
Laravel, one of the most popular PHP frameworks, offers a powerful tool for processing background tasks and jobs through its queue system. If you're hosting your Laravel application on an Ubuntu 22.04 server, you can ensure efficient queue management by using Supervisor. This guide will take you through the steps of installing Supervisor and configuring it to run your Laravel queue worker reliably.
Prerequisites
Before you begin, make sure you have:
- An AWS EC2 instance running Ubuntu 22.04.
- A Laravel application installed on your server.
Step 1: Update and Upgrade
First things first, ensure your Ubuntu system is up-to-date:
sudo apt update
sudo apt upgrade -y
Step 2: Install Supervisor
Install Supervisor, a process control system, with this straightforward command:
sudo apt install supervisor
Step 3: Configure Supervisor for Laravel Queue Worker
Now, let's set up Supervisor to manage your Laravel queue worker.
- Create a Configuration File Start by creating a Supervisor configuration file specifically for your Laravel queue worker. You can name it laravel-worker.conf:
sudo nano /etc/supervisor/conf.d/laravel-worker.conf
# or
sudo vim /etc/supervisor/conf.d/laravel-worker.conf
- Add Configuration Details Next, populate the configuration file with the following settings. Be sure to replace placeholders with your actual project details:
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)s
command=php /path/to/your/laravel/project/artisan queue:work --sleep=3 --tries=3
autostart=true
autorestart=true
user=your-username
numprocs=8
redirect_stderr=true
stdout_logfile=/path/to/your/laravel/project/storage/logs/worker.log
- command: Specify the command to run the Laravel queue worker.
- autostart and autorestart: Ensure that the worker starts automatically and restarts if it fails.
- user: Replace your-username with your user account.
- numprocs: Adjust the number of worker processes to match your requirements.
- stdout_logfile: Define the path to the log file. E.g
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)s
command=php /var/www/apps/laravel/artisan queue:work --sleep=3 --tries=3
autostart=true
autorestart=true
user=www-data
numprocs=2
redirect_stderr=true
stdout_logfile=/var/www/apps/laravel/storage/logs/worker.log
- Save and Exit Save the configuration file, and then exit the text editor.
Step 4: Reread and Update Supervisor
Once you've created the Supervisor configuration, let Supervisor know about it by rereading its configuration and updating its processes:
sudo supervisorctl reread
sudo supervisorctl update
Step 5: Start the Laravel Queue Worker
It's time to initiate the Laravel queue worker using Supervisor:
sudo supervisorctl start laravel-worker:*
Step 6: Check the Status
To check the status of the Laravel queue worker being managed by Supervisor:
sudo supervisorctl status
This will display the current status of your laravel-worker process.
Step 7: Monitor Worker Output
To monitor the output of the queue worker in real-time, tail the log file defined in the Supervisor configuration:
tail -f /path/to/your/laravel/project/storage/logs/worker.log
With Supervisor in control, your Laravel application's queue worker is now running efficiently in the background, processing queued jobs. If any issues arise, Supervisor will automatically restart the worker, ensuring smooth operation.
With your Laravel queue worker skillfully managed by Supervisor, you can focus on creating powerful web applications without worrying about background job processing.
Happy coding!