Laravel Queue Workers Gone Dead on a cPanel VPS: 7 Hidden PHP‑FPM Config Tweaks That Actually Stop the Crash
If you’ve ever watched a Laravel queue grind to a halt on a cheap cPanel VPS, you know the mix of panic‑induced coffee consumption and the dread of a production outage. One minute your API is serving 5 k rps, the next php-fpm silently dies and the workers disappear into the void. The frustration is real, the downtime is costly, and the fix is rarely obvious.
Why This Matters
Queue workers power email dispatch, notifications, data imports, and any async heavy‑lifting in modern SaaS apps. When they die, users see delayed emails, lost webhooks, and a bruised reputation. On a VPS with cPanel you’re also sharing resources with other accounts, so a mis‑configured PHP‑FPM can bring the whole stack down.
Common Causes
- Default
pm.max_childrenfar too low for your traffic spikes. - Insufficient
request_terminate_timeoutcausing PHP processes to be killed before they finish. - Wrong
rlimit_filesandrlimit_coresettings that hit the OS limit. - Supervisor not restarting dead workers fast enough.
- Redis connection time‑outs causing jobs to be re‑queued endlessly.
- MySQL slow‑query lockups that block the worker process.
- cPanel’s
mod_fcgidinterfering with PHP‑FPM sockets.
Step‑By‑Step Fix Tutorial
1️⃣ Tune PHP‑FPM Pool Settings
Open your pool file (usually /opt/cpanel/ea-php*/root/etc/php-fpm.d/www.conf) and apply the following overrides:
# Increase children to match CPU cores + 2
pm = dynamic
pm.max_children = 30
pm.start_servers = 6
pm.min_spare_servers = 4
pm.max_spare_servers = 12
# Give long jobs breathing room
request_terminate_timeout = 300
request_slowlog_timeout = 10
slowlog = /var/log/php-fpm/www-slow.log
# Raise OS limits
rlimit_files = 65535
rlimit_core = unlimited
2️⃣ Adjust php.ini for Memory & Execution
In /opt/cpanel/ea-php*/root/etc/php.ini set:
memory_limit = 512M
max_execution_time = 300
max_input_time = 300
3️⃣ Optimize Supervisor Configuration
Supervisor is the watchdog that restarts dead workers. Edit /etc/supervisord.d/laravel-queue.conf:
[program:laravel-queue]
process_name=%(program_name)s_%(process_num)02d
command=php /home/username/webapp/artisan queue:work redis --sleep=3 --tries=3 --timeout=300
autostart=true
autorestart=true
user=username
numprocs=4
priority=990
stopwaitsecs=360
stdout_logfile=/home/username/logs/queue.stdout.log
stderr_logfile=/home/username/logs/queue.stderr.log
After saving, run:
supervisorctl reread && supervisorctl update && supervisorctl restart laravel-queue:*
numprocs to ceil(CPU_COUNT / 2) for best CPU utilization.
4️⃣ Harden Redis Connection
In .env make sure you’re using a persistent connection and a reasonable timeout:
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
REDIS_TIMEOUT=5
QUEUE_CONNECTION=redis
Then restart Redis:
systemctl restart redis
5️⃣ MySQL Slow‑Query Log & Indexes
Enable the slow‑query log and add missing indexes that the queue jobs rely on.
# /etc/mysql/my.cnf
[mysqld]
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 1
log_queries_not_using_indexes = 1
After editing, run:
systemctl restart mysql
6️⃣ Nginx (or Apache) Proxy Buffering
If you’re behind Nginx, add a fastcgi buffer for PHP‑FPM sockets:
# /etc/nginx/conf.d/php-fpm.conf
fastcgi_buffer_size 64k;
fastcgi_buffers 8 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 256k;
For Apache with mod_proxy_fcgi:
# /etc/httpd/conf.d/php-fpm.conf
ProxyPassMatch ^/(.*\.php)$ fcgi://127.0.0.1:9000/home/username/webapp/$1
7️⃣ Cloudflare & Rate Limiting
Set a generous “Burst” value in Cloudflare’s “Rate Limiting” page so that sudden API spikes don’t force the queue to retry.
VPS or Shared Hosting Optimization Tips
- Prefer a dedicated VPS over shared cPanel when running Laravel queues.
- Allocate at least 2 vCPU and 4 GB RAM for a production queue.
- Use RAID‑1 SSD storage for low I/O latency.
- Disable unnecessary cPanel plugins that spawn extra PHP workers.
- Enable
cagefsonly if you need per‑user isolation; otherwise it adds overhead.
Real World Production Example
Company Acme SaaS ran a Laravel‑based newsletter service on a 2‑core cPanel VPS. Workers crashed every 4–6 hours, causing a 12‑hour email backlog. After implementing the seven PHP‑FPM tweaks, they reduced worker crashes from 24 per week to zero, cut email delivery latency from 45 min to under 2 min, and saved $150 / month on an upgraded plan.
Before vs After Results
| Metric | Before | After |
|---|---|---|
| Worker Crashes/Day | 12‑18 | 0 |
| Avg Job Latency | 45 min | 2 min |
| CPU Utilization | 85 % | 55 % |
Security Considerations
- Never run
php-fpmas root. Use a dedicated system user per site. - Set
listen.ownerandlisten.groupto the same user that runs Supervisor. - Enable
opcache.blacklist_filenamefor sensitive paths. - Use
disable_functionsto blockexec,system,shell_execin shared environments. - Restrict Redis access to localhost or a VPC private subnet.
Bonus Performance Tips
- Turn on
opcache.validate_timestamps=0for production builds. - Run
composer install --optimize-autoloader --no-devduring deployment. - Cache config and routes:
php artisan config:cache && php artisan route:cache. - Use
php artisan queue:restartafter each code push to avoid stale workers. - Leverage
horizondashboard for real‑time queue metrics.
FAQ
- Q: My VPS uses Apache only. Do I still need the Nginx buffer settings?
- No, but you should enable
ProxyPassMatchwithmod_proxy_fcgias shown above. - Q: How often should I restart the queue workers?
- After every deployment or any change to
.env. A simplephp artisan queue:restartwill gracefully kill old processes. - Q: Can I use Docker instead of cPanel?
- Absolutely. Docker isolates PHP‑FPM, Redis, and MySQL, making the above tweaks part of your
docker-compose.ymlconfiguration.
Final Thoughts
Queue stability on a cPanel VPS is rarely a “code bug” – it’s a server‑tuning problem. By adjusting the hidden PHP‑FPM parameters, aligning Supervisor, and tightening Redis/MySQL, you turn a flaky dev environment into a rock‑solid production worker farm. The seven tweaks cost minutes to implement, but they save hours of detective work and thousands of dollars in lost revenue.
No comments:
Post a Comment