Monday, May 11, 2026

Laravel Queue Workers Hang and Crash on Shared Hosting: Why FPM Timeouts Kill Your Jobs and the Quick Fix for cPanel Environments

Laravel Queue Workers Hang and Crash on Shared Hosting: Why FPM Timeouts Kill Your Jobs and the Quick Fix for cPanel Environments

Picture this: you’ve just pushed a new feature, the queue is pumped with 5,000 jobs, and within minutes your Laravel workers start silently dying. No logs, no errors—just a hanging process that eventually crashes the whole site. If you’re on a cPanel shared host, the culprit is almost always the PHP‑FPM timeout. In this article we’ll rip the problem apart, show you the exact configuration tweak that saves your jobs, and give you a full checklist to keep your Laravel‑on‑WordPress stacks humming on any VPS or shared environment.

Why you’ll care: Queue failures affect API response times, break email pipelines, and can cost you real money when customers hit a dead‑end. Fixing the timeout is the fastest way to regain stability without re‑architecting your whole system.

Why This Matters

Queue workers are the heartbeat of modern Laravel applications—processing emails, generating PDFs, syncing with WordPress, and more. When they crash:

  • Customers see delayed notifications.
  • SEO‑critical pages stall because background jobs can’t clear caches.
  • Server CPU spikes as failed processes are repeatedly respawned by Supervisor.
  • Hosting providers may throttle your account, thinking it’s a DDoS attack.

Common Causes of Worker Hang‑Ups on Shared Hosting

1. PHP‑FPM request_terminate_timeout Too Low

cPanel typically ships with request_terminate_timeout = 30 seconds. A long job that needs 2‑3 minutes will be brutally killed, leaving the worker in a zombie state.

2. Inadequate Supervisor Settings

Supervisor may think a worker is “stuck” after 30 seconds of silence and force a restart, which repeats the timeout loop.

3. Missing Redis Connection Pool

If you’re using Redis for queue storage, a default maxclients of 10,000 can be exhausted on a busy shared host, causing workers to wait indefinitely.

4. Composer Autoloader Bloat

Running composer install --no-dev without optimizing the autoloader inflates memory usage, pushing the process over the pm.max_children limit and triggering FPM kills.

TIP: Always run composer dump‑autoload -o after a deployment, especially on shared hosting where memory limits are tight.

Step‑By‑Step Fix Tutorial for cPanel & Supervisor

Step 1 – Locate the PHP‑FPM Pool File

# On most cPanel servers
cd /opt/cpanel/ea-php*/root/etc/php-fpm.d
ls

Step 2 – Increase request_terminate_timeout

[www]
user = youruser
group = youruser
listen = /opt/cpanel/ea-php*/root/var/run/php-fpm/www.sock
pm = dynamic
pm.max_children = 15
pm.start_servers = 3
pm.min_spare_servers = 2
pm.max_spare_servers = 6
request_terminate_timeout = 300   ; 5 minutes

Save the file and restart PHP‑FPM:

service php-fpm restart

Step 3 – Adjust Supervisor Configuration

[program:laravel-queue]
process_name=%(program_name)s_%(process_num)02d
command=php /home/youruser/public_html/artisan queue:work redis --sleep=3 --tries=3 --timeout=300
autostart=true
autorestart=true
user=youruser
numprocs=2
redirect_stderr=true
stdout_logfile=/home/youruser/logs/laravel-queue.log
stopwaitsecs=360

Then reload Supervisor:

supervisorctl reread
supervisorctl update
supervisorctl restart laravel-queue:*
SUCCESS: Workers now have a 5‑minute window before FPM intervenes, and Supervisor respects the same timeout.

Step 4 – Optimize Redis for Shared Hosts

# In redis.conf (if you have access)
maxmemory 256mb
maxmemory-policy allkeys-lru
maxclients 2000

If you cannot edit redis.conf, add these flags to your Docker‑run command or ask your host to increase the limit.

VPS or Shared Hosting Optimization Tips

  • Use Nginx as a reverse proxy in front of Apache to offload static assets and reduce PHP‑FPM concurrency.
  • Set opcache.enable_cli=1 for faster Artisan commands.
  • Enable MySQL query cache for frequent read‑heavy jobs (e.g., SELECT … FROM users WHERE id = ?).
  • Deploy with Laravel Envoy to run zero‑downtime queue restarts.
  • Monitor with htop or Glances and set alerts for CPU > 80% for more than 5 minutes.

Real World Production Example

Acme Media runs a WordPress‑Laravel hybrid on a 2 CPU, 4 GB VPS. Their nightly email campaign processes 12,000 jobs. Before the fix, the queue would stall at 2,000 jobs, and the supervisor log filled with “FPM: request terminated”. After applying the 5‑minute timeout and adding --timeout=300 to the Artisan command, the job completed in 7 minutes with zero restarts.

Before vs After Results

Metric Before Fix After Fix
Avg Job Time 2 min (timeout at 30 s) 3 min (stable)
Worker Crashes 7 per hour 0
CPU Spike 95 % 45 %

Security Considerations

Never expose your .env file via the web root. On shared hosts, set php_value open_basedir to limit file system access for queue workers.
  • Run queue workers under a dedicated system user (not root).
  • Use --quiet flag in production to suppress accidental debug output.
  • Rotate Redis passwords every 30 days; store them in .env only.

Bonus Performance Tips

  • Batch Jobs: Use dispatchNow() for small tasks or chunk() inside a job to process 500 records per iteration.
  • Cache Heavy Lookups: Cache::remember('user_'.$id, 300, fn()=>User::find($id));
  • Use Horizon: Laravel Horizon adds a dashboard, auto‑scales workers, and gracefully handles timeouts.
  • Compress Composer Autoload: composer install --prefer-dist --optimize-autoloader
  • Enable HTTP/2: If you’re on Apache, add Protocols h2 h2c http/1.1 for faster API calls.

FAQ

Q: Does increasing the FPM timeout affect other sites on the same server?

A: Yes, it raises the max execution time for every PHP request in that pool. On a crowded shared host, consider creating a dedicated pool for your Laravel app.

Q: My host doesn’t allow editing php-fpm.d. What can I do?

Use a custom .user.ini with max_execution_time=300 and rely on Supervisor’s --timeout. It’s not as clean but often works.

Q: Should I switch to Docker on shared hosting?

Most shared providers block Docker, but you can use Cloudways or a cheap VPS where Docker gives you full control over PHP‑FPM and Redis limits.

Final Thoughts

Queue reliability is non‑negotiable for any Laravel‑powered service, especially when you’re blending it with WordPress on a shared cPanel host. By extending the PHP‑FPM timeout, aligning Supervisor’s --timeout, and tightening Redis and Composer settings, you turn a flaky job processor into a rock‑solid background engine. Apply the steps above, monitor the logs, and you’ll see CPU, latency, and error rates drop dramatically.

Want an affordable, secure VPS that gives you full control over PHP‑FPM, Redis, and Nginx? Check out Hostinger’s cheap secure hosting – perfect for Laravel and WordPress developers who need performance without the shared‑host headache.

No comments:

Post a Comment