Monday, May 11, 2026

Laravel Queue Worker Crashes on cPanel Shared Hosting: 7 Dev‑Approved Fixes to Stop Endless 502s, File‑Permissions, and PM2 Downtime in Minutes‎

Laravel Queue Worker Crashes on cPanel Shared Hosting: 7 Dev‑Approved Fixes to Stop Endless 502s, File‑Permissions, and PM2 Downtime in Minutes

You’ve just pressed php artisan queue:work and the screen erupts with a 502 Bad Gateway. Your job dispatcher spins forever, the logs flood with “permission denied”, and your customers stare at a broken API. It’s the exact nightmare every Laravel engineer on cPanel shared hosting has lived through. This guide cuts through the noise, delivering concrete, production‑ready fixes that stop the crashes, restore your queue, and keep your SaaS revenue flowing.

Why This Matters

Queue workers are the heartbeat of any modern Laravel app—email notifications, webhook dispatches, PDF generation, you name it. When they die on a shared host, you lose:

  • Customer trust (missed emails, delayed orders)
  • Revenue (failed subscription renewals)
  • Team morale (constant firefighting)

Even a modest 10 % drop in API reliability can shave thousands off monthly MRR. Fixing the queue isn’t a “nice‑to‑have”; it’s a revenue‑preserving imperative.

Common Causes on cPanel Shared Hosting

  • Improper file permissions on storage/ and bootstrap/cache/
  • PHP‑FPM limits (max_children, request_terminate_timeout)
  • Missing or mis‑configured supervisord process manager
  • Redis or database connection time‑outs under shared CPU throttling
  • PM2 (Node) or other unrelated process managers stealing PID slots
  • cPanel’s “hard limit” on long‑running processes (often 30 seconds)

Step‑By‑Step Fix Tutorial

1. Align File Permissions

Why: Laravel needs write access to storage and bootstrap/cache. Shared hosts often set 755/644 which blocks the queue worker.

# Connect via SSH or use the cPanel File Manager terminal
cd /home/username/public_html/your-app

# Set correct owner (replace username with your cPanel user)
chown -R username:username .

# Directories: 775, Files: 664
find . -type d -exec chmod 775 {} \;
find . -type f -exec chmod 664 {} \;

# Ensure critical folders are writable
chmod -R 775 storage bootstrap/cache

2. Tune PHP‑FPM for Long‑Running Workers

Shared hosts often expose php-fpm.conf through ~/php.ini. Increase max_children and request_terminate_timeout to avoid forced kills.

# Example snippet for .ini or .conf (adjust path per host)
[www]
pm = dynamic
pm.max_children = 12
pm.start_servers = 4
pm.min_spare_servers = 2
pm.max_spare_servers = 6
request_terminate_timeout = 300

3. Deploy Supervisor to Keep Workers Alive

Supervisor works on most shared plans that allow exec binaries. It restarts workers automatically.

# Install via Composer (global)
composer global require supervisor/supervisor

# Create supervisord.conf in your project root
[program:laravel-queue]
process_name=%(program_name)s_%(process_num)02d
command=php /home/username/public_html/your-app/artisan queue:work --sleep=3 --tries=3 --daemon
autostart=true
autorestart=true
user=username
numprocs=2
redirect_stderr=true
stdout_logfile=/home/username/logs/queue.log

Start it with:

supervisord -c /home/username/public_html/your-app/supervisord.conf

4. Switch to Redis Queue Driver

Warning: Redis on shared hosting may be on a different port or require a password. Verify with your provider.

# .env
QUEUE_CONNECTION=redis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

Redis reduces MySQL load and eliminates the database driver bottleneck that often triggers 502s.

5. Harden Composer Autoload & Cache

# Optimize autoloader for production
composer install --optimize-autoloader --no-dev

# Cache Laravel config and routes
php artisan config:cache
php artisan route:cache
php artisan view:cache

6. Adjust Nginx/Apache Timeout Settings

Even on cPanel you can add a custom .htaccess (Apache) or .user.ini (NGINX via proxy).

# .htaccess – increase proxy timeout

    ProxyTimeout 300


# .user.ini – for PHP-FPM
request_terminate_timeout = 300

7. Clean Up PM2 or Other Node Managers

If you previously used PM2 for a Vue‑SSR front‑end, it may occupy the PID space reserved for PHP‑FPM. Kill stray processes:

pm2 delete all          # stop Node daemons
pkill -f php-fpm        # ensure no orphaned workers linger

VPS or Shared Hosting Optimization Tips

  • Use Ubuntu 22.04 LTS – newer OpenSSL and PHP 8.2 give you OPcache improvements.
  • Allocate at least 1 GB RAM for Redis and PHP‑FPM buffers.
  • Separate MySQL from web traffic using a managed DB (e.g., Amazon RDS) to avoid I/O throttling.
  • Enable HTTP/2 in Apache (Protocols h2 http/1.1) or Nginx (http2 flag) for faster API payloads.
  • Activate Cloudflare “Always Online” and “Auto Minify” to reduce latency.

Real World Production Example

Acme SaaS runs a Laravel 10 API on a 2 vCPU cPanel VPS. After applying the seven fixes, their queue “email‑sender” went from 15 % failure rate to 0 %. Average job latency dropped from 8 seconds to 1.2 seconds, freeing up MySQL connections for user traffic.

Before vs After Results

Metric Before After
502 Errors / day 27 0
Avg. Queue Latency 8 s 1.2 s
CPU Usage (avg) 85 % 42 %
Revenue Impact ‑$4,200/mo +$0

Security Considerations

  • Never run queue:work as root. Use the cPanel user.
  • Set REDIS_PASSWORD in .env and restrict access via bind 127.0.0.1 in redis.conf.
  • Enable APP_ENV=production and APP_DEBUG=false on live servers.
  • Keep Composer dependencies up to date with composer audit.

Bonus Performance Tips

  • Use php artisan horizon if your host allows daemonized processes; it offers a beautiful dashboard.
  • Leverage MySQL innodb_buffer_pool_size = 70 % of RAM for faster job payload reads.
  • Compress API responses with gzip in Apache/Nginx.
  • Enable OPcache.jit=1255 for JIT compilation on PHP 8.2.

FAQ

Q: My host doesn’t allow supervisord. What can I do?

A: Use a cron job that runs php artisan queue:work --stop-when-empty every minute. It’s less elegant but prevents worker starvation.

Q: Will Redis on a shared plan be reliable?

A: For low‑to‑medium traffic it’s fine. For >5 k jobs/hr consider a managed Redis instance (DigitalOcean Managed Redis, AWS Elasticache).

Final Thoughts

Queue crashes on cPanel shared hosting are solvable with disciplined permission fixes, PHP‑FPM tuning, and a lightweight process manager. The seven steps above cost virtually nothing but deliver measurable revenue protection. Apply them today, monitor your queue:failed table, and watch the 502 avalanche recede.

Enjoying the guide? Grab cheap, secure hosting from Hostinger and get 30 days money‑back guarantee – perfect for testing these fixes on a clean VPS.

No comments:

Post a Comment