# Laravel Production Stack

> Production Laravel with Octane, Horizon, MySQL, and Redis.

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

## Overview

Laravel is a modern PHP web framework that revolutionized web development with its elegant syntax and rich ecosystem. Built around the MVC pattern, Laravel provides powerful features like Eloquent ORM, Blade templating, and robust routing out of the box. Originally created by Taylor Otwell in 2011, Laravel has become the most popular PHP framework, powering millions of applications worldwide from startups to enterprise systems.

This production stack combines Laravel with Octane for dramatically improved performance, Horizon for queue management and monitoring, MySQL as the primary database, Redis for caching and sessions, and NGINX for high-performance web serving. Octane uses Swoole to keep Laravel applications in memory between requests, eliminating the traditional PHP bootstrap overhead and delivering response times that rival Node.js applications. Horizon provides a beautiful dashboard and code-driven configuration for Redis queues, while the entire stack is orchestrated for maximum throughput and reliability.

This configuration targets production environments where performance and scalability are critical. Enterprises running high-traffic Laravel applications, SaaS platforms with demanding performance requirements, and API-heavy applications will benefit most from this setup. The combination of Octane's memory-resident application server, Redis-backed caching and queuing, and NGINX's efficient request handling creates a Laravel deployment capable of handling thousands of concurrent requests while maintaining sub-100ms response times.

## Features

- Laravel Octane with Swoole server for persistent application instances and sub-50ms response times
- Laravel Horizon dashboard for Redis queue monitoring, failed job management, and throughput metrics
- MySQL 8.0 with InnoDB storage engine providing ACID compliance and JSON column support
- Redis 7 Alpine for ultra-fast session storage, application caching, and queue backend
- NGINX Alpine reverse proxy with SSL termination and static asset serving
- Automated Laravel scheduler container running artisan commands every minute
- Production-optimized environment variables for database connections and cache drivers
- Isolated service networking with proper dependency management and health checks

## Use cases

- High-traffic Laravel SaaS applications requiring sub-100ms API response times
- E-commerce platforms with heavy database operations and real-time inventory updates
- Content management systems serving thousands of concurrent users
- API backends for mobile applications with strict performance requirements
- Multi-tenant Laravel applications with complex queue processing workflows
- Enterprise web applications requiring 99.9% uptime and horizontal scalability
- Laravel applications with heavy background job processing and scheduled tasks

## Prerequisites

- Minimum 4GB RAM (2GB for MySQL, 1GB for Redis, 1GB for Laravel containers)
- Docker Engine 20.10+ and Docker Compose v2 for multi-container orchestration
- Generated Laravel APP_KEY and secure MySQL passwords in .env file
- SSL certificates for NGINX HTTPS configuration (Let's Encrypt recommended)
- Understanding of Laravel Octane limitations and stateful application considerations
- Experience with Laravel queue workers and Horizon configuration

## docker-compose.yml

```yaml
services:
  mysql:
    image: mysql:8.0
    environment:
      - MYSQL_DATABASE=laravel
      - MYSQL_USER=laravel
      - MYSQL_PASSWORD=${MYSQL_PASSWORD}
      - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
    volumes:
      - mysql_data:/var/lib/mysql
    networks:
      - laravel_net

  redis:
    image: redis:7-alpine
    volumes:
      - redis_data:/data
    networks:
      - laravel_net

  laravel:
    build:
      context: .
      dockerfile: Dockerfile
    command: php artisan octane:start --server=swoole --host=0.0.0.0 --port=8000
    environment:
      - APP_ENV=production
      - APP_KEY=${APP_KEY}
      - DB_CONNECTION=mysql
      - DB_HOST=mysql
      - DB_DATABASE=laravel
      - DB_USERNAME=laravel
      - DB_PASSWORD=${MYSQL_PASSWORD}
      - REDIS_HOST=redis
      - CACHE_DRIVER=redis
      - QUEUE_CONNECTION=redis
      - SESSION_DRIVER=redis
    volumes:
      - laravel_storage:/app/storage
    depends_on:
      - mysql
      - redis
    networks:
      - laravel_net

  horizon:
    build:
      context: .
      dockerfile: Dockerfile
    command: php artisan horizon
    environment:
      - APP_ENV=production
      - APP_KEY=${APP_KEY}
      - DB_CONNECTION=mysql
      - DB_HOST=mysql
      - DB_DATABASE=laravel
      - DB_USERNAME=laravel
      - DB_PASSWORD=${MYSQL_PASSWORD}
      - REDIS_HOST=redis
    depends_on:
      - mysql
      - redis
    networks:
      - laravel_net

  scheduler:
    build:
      context: .
      dockerfile: Dockerfile
    command: sh -c "while true; do php artisan schedule:run; sleep 60; done"
    environment:
      - APP_ENV=production
      - APP_KEY=${APP_KEY}
      - DB_CONNECTION=mysql
      - DB_HOST=mysql
      - DB_DATABASE=laravel
      - DB_USERNAME=laravel
      - DB_PASSWORD=${MYSQL_PASSWORD}
      - REDIS_HOST=redis
    depends_on:
      - mysql
      - redis
    networks:
      - laravel_net

  nginx:
    image: nginx:alpine
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
    depends_on:
      - laravel
    networks:
      - laravel_net

volumes:
  mysql_data:
  redis_data:
  laravel_storage:

networks:
  laravel_net:
```

## .env

```bash
# Laravel Production
MYSQL_PASSWORD=secure_mysql_password
MYSQL_ROOT_PASSWORD=secure_root_password
APP_KEY=base64:your_app_key_here

# Laravel at http://localhost
# Horizon dashboard at /horizon
```

## Usage notes

- Laravel at http://localhost
- Horizon dashboard at /horizon
- Octane with Swoole for performance
- Run migrations before start
- Configure APP_KEY properly

## Troubleshooting

- Octane memory leaks with static variables: Restart Octane workers periodically using OCTANE_WATCH=true
- Horizon workers not processing jobs: Check Redis connection and run 'php artisan horizon:terminate' to restart
- MySQL connection refused errors: Verify MYSQL_PASSWORD environment variable matches across all Laravel services
- NGINX 502 Bad Gateway: Ensure Laravel Octane is binding to 0.0.0.0:8000, not localhost
- Laravel sessions not persisting: Confirm SESSION_DRIVER=redis and REDIS_HOST=redis in environment
- Scheduler not executing: Check that cron jobs are running every minute and APP_KEY is properly set

