Introduction
Setting up Supervisor for Laravel on Ubuntu AWS is essential for managing queue workers efficiently. Supervisor is a process control system that allows you to manage and monitor Laravel queues, ensuring they run in the background without interruption. This guide provides a step-by-step setup process and an SH script to automate the installation.
Prerequisites
- A Linux-based server (Ubuntu/Debian recommended)
- Laravel installed and configured
- Root or sudo access
Step-by-Step Setup
Step 1: Install Supervisor
Run the following command to install Supervisor:
sudo apt update && sudo apt install -y supervisor
Step 2: Create a Supervisor Configuration File
Navigate to the Supervisor configuration directory:
cd /etc/supervisor/conf.d/
Create a new configuration file for Laravel queues:
sudo nano laravel-worker.conf
Add the following content, adjusting paths as necessary:
laravel-worker.conf setup file code sem as file name [program:laravel-worker]
Laravel root folder /path/to/your/laravel/artisan
Ex . /var/www/html/project_name/
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /path/to/your/laravel/artisan queue:work --tries=3
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
user=www-data
numprocs=1
redirect_stderr=true
stdout_logfile=/path/to/your/laravel/storage/logs/worker.log
Step 3: Update Supervisor Configuration
Reload Supervisor to recognize the new configuration:
sudo supervisorctl reread
sudo supervisorctl update
Step 4: Start the Laravel Worker
Start the Laravel worker process:
sudo supervisorctl start laravel-worker:*
Step 5: Verify the Status
Check the status of Supervisor processes:
sudo supervisorctl status
Automating the Setup with an SH Script
Create an SH script to automate the Supervisor setup:
#!/bin/bash
# Update system
sudo apt update && sudo apt install -y supervisor
# Create Supervisor config file for Laravel worker
cat <<EOL | sudo tee /etc/supervisor/conf.d/laravel-worker.conf
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /path/to/your/laravel/artisan queue:work --tries=3
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
user=www-data
numprocs=1
redirect_stderr=true
stdout_logfile=/path/to/your/laravel/storage/logs/worker.log
EOL
# Reload Supervisor
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl start laravel-worker:*
# Show status
sudo supervisorctl status
echo "Supervisor setup completed successfully."
- Save the script as
setup_supervisor.sh
- Make it executable:
chmod +x setup_supervisor.sh
- Run the script:
setup_supervisor.sh