skip to Main Content

I am using jobs in Laravel but do not know how to them in production on shared server

I ‘m not sure my shared server is able to handle the ubuntu supervisor script to handle queues
Is there any other way I can make job / queues work in production.
I’ surprised at the very few & vague answers to this on the net.
I am running Laravel 10
[I understand I could be using cron Jobs and a scheduled command to run the worker:
(https://stackoverflow.com/q/46141652/2955335)
Can any one lead me on the right path

2

Answers


  1. Most shared hosting providers don’t allow running Supervisor scripts, so if you’re on shared hosting, cron jobs are the best alternative to handle Laravel jobs, queues, and scheduling. Here’s how you can set it up:

    1. Using Cron Jobs for Queues

    2. Using Laravel Scheduler,

      php /project-path/artisan schedule:run >> /dev/null 2>&1

    Budget Consideration
    If you or your client has the budget, consider moving to a VPS (Virtual Private Server). A VPS gives you full control over the server, allowing you to use Supervisor for managing queues more efficiently. It’s a better option for handling Laravel queues and jobs in production.

    Login or Signup to reply.
  2. HI I’m Salman Senior Software Engineer & DevOps

    On shared hosting, where you may not have access to install or manage supervisor, you can still handle Laravel queues effectively by using cron jobs to run the queue worker. Here’s how you can set it up:

    ---
    
    ### 1. Use `database` as Your Queue Driver
    Since shared hosting typically doesn't support Redis or other advanced queue drivers, use the `database` driver. Update your `.env` file:
    ```env
    QUEUE_CONNECTION=database
    ```
    
    Run the migration to create the jobs table:
    ```bash
    php artisan queue:table
    php artisan migrate
    ```
    
    ---
    
    ### 2. Use a Scheduled Command to Run Queue Workers
    You can use a cron job to simulate a queue worker on shared hosting. 
    
    #### Add a Cron Job:
    In your shared hosting control panel (e.g., cPanel, Plesk), go to **Cron Jobs** and create a cron job with the following command:
    ```bash
    * * * * * php /path/to/your/project/artisan queue:work --stop-when-empty
    ```
    
    Replace `/path/to/your/project/` with the full path to your Laravel project.
    
    #### Explanation:
    - `* * * * *` runs the command every minute.
    - `queue:work` processes jobs in the queue.
    - `--stop-when-empty` ensures the worker stops after processing the current jobs, preventing a runaway process on shared hosting.
    
    ---
    
    ### 3. Alternative: Use `queue:listen`
    If you can’t use cron jobs to start a worker, use the `queue:listen` command in the background:
    ```bash
    php artisan queue:listen > /dev/null 2>&1 &
    ```
    
    However, this requires SSH access, and it might stop running after the session ends unless you use tools like `screen` or `nohup`.
    
    ---
    
    ### 4. Fallback: Add Queue Processing to `schedule:run`
    If your shared hosting only allows one cron job, integrate the queue worker into the scheduler. Update `app/Console/Kernel.php`:
    ```php
    protected function schedule(Schedule $schedule)
    {
        $schedule->command('queue:work --stop-when-empty')->everyMinute();
    }
    ```
    
    Set up a single cron job for the scheduler:
    ```bash
    * * * * * php /path/to/your/project/artisan schedule:run >> /dev/null 2>&1
    ```
    
    ---
    
    ### 5. Optimize Job Processing
    To minimize resource usage on shared hosting:
    - Use `queue:work` with the `--stop-when-empty` flag instead of `queue:listen`.
    - Reduce the number of retries in `config/queue.php`:
      ```php
      'retry_after' => 60,
      ```
    - Limit job memory consumption with `--memory`:
      ```bash
      php artisan queue:work --memory=128 --stop-when-empty
      ```
    
    ---
    

    By combining these steps, you can efficiently run Laravel queues on a shared hosting environment without needing supervisor.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search