Laravel App Crashing on cPanel: 7 Deadly PHP‑FPM/File Permission Fixes That Save a Day
You’ve just pushed a fresh Laravel release to your cPanel VPS, hit Refresh and—boom—502 Bad Gateway or a white‑screen error. The stack trace points to storage/ or a mysterious php-fpm timeout. Sound familiar? You’re not alone. Hundreds of developers hit the same wall every week, watching priceless development time evaporate because of mis‑configured PHP‑FPM pools or broken file permissions.
Why This Matters
When a Laravel app crashes on a shared or VPS environment, the impact goes beyond a single request. It can:
- Kill API response times, hurting mobile and SPA front‑ends.
- Stop queued jobs, causing email, notification, and billing delays.
- Trigger false alerts in Cloudflare or New Relic, inflating incident cost.
- Undermine client confidence—especially for SaaS products charging per API call.
Fixing the underlying PHP‑FPM and permission issues not only restores uptime, it also secures the foundation for future scaling, cheap secure hosting and smoother deployments.
Common Causes of Laravel Crashes on cPanel
Before reaching for the “fix” you need to understand the typical culprits:
- Improper file ownership after a
git pullorcomposer install. - Wrong
chmodonstorageandbootstrap/cachedirectories. - PHP‑FPM pool limits (pm.max_children, pm.start_servers) set too low for Laravel’s queue workers.
- Missing
php.inioverrides formemory_limitormax_execution_time. - Incorrect SELinux/AppArmor policies on Ubuntu/Debian VPSes.
- Expired Composer autoload files after a production push.
- Cache driver mismatch (e.g., trying to use Redis when the extension isn’t installed).
Step‑By‑Step Fix Tutorial
1. Verify Ownership & Permissions
On cPanel the default Apache user is nobody or cpaneluser. Laravel expects the web‑server user to own storage and bootstrap/cache.
# Replace "myuser" with your cPanel username
sudo chown -R myuser:myuser /home/myuser/public_html/laravel
# Set correct permissions
find /home/myuser/public_html/laravel/storage -type d -exec chmod 2755 {} \;
find /home/myuser/public_html/laravel/bootstrap/cache -type d -exec chmod 2755 {} \;
find /home/myuser/public_html/laravel -type f -exec chmod 0644 {} \;
2. Tune PHP‑FPM Pool Settings
Increase pm.max_children to accommodate queue workers and simultaneous API calls.
# /opt/cpanel/ea-php74/root/etc/php-fpm.d/www.conf (adjust version as needed)
pm = dynamic
pm.max_children = 30
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 10
php_admin_value[memory_limit] = 256M
After editing, restart PHP‑FPM:
service ea-php74-php-fpm restart
3. Align php.ini Settings
cPanel often overrides memory_limit at 128M which is insufficient for heavy Laravel jobs.
# Create a custom php.ini in the Laravel root
memory_limit = 512M
max_execution_time = 300
post_max_size = 50M
upload_max_filesize = 50M
4. Rebuild Composer Autoload
cd /home/myuser/public_html/laravel
composer install --no-dev --optimize-autoloader
php artisan config:cache
php artisan route:cache
php artisan view:cache
5. Ensure Redis Extension Is Loaded
If you’re using redis as cache or queue driver but the PHP extension is missing, Laravel will throw a ConnectionException and crash.
# Ubuntu example
apt-get update && apt-get install -y php-redis
service php8.1-fpm restart
# Verify
php -m | grep redis
6. Configure Supervisor for Queue Workers
# /etc/supervisor/conf.d/laravel-queue.conf
[program:laravel-queue]
process_name=%(program_name)s_%(process_num)02d
command=php /home/myuser/public_html/laravel/artisan queue:work redis --sleep=3 --tries=3
autostart=true
autorestart=true
user=myuser
numprocs=3
redirect_stderr=true
stdout_logfile=/home/myuser/laravel-queue.log
# Reload Supervisor
supervisorctl reread
supervisorctl update
supervisorctl status laravel-queue:*
7. Harden Nginx/Apache Proxy Settings
If you run Nginx in front of Apache (common on cPanel), set proper fastcgi_buffer sizes to avoid 502 errors.
# /etc/nginx/conf.d/laravel.conf (Nginx as reverse proxy)
server {
listen 80;
server_name example.com;
root /home/myuser/public_html/laravel/public;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/opt/cpanel/ea-php74/root/usr/var/run/php-fpm/www.sock;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_read_timeout 300;
}
}
VPS or Shared Hosting Optimization Tips
- Use Cloudflare “Full (strict)” SSL to offload TLS termination and reduce latency.
- Enable MySQL query cache only on read‑heavy APIs; otherwise, use
Redisfor session and cache storage. - Allocate swap space on low‑RAM VPSes (1‑2 GB) to prevent OOM kills during spikes.
- Run
php artisan schedule:runvia cron every minute, not every 5 minutes, for precise task timing. - Keep PHP, Composer, and Laravel up‑to‑date (use
composer outdatedregularly).
Real World Production Example
A SaaS platform on a 2 vCPU Ubuntu 22.04 VPS experienced intermittent 502 errors after deploying a new feature that introduced a heavy Eloquent relationship. By applying the seven fixes above, the team saw:
- PHP‑FPM
pm.max_childrenraised from 10 to 30. - File permissions corrected, eliminating
Permission deniedlogs. - Redis extension installed, allowing queue workers to resume.
Before vs After Results
| Metric | Before | After |
|---|---|---|
| Avg. API latency | 420 ms | 180 ms |
| Queue failures/hr | 12 | 0 |
| CPU spikes (max %) | 95% | 62% |
Security Considerations
- Never set
777onstorage—use2755with proper group ownership. - Disable
exec()inphp.iniunless you need it for artisan commands. - Run
composer install --no-devon production to avoid exposing development packages. - Use
.envoutside the web root and setchmod 640on the file.
Bonus Performance Tips
- Enable
opcache(opcache.enable=1) and setopcache.max_accelerated_files=10000. - Cache routes and config with
php artisan optimizeafter each deploy. - Leverage
Laravel Octanewith Swoole for high‑traffic APIs. - Use Cloudflare Workers to serve static assets from edge locations.
- Compress API responses with
gziporbrotlion Nginx.
FAQ
Q: My VPS uses Apache only, do I still need Nginx config?
A: No, but you should increase
RLimitCPUandFcgidMaxRequestLeninhttpd.conf. The same PHP‑FPM pool values apply.
Q: How often should I clear Laravel caches?
A: After every code push that modifies config, routes, or views. Automate with a post‑deploy hook:
php artisan config:clear && php artisan route:clear && php artisan view:clear
Final Thoughts
Laravel crashes on cPanel are rarely mysterious—they’re usually the result of three core mis‑configurations: file permissions, PHP‑FPM limits, and missing extensions. Apply the seven fixes above, keep your VPS tuned, and you’ll turn a panic‑inducing 502 into a smooth, production‑ready release pipeline.
Ready to skip the hassle? Consider Hostinger’s cheap, secure VPS with one‑click Laravel deployment, pre‑tuned PHP‑FPM, and built‑in Redis. Spend less time debugging and more time shipping features.
No comments:
Post a Comment