6 Easy Steps: How to Install and Configure Supervisor for a Laravel Application on Linux

Configure Supervisor

Laravel Supervisor is a powerful process monitoring tool that ensures your Laravel application’s background tasks, such as queue processing and task scheduling, run smoothly and reliably. In this guide, we’ll walk through the steps to set up Laravel Supervisor on a Linux server, enabling you to manage your Laravel instance tasks efficiently.

Step 1: Install Supervisor

Supervisor is available through the default package manager on most Linux distributions. Follow the installation steps below for your specific operating system:

For Debian/Ubuntu:
sudo apt-get update
sudo apt-get install supervisor
For CentOS/RHEL:
sudo yum install epel-release
sudo yum install supervisor

This will install Supervisor and its related dependencies on your Linux system. After the installation, Supervisor should be running as a background service. You can check its status using the command:

sudo systemctl status supervisor

Step 2: Configure Supervisor for Laravel Queues

Once Supervisor is installed, you need to create a configuration file to manage your Laravel application’s queue workers. This configuration file will define how Supervisor should start, stop, and monitor your worker processes.

2.1 Create a Supervisor Configuration File

To create a Supervisor configuration file for Laravel, run the following command:

sudo nano /etc/supervisor/conf.d/laravel-worker.conf
2.2 Add the Following Configuration

Add the following content to the laravel-worker.conf file:

[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /path-to-your-laravel-project/artisan queue:work --sleep=3 --tries=3 --timeout=90
autostart=true
autorestart=true
user=your-user-name
numprocs=3
redirect_stderr=true
stdout_logfile=/path-to-your-laravel-project/storage/logs/worker.log

In this configuration:

  • command: Specifies the command to run the Laravel queue worker.
  • autostart: Automatically starts the process when Supervisor starts.
  • user: Sets the user under which the process should run. Replace your-user-name with your Linux username.
  • numprocs: Number of worker processes to start. Adjust this based on your server’s capacity and application needs.
  • stdout_logfile: Sets the log file location for the output of the worker processes. Ensure this path is writable by the specified user.

Pro Tip: If you have a high volume of queued jobs, consider increasing the numprocs value to create multiple worker processes.

Step 3: Update Supervisor Configuration

After creating the configuration file, you need to update Supervisor to read the new configuration:

sudo supervisorctl reread
sudo supervisorctl update

These commands tell Supervisor to read any new configuration files and apply them to its process management.

Step 4: Start the Laravel Worker

To start the Laravel worker process defined in the configuration, use the following command:

sudo supervisorctl start laravel-worker:*

This command will start all the worker processes defined under the laravel-worker configuration.

Pro Tip: If you want to start only specific processes, replace * with the process number, like laravel-worker:1.

Step 5: Monitor the Worker Process

Supervisor provides several commands to help you monitor and control your worker processes. Here are some useful commands:

Check the Status of the Worker:
sudo supervisorctl status
Restart the Worker Process:
sudo supervisorctl restart laravel-worker:*
Stop the Worker Process:
sudo supervisorctl stop laravel-worker:*

These commands provide full control over the Laravel worker processes managed by Supervisor.

Step 6: Ensure Supervisor Starts on Boot

To make sure Supervisor starts automatically when your server boots, you need to enable the Supervisor service:

For Debian/Ubuntu:
sudo systemctl enable supervisor
For CentOS/RHEL:
sudo systemctl enable supervisord

Troubleshooting Tips

If you encounter any issues during installation or configuration, here are some troubleshooting tips:

  • Logs Not Updating: Ensure the path to the log file specified in the configuration is correct and that the user running process manager has write permissions to that file.
  • Process Not Starting: Double-check the command paths in the configuration file. Ensure that your Laravel project directory is correct and accessible.

For more detail you can write Laravel Documentation

Summary

background job manager is a robust tool for managing processes in your Laravel project, and with the steps outlined above, you should be able to install, configure, and monitor your Laravel queues and workers with ease.

You may also find interesting:

Effortlessly Upload Images to AWS S3 in Laravel | Complete Step-by-Step Guide

Post a Comment

Leave a Reply

Your email address will not be published. Required fields are marked *