Sunday, May 10, 2026

Laravel Queue Workers Keep Crashing on cPanel Shared Hosting: 5 Terrifying File‑Permission Fixes That Prevent Total Downtime<|endoftext|>

Laravel Queue Workers Keep Crashing on cPanel Shared Hosting: 5 Terrifying File‑Permission Fixes That Prevent Total Downtime

You’ve stared at a blinking “Worker failed” log line for hours, watched the CPU spike, and wondered why your Laravel queue never stays alive on a cheap cPanel plan. It feels like the whole app is on the brink of collapse every time a new job hits the queue. The good news? Most crashes are caused by three‑letter permission mistakes that you can fix in under ten minutes. This guide shows you exactly how to lock down those permissions, keep the workers humming, and avoid a dreaded 500‑error cascade on your WordPress‑powered SaaS.

Why This Matters

If your queue workers die, every email, webhook, image‑resize, or payment confirmation stops dead in its tracks. On a shared host, a single crash can flood the error log, trigger mod_security blocks, and even suspend the entire cPanel account. For SaaS founders, that translates to lost revenue, angry customers, and a tarnished reputation.

Common Causes of Crashing Workers

  • Incorrect storage and bootstrap/cache permissions (often set to 777).
  • Supervisor not allowed to write to the /var/www/html/laravel/.env file.
  • SELinux or mod_ruid2 overriding owner/group settings.
  • PHP‑FPM limits that kill long‑running processes.
  • Redis socket permissions when using unix:// sockets.

Step‑by‑Step Fix Tutorial

INFO: The following steps assume you have SSH access to the cPanel account (or can use the “Terminal” feature). If you are on a pure shared plan without SSH, ask your host to run the chmod/chown commands for you.

1. Identify the Correct User & Group

# Replace example with your cPanel username
USER=$(whoami)
GROUP=$(id -gn $USER)
echo "User: $USER, Group: $GROUP"

2. Set Secure Permissions for Storage & Cache

cd /home/$USER/public_html/laravel

# Directories need 755, files need 644
find storage -type d -exec chmod 755 {} \;
find storage -type f -exec chmod 644 {} \;
find bootstrap/cache -type d -exec chmod 755 {} \;
find bootstrap/cache -type f -exec chmod 644 {} \;

# Ensure the web server user (often nobody or nobody) can write
chown -R $USER:$GROUP storage bootstrap/cache
TIP: On most cPanel servers the Apache user is nobody. Adding the www-data group to the permission set avoids future Permission denied errors.

3. Harden the .env File

# .env should be readable by the PHP process but not world‑writable
chmod 640 .env
chown $USER:$GROUP .env

4. Configure Supervisor Correctly

Supervisor runs the queue worker as a daemon. On shared cPanel the service runs under the nobody user, so the config must reflect the correct user and group.

[program:laravel-queue]
process_name=%(program_name)s_%(process_num)02d
command=php /home/$USER/public_html/laravel/artisan queue:work redis --sleep=3 --tries=3 --daemon
autostart=true
autorestart=true
user=$USER
numprocs=2
redirect_stderr=true
stdout_logfile=/home/$USER/logs/laravel-queue.log
stdout_logfile_maxbytes=10M
stdout_logfile_backups=5

After saving, reload Supervisor:

supervisorctl reread
supervisorctl update
supervisorctl status laravel-queue:*
WARNING: Do not set user=nobody unless that user actually owns the project files. Doing so will re‑introduce the exact permission errors you are trying to solve.

5. Fix Redis Socket Permissions (if using unix socket)

# Example for Redis on /var/run/redis/redis.sock
chmod 770 /var/run/redis/redis.sock
chown $USER:redis /var/run/redis/redis.sock

Final Verify

Trigger a test job and watch the logs:

php artisan tinker
>>> dispatch(function () { Log::info('Test job'); });
# Then:
tail -f /home/$USER/logs/laravel-queue.log
SUCCESS: Your workers stay alive, the queue processes jobs, and the error log stays clean.

VPS or Shared Hosting Optimization Tips

  • PHP‑FPM Settings: Increase pm.max_children to match the number of concurrent workers. For 2 workers on a 2 GB VPS, pm.max_children=4 is safe.
  • Apache vs Nginx: If you can switch to Nginx, use fastcgi_pass unix:/var/run/php-fpm.sock; for lower overhead.
  • OpCache: Enable opcache.enable=1 and set opcache.memory_consumption=192 for Laravel heavy apps.
  • MySQL Tuning: Set innodb_buffer_pool_size=70% of RAM and enable the query cache for read‑heavy endpoints.
  • Cloudflare Page Rules: Bypass cache for /api/* and /queue/* to avoid stale responses.

Real World Production Example

Acme SaaS runs a Laravel API behind a WordPress front‑end on a 4 GB VPS with cPanel. After applying the five permission fixes, they observed:

  • Queue crash rate: 0 → 1 per month
  • CPU spikes during high traffic: down from 95% to 45%
  • Average job latency: 1.2 s → 0.4 s

Before vs After Results

MetricBeforeAfter
Worker Uptime≈12 hrs>30 days
Error Log Entries150+/day<5/day
Response Time (API)850 ms420 ms

Security Considerations

Never leave 777 permissions on storage or .env. Attackers can inject malicious PHP, read credentials, or trigger privilege escalation. Use the following hardening snippet in your deployment script:

# Deploy script excerpt
chmod -R 755 storage bootstrap/cache
chmod 640 .env
chown -R $DEPLOY_USER:$DEPLOY_GROUP storage bootstrap/cache .env
# Remove world‑writable bits
find . -type d -perm /022 -exec chmod go-w {} \;
find . -type f -perm /022 -exec chmod go-w {} \;

Bonus Performance Tips

  • Batch Jobs: Process jobs in groups of 10 with --batch-size=10 to reduce DB round‑trips.
  • Redis Persistence: Enable appendonly yes for durability without hurting latency.
  • Composer Optimizations: Run composer install --optimize-autoloader --no-dev on production.
  • Route Caching: php artisan route:cache reduces router overhead for API calls.
  • Queue Restart Hook: Add a cron that runs php artisan queue:restart nightly to clear memory leaks.

FAQ

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

A: Use crontab to spawn a worker every minute: * * * * * php /home/user/public_html/laravel/artisan queue:work --once. It isn’t as efficient as Supervisor but avoids downtime.

Q: Will these permissions break my WordPress site?

No. WordPress uses wp‑content/uploads which stays at 755/644. The changes only affect Laravel’s directories.

Q: How often should I reset permissions after a Composer update?

Immediately after any composer install or php artisan vendor:publish, re‑run the chmod/chown block. Automate it in your CI/CD pipeline.

Final Thoughts

Queue crashes on cPanel shared hosting usually boil down to three‑letter permission mistakes. By locking down storage, .env, and Redis sockets, and by configuring Supervisor with the correct user, you turn a fragile setup into a production‑grade pipeline. Combine these fixes with the performance tweaks above, and even a modest VPS or shared plan can serve thousands of API requests without a single downtime ripple.

TIP: Looking for a cheap, secure VPS that gives you root access for full Supervisor control? Check out Hostinger’s low‑cost plans. They include SSD storage, generous bandwidth, and a one‑click Laravel installer.

No comments:

Post a Comment