Laravel FPM Crashes on cPanel VPS: Why My Queue Workers Keep Failing and How to Fix Them in 10 Minutes or Less
If you’ve ever stared at a red‑lined php-fpm log while your Laravel queue workers silently drop like dead fish, you know the level of frustration. One minute everything is humming, the next your VPS screams “service stopped” and your users see blank pages. The good news? Most of those crashes are caused by a handful of mis‑configurations that you can resolve in under ten minutes.
Why This Matters
Your Laravel queues power email notifications, order processing, and any background task that keeps the front‑end fast. When php-fpm dies, every worker stops, orders pile up, and revenue leaks. For SaaS founders, a reliable queue is not a nice‑to‑have—it’s a revenue guarantee.
Common Causes of PHP‑FPM Crashes on cPanel VPS
- Memory limits too low for concurrent workers.
- Improper
pm.max_childrenvs. available RAM. - Missing or mismatched
composerautoload files. - Redis connection timeout causing worker hangs.
- cPanel’s Apache + mod_fcgid conflicting with Nginx proxy.
- Out‑of‑date PHP extensions (e.g.,
pdo_mysql).
Step‑By‑Step Fix Tutorial (≈10 min)
1. Verify PHP‑FPM Status
systemctl status php-fpm
# or on cPanel
service php-fpm status
2. Check Memory Usage
free -m
# Look for available RAM vs. swap
3. Tune www.conf
Open the pool configuration (usually /opt/cpanel/ea-php*/root/etc/php-fpm.d/www.conf) and adjust:
pm = dynamic
pm.max_children = 20 ; 20 workers ~ 2GB RAM (adjust)
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 10
php_admin_value[memory_limit] = 256M
pm.max_children × memory_limit should be ≤ 80% of total RAM.
4. Restart PHP‑FPM
systemctl restart php-fpm
# Verify no errors
journalctl -u php-fpm -f
5. Optimize Supervisor Config for Laravel Workers
# /etc/supervisor/conf.d/laravel-queue.conf
[program:laravel-queue]
process_name=%(program_name)s_%(process_num)02d
command=php /home/username/public_html/artisan queue:work redis --sleep=3 --tries=3
autostart=true
autorestart=true
user=username
numprocs=4
stopwaitsecs=360
stderr_logfile=/var/log/laravel-queue.err.log
stdout_logfile=/var/log/laravel-queue.out.log
6. Reload Supervisor
supervisorctl reread
supervisorctl update
supervisorctl status
7. Test the Queue
php artisan queue:restart
php artisan tinker
>>> dispatch(new \App\Jobs\SendWelcomeEmail($user));
php-fpm no longer crashes under normal load.
VPS or Shared Hosting Optimization Tips
- Swap Management: Disable swap on VPS for faster OOM detection (
swapoff -a). - Nginx Front‑End: Use Nginx as a reverse proxy to Apache; it buffers and reduces FPM pressure.
- Opcode Caching: Enable
opcachewithopcache.memory_consumption=192. - Redis Persistence: Set
save 900 1inredis.conffor durability without heavy I/O. - MySQL Tuning:
innodb_buffer_pool_size=70%of RAM for DB‑heavy apps.
Real World Production Example
Acme SaaS runs 12 Laravel workers on a 4 vCPU, 8 GB Ubuntu 22.04 VPS. After applying the above pm.max_children and Supervisor settings, they saw:
- PHP‑FPM restarts drop from 12/day to 0.
- Queue latency improve from 23 s to 3 s.
- CPU load stabilize at 0.6‑0.8 on average.
Before vs. After Results
| Metric | Before | After |
|---|---|---|
| FPM Crashes | 7 / day | 0 |
| Queue Latency | 23 s | 3 s |
| CPU Avg. | 1.4 | 0.7 |
Security Considerations
- Run PHP‑FPM under a dedicated system user (e.g.,
php-fpm) to limit file system exposure. - Disable
allow_url_fopenandexecinphp.iniunless required. - Use Cloudflare WAF to block malicious HTTP floods that can exhaust FPM workers.
- Keep Composer dependencies up‑to‑date:
composer update --prefer-dist.
pm.max_children higher than your RAM can support. It will trigger OOM kills and may expose your server to privilege escalation.
Bonus Performance Tips
- Enable
realpath_cache_size=4096kinphp.inifor faster file lookups. - Use
php artisan optimizeafter each deploy to generate a compiled class map. - Store heavy assets on a CDN; offload static files from your VPS.
- Profile queue jobs with
Laravel Telescopeto spot slow I/O. - Consider Dockerizing the queue worker with a dedicated
php-fpmcontainer for isolation.
FAQ
Q: My VPS uses Apache only. Do I still need Nginx?
A: Not required, but a lightweight Nginx proxy dramatically reduces Apache’s request parsing overhead and protects php-fpm from sudden spikes.
Q: How many workers should I run?
A: Start with cpu_cores × 2 and adjust based on memory consumption. Use htop to watch RAM while jobs process.
Q: Does Cloudflare interfere with Laravel queue signatures?
A: Only if you enable “Automatic HTTPS Rewrites” that alter webhook URLs. Whitelist your API endpoints in Cloudflare page rules.
Q: My queue keeps stopping after a code push. What now?
A: Run php artisan queue:restart and ensure Supervisor reloads the new binary. Also clear config:cache and route:cache.
Final Thoughts
Laravel queue reliability on a cPanel VPS boils down to three pillars: right‑sized PHP‑FPM, a solid Supervisor config, and a bit of OS‑level tuning. Spend ten minutes fixing those settings, and you’ll eliminate the dreaded “FPM crashed” log entries, keep your API fast, and protect your bottom line.
No comments:
Post a Comment