Setting Up Laravel Queue Workers on Ubuntu with PHP 8.3: A Step-by-Step Guide

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

  1. 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
    
  2. Install Supervisor (for managing background processes):

     sudo apt install supervisor
    

Step 2: Set Up the Laravel Queue Worker

  1. Navigate to Your Laravel Project Directory:

     cd /path/to/your/laravel/project
    
  2. Run the Queue Worker Command to Test:

     php artisan queue:work --queue=high,default,low
    

    Replace high,default,low with your specific queue names.

  3. If Successful, Configure Supervisor for Continuous Running.


Step 3: Configure Supervisor

  1. Create a Configuration File for Supervisor:

     sudo nano /etc/supervisor/conf.d/laravel-worker.conf
    
  2. 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
    
  3. Save and Exit.


Step 4: Enable and Start Supervisor

  1. Reload Supervisor to Apply Changes:

     sudo supervisorctl reread
     sudo supervisorctl update
    
  2. Start the Laravel Worker:

     sudo supervisorctl start laravel-worker:*
    

Step 5: Monitor and Debug

  1. Check the Logs:

     tail -f /path/to/your/laravel/project/storage/logs/worker.log
    
  2. View Supervisor Status:

     sudo supervisorctl status
    

This setup ensures that your Laravel queues are always processed in the background, even after server restarts.

Did you find this article valuable?

Support Mandeep Singh Blog by becoming a sponsor. Any amount is appreciated!