CodingTricks LogoCodingTricks
HomePostsTipsCopy/PasteLinksContact UsAbout Us
2024 - 2025 CodingTricks.co | All rights reserved
Privacy PolicyTerms of Service
How to setup Laravel Queues on cPanel: A Step-by-Step Guide

How to setup Laravel Queues on cPanel: A Step-by-Step Guide

Posted by

kamlesh paul

on

Dec 13, 2024

| 3 min read

Last updated on : Dec 13, 2024

ServerQueue
162 views

In 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:

php artisan schedule:run
php artisan queue:work

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.”

has-been-attempted-too-many-times.webp

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
    • Why do I get the error App\Jobs\SendWelcomeEmail has been attempted too many times?
    • What does the --stop-when-empty flag do in Laravel queue work?
    • Can I use shared hosting like cPanel to run Laravel queues efficiently?
    • Do I need to manually run php artisan queue:work on cPanel?

#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:

  1. In your cPanel dashboard, go to Cron Jobs.
  2. Add a new cron job with the following command:
* * * * * /usr/local/bin/php /home/{accountName}/laravel/artisan schedule:run

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:

app/Console/Kernel.php
$schedule->command('queue:work --stop-when-empty')
         ->everyMinute()
         ->withoutOverlapping();

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.

Related Posts

  • How to Customize FrankenPHPHow to Customize FrankenPHP
  • How to Protect Your Laravel from Spam IPs Using the Laravel Abuse IP PackageHow to Protect Your Laravel from Spam IPs Using the Laravel Abuse IP Package
  • How to Protect Your Linux Server from Brute Force SSH AttacksHow to Protect Your Linux Server from Brute Force SSH Attacks
  • How to Manage Multiple GitHub Accounts on the Same ComputerHow to Manage Multiple GitHub Accounts on the Same Computer
  • How to Set Up Queue Jobs in NextJS Using BullMQHow to Set Up Queue Jobs in NextJS Using BullMQ

Tags

Api(1)Authentication(5)Backup (1)Copy Paste(12)Email(2)Express(1)Firebase(1)Github Action(2)News(8)Push Notification(1)Queue(2)Server(11)Server Action(3)Testing(1)Tips(17)Websocket(1)

Popular Posts

  • TweakPHP 0.1.0 Beta: A Free and Open-Source Alternative to Tinkerwell Is Here!  TweakPHP 0.1.0 Beta: A Free and Open-Source Alternative to Tinkerwell Is Here!
  • How to use WebSocket in NextJS App router with Socket.IOHow to use WebSocket in NextJS App router with Socket.IO
  • How to Set Up Queue Jobs in NextJS Using BullMQHow to Set Up Queue Jobs in NextJS Using BullMQ
  • Boost Laravel Performance: Running Octane with FrankenPHP in Production ( Zero downtime)Boost Laravel Performance: Running Octane with FrankenPHP in Production ( Zero downtime)
  • How to Set Up NextJS cron jobs without VercelHow to Set Up NextJS cron jobs without Vercel
  • Mastering Laravel Streamed Responses: Boost Performance with Fast Data DeliveryMastering Laravel Streamed Responses: Boost Performance with Fast Data Delivery
  • Tinkerwell Alternative: Free and Open-Source PHP Debugging with TweakPHPTinkerwell Alternative: Free and Open-Source PHP Debugging with TweakPHP
  • Nextjs 14 roles and permissions (RBAC) : Step-by-Step GuideNextjs 14 roles and permissions (RBAC) : Step-by-Step Guide

Get updates directly to your inbox.

Join 500+ developers getting updates on Laravel & Next.js tips. No spam,
unsubscribe anytime.


Share this article:

162 views