Laravel queues allow you to defer time-consuming tasks like sending emails or processing uploads. But on shared hosting, there’s no supervisor tool like on VPS. Don’t worry — with cPanel and cron, you can still run queues efficiently.
๐ ️ Prerequisites
- Laravel project already deployed on shared hosting (via cPanel)
- Queueable jobs created using
php artisan make:job - Basic understanding of Laravel's queue system
⚙️ Step 1: Configure Your Laravel Queue
By default, Laravel uses the sync driver, which runs jobs immediately. Update the driver to database (or Redis, if supported):
QUEUE_CONNECTION=database
In your .env file, set the queue driver. If using database, run:
php artisan queue:table && php artisan migrate
This creates the necessary table for storing queued jobs.
๐ Step 2: Locate the Laravel Project Path
Find your full Laravel path on the server. It usually looks like:
/home/your-cpanel-user/your-laravel-folder
You’ll need this to run Artisan commands via cron jobs.
⏱️ Step 3: Add a Cron Job to Process Queues
- Log in to your cPanel account.
- Go to the Cron Jobs section.
- Add a new cron job with a schedule like: Every minute (* * * * *).
- Use the command:
cd /home/your-cpanel-user/your-laravel-folder && php artisan queue:work --tries=3 >> /dev/null 2>&1
This starts Laravel’s queue worker every minute and processes jobs if any are pending.
๐ง Optional: Use queue:listen vs queue:work
On shared hosting, you should prefer queue:work since queue:listen keeps running and won’t be handled properly by cron jobs.
๐ Step 4: Test Your Setup
- Dispatch a job using
dispatch(new YourJob()). - Wait a minute and check if the task completed (e.g. email sent, file processed).
- Check
storage/logs/laravel.logfor any errors.
๐งช Troubleshooting Tips
- Ensure
php artisancommands work via terminal or cron. - If needed, use the full PHP path (e.g.,
/usr/local/bin/php). - Verify that
jobsandfailed_jobstables exist if using the database queue.
๐ Done!
Even without root access or Supervisor, you can run Laravel queues reliably on shared hosting using cron jobs. This setup works great for small to medium apps.
If this helped you get your Laravel queues running on cPanel, feel free to share it or drop a link to your project!
Comments
Post a Comment