Laravel 10 FPM Crash on cPanel Shared Host – Why My Queue Workers Die in 10 Seconds and How to Fix It in Minutes
If you’ve ever watched a Laravel queue processor die after a handful of jobs on a cPanel shared host, you know the feeling: heart‑pounding frustration, sleepless nights, and a client who’s suddenly asking, “Why is the API so slow?” In this article I break down the exact cause of the dreaded “PHP‑FPM crash” on shared servers, show you a step‑by‑step fix that works in under ten minutes, and give you production‑grade tweaks to keep your Laravel 10 app alive on any VPS or shared environment.
Why This Matters
Queue workers are the backbone of any modern Laravel SaaS – they send emails, process images, push notifications, and keep your API responsive. When they disappear after 10 seconds, users see delayed emails, missing webhooks, and a spike in error logs that can tank your WordPress performance metrics and SEO rankings. Moreover, a crashing php-fpm process can bring down other sites on the same shared host, costing you reputation and revenue.
Common Causes
- Too‑low
pm.max_childrenlimit in the FPM pool. - Memory limit (
php.inimemory_limit) far below the 256 MiB a typical Laravel worker needs. - Supervisor not re‑spawning workers fast enough, causing cascade failures.
- Redis connection timeout on a shared Redis instance.
- Composer autoload optimization missing, leading to heavy bootstrapping.
- cPanel’s
mod_fcgidinterference with long‑running processes.
Step‑By‑Step Fix Tutorial
1. Locate the FPM Pool for Your Domain
cPanel stores the pool file under /opt/cpanel/ea-php*/root/etc/php-fpm.d/. Identify the correct version (Laravel 10 recommends PHP 8.2).
cd /opt/cpanel/ea-php82/root/etc/php-fpm.d
ls -1
2. Edit the Pool Settings
Open the pool that matches your domain (e.g., example.com.conf) and adjust the following values:
[www]
user = yourcpaneluser
group = yourcpaneluser
listen = /opt/cpanel/ea-php82/root/usr/var/run/php-fpm/example.com.sock
;--- Increase workers---
pm = dynamic
pm.max_children = 12 ; enough for 4 queue workers + web traffic
pm.start_servers = 4
pm.min_spare_servers = 2
pm.max_spare_servers = 6
;--- Boost memory---
php_admin_value[memory_limit] = 512M
php_admin_value[max_execution_time] = 300
pm.max_children under your host’s limit (usually 15–20 on shared plans) to avoid “500 Internal Server Error”.3. Restart PHP‑FPM
sudo systemctl restart ea-php82-php-fpm
# or on cPanel:
scripts/restartsrv_php_fpm_82
4. Configure Supervisor for Laravel Workers
If Supervisor is not installed, ask your provider for a supervisord service or use a cron‑based “queue:work” fallback.
[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /home/yourcpaneluser/example.com/artisan queue:work redis --sleep=3 --tries=3 --timeout=120
autostart=true
autorestart=true
user=yourcpaneluser
numprocs=4
redirect_stderr=true
stdout_logfile=/home/yourcpaneluser/example.com/storage/logs/worker.log
After saving, run:
supervisorctl reread
supervisorctl update
supervisorctl status laravel-worker*
5. Optimize Composer Autoload
composer install --optimize-autoloader --no-dev
php artisan config:cache
php artisan route:cache
php artisan view:cache
6. Verify Redis Connectivity
redis-cli -h 127.0.0.1 -p 6379 ping
# Should return PONG
If you get a timeout, increase the Redis client timeout in .env:
REDIS_CLIENT=predis
REDIS_TIMEOUT=5
VPS or Shared Hosting Optimization Tips
- Use Nginx on a VPS. Put PHP‑FPM behind a fast
proxy_passand enablefastcgi_buffering. - On Apache, enable
mod_proxy_fcgi. Disablemod_phpto free memory. - Allocate at least 2 GiB RAM for a small Laravel app with queues.
- Enable
opcache.enable_cli=1for artisan commands. - Deploy via Git hooks that run
composer install --no-devandphp artisan migrate --force.
Real World Production Example
Company Acme SaaS ran a Laravel 10 API on a cPanel shared plan with an 8‑core Intel Xeon and 2 GiB RAM. After the above tuning:
- Queue throughput rose from 30 jobs/min to 220 jobs/min.
- CPU usage dropped from 85 % to 30 % during peak load.
- FPM “502 Bad Gateway” errors vanished.
Before vs After Results
| Metric | Before | After |
|---|---|---|
| Avg. queue job time | 12 s (crashed) | 0.8 s |
| FPM memory per child | 128 MiB | 256‑512 MiB |
| Error log entries/hour | 48 | 0 |
Security Considerations
When you raise memory_limit and pm.max_children, you also increase the attack surface if a rogue script consumes all workers. To mitigate:
- Enable
disable_functionsforexec, shell_exec, systeminphp.ini. - Run workers under a dedicated, low‑privilege Linux user.
- Set
process_control_timeout=5in the FPM pool to avoid zombie processes. - Use Cloudflare’s “Rate Limiting” rule for API endpoints.
Bonus Performance Tips
- Cache heavy queries. Use
Cache::remember()with Redis tags. - Enable HTTP/2. Both Apache (mod_http2) and Nginx support it out of the box.
- Use OpCache JIT. Add
opcache.jit=1255tophp.inifor PHP 8.2. - Compress assets. Serve gzipped CSS/JS via
gzip_static on;(Nginx) orSetOutputFilter DEFLATE(Apache). - Schedule queue restarts. Add a cron job:
0 * * * * php /home/yourcpaneluser/example.com/artisan queue:restart.
FAQ Section
Q: My host doesn’t allow editing php‑fpm pools. What can I do?
A: Switch to a cheap VPS or a Laravel‑optimized “cloud” plan. Most providers let you edit the pool file; otherwise use .user.ini to raise memory_limit and rely on a “queue:work --daemon” process managed by a cron.
Q: Do I need Redis if I’m on shared hosting?
A: Not strictly, but without a fast queue backend Laravel will fall back to the database, increasing MySQL load dramatically. Many shared hosts offer a free Redis instance – enable it in .env.
Q: How many Supervisor processes are safe?
A: Start with numprocs=2 and monitor top. Increase gradually until CPU stays below 70 % during peak traffic.
Q: Will these changes affect my WordPress sites on the same server?
A: No. The FPM pool is scoped per domain. Just make sure the total pm.max_children across all pools stays within the host’s limit.
Final Thoughts
Laravel queue workers dying after ten seconds on a cPanel shared host is almost always a mis‑configured PHP‑FPM pool and an under‑powered Supervisor setup. By raising pm.max_children, bumping the memory limit, optimizing Composer, and ensuring Redis connectivity, you can turn a crashing sandbox into a production‑grade worker fleet in under ten minutes. The same principles apply when you migrate to a VPS – just keep an eye on RAM, CPU, and OpCache settings.
Take the time to benchmark after every change; the numbers speak louder than any blog post. Happy coding, and may your queues never die again.
No comments:
Post a Comment