# Production Laravel Stack

> Production Laravel with Nginx, PHP-FPM, MySQL, Redis, and Laravel Horizon for queues

- Category: Full Web Stacks
- Difficulty: advanced
- Components: nginx, laravel, php-fpm, mysql, redis, horizon
- Tags: laravel, php, production, horizon, nginx, mysql, redis
- Source: https://docker.recipes/full-stacks/production-laravel-stack

## Overview

Laravel is a PHP web application framework that follows the Model-View-Controller (MVC) architectural pattern, known for its elegant syntax, built-in ORM (Eloquent), and comprehensive tooling ecosystem. This production-ready stack combines Laravel with PHP-FPM for efficient PHP process management, NGINX as a high-performance reverse proxy and static file server, MySQL for reliable relational data storage, Redis for caching and session management, and Laravel Horizon for sophisticated queue monitoring and management. The architecture separates concerns effectively: NGINX handles HTTP requests and serves static assets while proxying dynamic requests to PHP-FPM, MySQL manages persistent data with ACID compliance, Redis provides sub-millisecond response times for caching and session storage, and Horizon offers real-time queue monitoring with automatic worker scaling. This combination is ideal for businesses running Laravel applications that require high availability, scalability, and professional queue management, particularly e-commerce platforms, SaaS applications, and content management systems that need robust background job processing and real-time features.

## Features

- Laravel Horizon dashboard for real-time queue monitoring with automatic worker balancing
- PHP-FPM process manager with optimized memory usage and request handling
- NGINX event-driven architecture serving static assets and proxying dynamic requests
- Redis-based session storage and caching with sub-millisecond response times
- MySQL InnoDB storage engine with ACID compliance and transaction support
- Laravel scheduler container running artisan commands via cron-like functionality
- Separated application, queue worker, and scheduler containers for scalability
- Production-optimized Laravel configuration with opcache and Redis drivers

## Use cases

- E-commerce platforms requiring background order processing and inventory management
- SaaS applications with user authentication, subscription billing, and email notifications
- Content management systems with media processing and scheduled publishing
- API-driven applications serving mobile apps and third-party integrations
- Multi-tenant applications requiring session isolation and background tenant provisioning
- Educational platforms with automated course enrollment and progress tracking
- Marketing platforms processing email campaigns and analytics data

## Prerequisites

- Docker Engine 20.0+ and Docker Compose 2.0+ installed on the host system
- Minimum 2GB RAM available (1GB for MySQL, 512MB for Redis, 512MB for Laravel containers)
- Port 80 available for NGINX web server (configurable via NGINX_PORT environment variable)
- Generated Laravel APP_KEY using 'php artisan key:generate --show' command
- Basic understanding of Laravel framework, Eloquent ORM, and queue job concepts
- Familiarity with Redis data structures and MySQL database administration

## docker-compose.yml

```yaml
services:
  nginx:
    image: nginx:alpine
    container_name: laravel-nginx
    restart: unless-stopped
    ports:
      - "${NGINX_PORT:-80}:80"
    volumes:
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf:ro
      - ./:/var/www/html
    depends_on:
      - app

  app:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: laravel-app
    restart: unless-stopped
    volumes:
      - ./:/var/www/html
    environment:
      - APP_ENV=production
      - APP_KEY=${APP_KEY}
      - APP_DEBUG=false
      - DB_CONNECTION=mysql
      - DB_HOST=db
      - DB_PORT=3306
      - DB_DATABASE=${DB_NAME}
      - DB_USERNAME=${DB_USER}
      - DB_PASSWORD=${DB_PASSWORD}
      - REDIS_HOST=redis
      - CACHE_DRIVER=redis
      - SESSION_DRIVER=redis
      - QUEUE_CONNECTION=redis
    depends_on:
      - db
      - redis

  horizon:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: laravel-horizon
    restart: unless-stopped
    command: php artisan horizon
    volumes:
      - ./:/var/www/html
    environment:
      - APP_ENV=production
      - APP_KEY=${APP_KEY}
      - DB_CONNECTION=mysql
      - DB_HOST=db
      - DB_DATABASE=${DB_NAME}
      - DB_USERNAME=${DB_USER}
      - DB_PASSWORD=${DB_PASSWORD}
      - REDIS_HOST=redis
      - QUEUE_CONNECTION=redis
    depends_on:
      - db
      - redis

  scheduler:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: laravel-scheduler
    restart: unless-stopped
    command: sh -c "while true; do php artisan schedule:run; sleep 60; done"
    volumes:
      - ./:/var/www/html
    environment:
      - APP_ENV=production
      - APP_KEY=${APP_KEY}
      - DB_HOST=db
      - DB_DATABASE=${DB_NAME}
      - DB_USERNAME=${DB_USER}
      - DB_PASSWORD=${DB_PASSWORD}
      - REDIS_HOST=redis
    depends_on:
      - db
      - redis

  db:
    image: mysql:8.0
    container_name: laravel-db
    restart: unless-stopped
    environment:
      - MYSQL_ROOT_PASSWORD=${DB_ROOT_PASSWORD}
      - MYSQL_DATABASE=${DB_NAME}
      - MYSQL_USER=${DB_USER}
      - MYSQL_PASSWORD=${DB_PASSWORD}
    volumes:
      - mysql_data:/var/lib/mysql

  redis:
    image: redis:7-alpine
    container_name: laravel-redis
    restart: unless-stopped
    volumes:
      - redis_data:/data

volumes:
  mysql_data:
  redis_data:
```

## .env

```bash
# Laravel Production Stack
NGINX_PORT=80

# Laravel
APP_KEY=base64:your-app-key-here

# Database
DB_ROOT_PASSWORD=root_password
DB_NAME=laravel
DB_USER=laravel
DB_PASSWORD=laravel_password
```

## Usage notes

- Create Dockerfile with PHP-FPM and Laravel dependencies
- Generate APP_KEY: php artisan key:generate --show
- Run migrations: docker compose exec app php artisan migrate
- Horizon dashboard at /horizon (configure auth)
- Scheduler runs artisan schedule:run every minute
- Optimize: docker compose exec app php artisan optimize

## Troubleshooting

- Laravel Horizon shows 'Inactive' status: Ensure Redis connection is working and run 'php artisan horizon:terminate' then restart the horizon container
- MySQL connection refused errors: Verify DB_HOST=db matches the service name and database container is fully initialized before application startup
- PHP-FPM 502 Bad Gateway errors: Check PHP-FPM pool configuration and ensure sufficient worker processes are configured for your traffic load
- Redis connection timeout issues: Increase Redis timeout settings in config/database.php and verify REDIS_HOST environment variable matches service name
- Queue jobs not processing: Confirm QUEUE_CONNECTION=redis is set and Horizon workers are active in the dashboard at /horizon
- Laravel scheduler not running: Verify the scheduler container is running and check Laravel's app/Console/Kernel.php for scheduled task definitions

