Laravel Queue Worker Crashes on VPS: Why Docker‑Dated Composer Dependencies & Nginx File‑Permission 500 Errors Keep Your Site Down for Hours!
If you’ve ever watched a production queue grind to a halt, stared at a 500 error page for minutes, and felt the panic of an e‑commerce checkout disappearing, you know the pain. The worst part? The culprit is often something you “fixed” weeks ago—out‑of‑date Docker images, a rogue Composer lock, or a mis‑set file permission on Nginx. This article walks you through the exact steps to stop those crashes, bring your Laravel‑WordPress hybrid back to life, and turn a nightmare into a repeatable, billable deployment.
Why This Matters
Queue workers are the heartbeat of any modern Laravel app—processing emails, webhook calls, image thumbnails, and even WordPress cron jobs. When a worker dies, jobs pile up, database connections choke, and customers experience latency or complete site outages. On a VPS, a single 500 error can cascade into hours of lost revenue, especially when Cloudflare continues to cache the error page.
Common Causes
- Docker images built on an old PHP version (e.g., 7.4) while the host runs PHP 8.2.
- Composer dependencies locked to a deprecated Laravel version, causing autoloader mismatches.
- Nginx user/group permissions (www-data) that prevent the queue process from writing to
storageandbootstrap/cache. - Supervisor not reloading after a code deploy, leaving stale binary paths.
- Redis connection time‑outs after a sudden traffic spike.
Step‑By‑Step Fix Tutorial
1. Verify PHP & Composer Versions Inside Docker
# Inside your Dockerfile
FROM php:8.2-fpm-alpine
# Install extensions
RUN apk add --no-cache \
git \
unzip \
libpng-dev \
libzip-dev \
&& docker-php-ext-install pdo_mysql zip exif pcntl
# Ensure Composer is latest
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
2. Re‑build the Image & Push to Registry
docker build -t yourrepo/laravel-app:latest .
docker push yourrepo/laravel-app:latest
3. Pull Fresh Image on VPS & Run Composer Install
docker pull yourrepo/laravel-app:latest
docker run --rm -v $(pwd):/var/www/html yourrepo/laravel-app:latest composer install --optimize-autoloader --no-dev
4. Fix Nginx File Permissions
# Set correct ownership
sudo chown -R www-data:www-data /var/www/html/storage /var/www/html/bootstrap/cache
# Ensure directories are writable
sudo find /var/www/html/storage -type d -exec chmod 775 {} \;
sudo find /var/www/html/bootstrap/cache -type d -exec chmod 775 {} \;
5. Configure Supervisor for Laravel Queue
[program:laravel-queue]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/html/artisan queue:work redis --sleep=3 --tries=3 --timeout=60
autostart=true
autorestart=true
user=www-data
numprocs=3
redirect_stderr=true
stdout_logfile=/var/log/laravel-queue.log
6. Reload Supervisor & Verify
sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl status laravel-queue:*
7. Test Queue locally
php artisan queue:retry all
php artisan queue:work --once
VPS or Shared Hosting Optimization Tips
- PHP‑FPM Pools: Create a dedicated pool for Laravel with
pm.max_children=20andpm.start_servers=4on a 4‑core VPS. - MySQL Tuning: Set
innodb_buffer_pool_size=70%of RAM, enablequery_cache_type=ONfor read‑heavy WordPress sites. - Redis Persistence: Use
appendonly yesandmaxmemory 256mbto avoid out‑of‑memory kills. - Cloudflare Cache‑Everything: Bypass for
/api/*and/queue/*to keep API latency low. - Swap File (Only on Small VPS):
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048 && sudo mkswap /swapfile && sudo swapon /swapfile
Real World Production Example
Acme SaaS runs a Laravel‑based billing micro‑service behind an Nginx reverse proxy on a 2 vCPU Ubuntu 22.04 VPS. After a Docker upgrade, the queue workers started failing with “Class 'App\Jobs\SendInvoice' not found”. The fix was to:
- Re‑build the Docker image with PHP 8.2.
- Run
composer dump‑autoload -oinside the container. - Reset Supervisor.
- Set
chmod 775 storagefor thewww-datauser.
Result: zero 500s for a full 30‑day period, 25 % faster invoice processing, and a 15 % reduction in server CPU usage.
Before vs After Results
| Metric | Before | After |
|---|---|---|
| Queue Failure Rate | 12 % | 0 % |
| CPU Avg (core) | 85 % | 62 % |
| Avg Job Latency | 4.8 s | 2.1 s |
Security Considerations
When you tighten permissions, remember:
- Never run queue workers as
root. Usewww-dataor a dedicatedqueueuser. - Set
open_basedirin PHP‑FPM to limit file system access to/var/www/html. - Enable
process_control_timeoutinphp.inito avoid abrupt kills. - Store Redis passwords in
.envand useREDIS_PASSWORDin the queue config.
chmod 777 storage may solve a permission error temporarily, but it opens a massive attack surface for remote code execution.Bonus Performance Tips
- Enable
opcache.validate_timestamps=0in production. - Use
php artisan horizonfor a visual queue dashboard and auto‑scaling. - Set
fastcgi_buffers 16 16k;andfastcgi_buffer_size 32k;in Nginx. - Compress JSON API responses with
gzip on;andgzip_types application/json;. - Leverage WordPress’
object-cache.phpRedis drop‑in to share the same Redis instance.
FAQ
Q: My queue still restarts after Supervisor reload?
A: Check the /var/log/laravel-queue.log for uncaught exceptions. Most often it’s a missing .env variable that only exists on the host, not inside Docker.
Q: Do I need a separate Redis instance for Laravel and WordPress?
No. Use unique Redis DB indexes (e.g., 0 for WordPress, 1 for Laravel) and set REDIS_DB accordingly.
Q: Can I run the queue on a shared hosting environment?
Only if the host provides SSH access, a supported PHP version, and allows you to run php artisan queue:work as a background process. Otherwise consider a cheap VPS.
Final Thoughts
Docker images, Composer, and Nginx permissions are three sides of the same coin. Treat them as a single deployment pipeline, automate the rebuild, and lock down file ownership. When you do, the dreaded 500 error disappears, queue workers stay alive, and your Laravel‑WordPress combo can handle traffic spikes without breaking a sweat.
Looking for Cheap, Secure Hosting?
If you need a reliable VPS or shared hosting plan that plays nicely with Docker, Composer, and Nginx, check out Hostinger's affordable hosting. Their 24/7 support and SSD‑backed servers are a solid foundation for any PHP‑Laravel‑WordPress stack.
No comments:
Post a Comment