How to run supervisor for Laravel Queue Worker on Plesk
The supervisor will take care of restarting your queue worker whenever its stopped. It’s necessary to restart queue worker on each deploy, so it reflects the code changes.
Setup
Login via SSH with root or any user with sudo rights. Install supervisor:
sudo apt-get install supervisor
Create a Supervisor configuration file for your Laravel queue worker. For example, create a file named laravel-worker.conf
in the /etc/supervisor/conf.d/
directory:
sudo nano /etc/supervisor/conf.d/laravel-worker.conf
Add the following configuration, adjusting the paths and options as needed:
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=/opt/plesk/php/8.2/bin/php /var/www/vhosts/yourdomain.com/httpdocs/artisan queue:work
autostart=true
autorestart=true
user=your-username
numprocs=1
redirect_stderr=true
stdout_logfile=/var/www/vhosts/yourdomain.com/httpdocs/storage/logs/worker.log
- Replace
your-username
with the username that owns the Laravel project files. - Ensure that
artisan
and thestorage/logs
directory paths are correct. - Ensure that your PHP version is correct
Save the file and exit the text editor. Update Supervisor to read the new configuration:
sudo supervisorctl reread
sudo supervisorctl update
Start the Laravel worker process with Supervisor:
sudo supervisorctl start laravel-worker
You can check the status of the worker:
sudo supervisorctl status
Add to deploy script
Since queue workers are long-lived processes, they will not notice changes to your code without being restarted. So, the simplest way to deploy an application using queue workers is to restart the workers during your deployment process. You may gracefully restart all of the workers by issuing the queue:restart
command:
Note, the queue:restart
command does not restart your jobs. It simply signals workers to stop gracefully after completing their current job, preventing them from picking up new ones. When Supervisor detects that a worker has stopped, it automatically restarts a new worker, ensuring it runs with the latest deployed code and migrations.
If your deployment strategy involves spinning up a new VPS (or container) for each deployment, queue:restart
and supervisor is typically not needed for the new instance, as it will automatically pick up queued jobs. However, to ensure that workers in the old container exit gracefully before termination, you should run queue:restart
before shutting down the old instance. This prevents jobs from being interrupted and allows the old workers to finish their current tasks cleanly before the container is removed.
Now your deployment script should work. Enjoy!