How to setup Laravel Queues on cPanel: A Step-by-Step Guide
Posted By
kamlesh paulon
Dec 13, 2024In a previous article, How to Deploy Laravel to Shared Hosting (cPanel) in 2024: The Easy Way, we covered how to set up Laravel on cPanel. Once your Laravel app is running smoothly on shared hosting, the next step is likely setting up additional features like the Laravel Queues and Scheduler.
At first glance, you might think setting up Laravel queues on cPanel is as simple as adding cron jobs for the following commands:
But this is where things can go wrong. I had the same thought and ran into an issue where the scheduler worked smoothly, but my queue jobs failed with the error:
“App\Jobs\GenerateContactEmail has been attempted too many times.”
If you’ve encountered similar problems, follow this step-by-step guide to set up your Laravel queues on cPanel correctly.
Table of contents
- Step 1: Set Up Laravel on cPanel
- Step 2: Configure the Laravel Scheduler on cPanel
- Step 3: Configure the Laravel Queues
- FAQ
Step 1: Set Up Laravel on cPanel
Before anything else, ensure that your Laravel project is deployed correctly. If you haven’t already, check out my detailed guide on How to Deploy Laravel to Shared Hosting (cPanel) in 2024: The Easy Way.
Step 2: Configure the Laravel Scheduler on cPanel
To schedule your Laravel tasks via cPanel’s cron job feature, follow these steps:
- In your cPanel dashboard, go to Cron Jobs.
- Add a new cron job with the following command:
Make sure the PHP path
/usr/local/bin/php
and the Laravel project path/home/{accountName}/laravel
are correct. These need to be absolute paths for the command to work.
Step 3: Configure the Laravel Queues
Now, to set up the queue worker properly, you need to modify your app/Console/Kernel.php
file.
Add this code inside the schedule()
method:
This ensures the queue worker runs every minute and prevents job overlaps by using the withoutOverlapping()
method, so your queue jobs won’t be retried unnecessarily, which was causing the attempted too many times
error.
FAQ
Why do I get the error App\Jobs\SendWelcomeEmail has been attempted too many times
?
This error usually occurs when the queue worker is overlapping, meaning the same job is being attempted multiple times before the previous instance completes. To prevent this, use the --stop-when-empty flag and the withoutOverlapping() method in your schedule() function within app/Console/Kernel.php.
What does the --stop-when-empty
flag do in Laravel queue work?
The
--stop-when-empty
flag ensures that the queue worker stops after processing all jobs in the queue, which helps to avoid potential overlaps or unnecessary job retries.
Can I use shared hosting like cPanel to run Laravel queues efficiently?
Yes, while shared hosting like cPanel has limitations, by setting up cron jobs and using the proper configuration like
withoutOverlapping()
, you can run Laravel queues and scheduler efficiently. However, for larger applications, a dedicated or cloud server might be more suitable.
Do I need to manually run php artisan queue:work
on cPanel?
No, once you configure the queue worker in the
schedule()
function using cron jobs, it will run automatically. There’s no need to manually execute the command each time.