Setting Up Laravel Queue Workers on Ubuntu with PHP 8.3: A Step-by-Step Guide
To set up php artisan queue:work --queue
on Ubuntu, follow these steps:
Step 1: Ensure Your Environment is Ready
Install PHP and Composer (if not already installed):
sudo apt update sudo apt install php-cli php-mbstring unzip curl curl -sS https://getcomposer.org/installer | php sudo mv composer.phar /usr/local/bin/composer
Install Supervisor (for managing background processes):
sudo apt install supervisor
Step 2: Set Up the Laravel Queue Worker
Navigate to Your Laravel Project Directory:
cd /path/to/your/laravel/project
Run the Queue Worker Command to Test:
php artisan queue:work --queue=high,default,low
Replace
high,default,low
with your specific queue names.If Successful, Configure Supervisor for Continuous Running.
Step 3: Configure Supervisor
Create a Configuration File for Supervisor:
sudo nano /etc/supervisor/conf.d/laravel-worker.conf
Add the Following Configuration:
[program:laravel-worker] process_name=%(program_name)s_%(process_num)02d command=/usr/bin/php8.1 /path/to/your/laravel/project/artisan queue:work --queue=high,default,low --sleep=3 --tries=3 autostart=true autorestart=true user=www-data numprocs=1 redirect_stderr=true stdout_logfile=/path/to/your/laravel/project/storage/logs/worker.log
Save and Exit.
Step 4: Enable and Start Supervisor
Reload Supervisor to Apply Changes:
sudo supervisorctl reread sudo supervisorctl update
Start the Laravel Worker:
sudo supervisorctl start laravel-worker:*
Step 5: Monitor and Debug
Check the Logs:
tail -f /path/to/your/laravel/project/storage/logs/worker.log
View Supervisor Status:
sudo supervisorctl status
This setup ensures that your Laravel queues are always processed in the background, even after server restarts.