# Laravel + MySQL

> PHP Laravel framework with MySQL and Redis.

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

## Overview

Laravel is an elegant PHP web framework that follows the Model-View-Controller (MVC) architectural pattern, created by Taylor Otwell in 2011. Built on the Symfony framework components, Laravel emphasizes developer productivity through expressive syntax, built-in features like Eloquent ORM, Blade templating engine, and Artisan command-line tool. It has become one of the most popular PHP frameworks due to its clean architecture, comprehensive documentation, and vibrant ecosystem of packages through Composer and Laravel-specific tools like Nova and Forge.

This stack combines Laravel's rapid development capabilities with MySQL's proven relational database performance and Redis's lightning-fast caching and session storage. NGINX serves as the high-performance web server handling static assets and reverse proxying to PHP-FPM, while MySQL manages persistent data with ACID compliance and complex relationships that Laravel's Eloquent ORM excels at handling. Redis accelerates the application by caching database queries, storing user sessions, and managing Laravel's queue system for background job processing.

This configuration targets web development teams building content management systems, e-commerce platforms, SaaS applications, and API backends that require robust data relationships, user authentication, and high-performance caching. Laravel developers working on applications with complex business logic, multi-user systems, or projects requiring rapid prototyping will benefit from this stack's balance of development speed and production reliability.

## Features

- Laravel Eloquent ORM with MySQL for complex database relationships and migrations
- Redis-powered session storage and cache driver for sub-millisecond response times
- NGINX FastCGI integration with PHP-FPM for optimal Laravel application performance
- Laravel Queue system backed by Redis for asynchronous job processing
- MySQL InnoDB storage engine with full ACID compliance for data integrity
- Laravel Artisan command-line tool integration for migrations and maintenance
- Redis Pub/Sub messaging for real-time Laravel broadcasting and WebSocket support
- NGINX static asset serving with Laravel Mix optimization support

## Use cases

- E-commerce platforms requiring complex product catalogs, inventory management, and order processing
- Content management systems with hierarchical data structures and media libraries
- SaaS applications needing multi-tenant architecture with user authentication and authorization
- RESTful API backends for mobile applications with token-based authentication
- Educational platforms with course management, user progress tracking, and payment integration
- Real-time applications using Laravel Echo with Redis for chat systems or live updates
- Enterprise web applications requiring role-based permissions and audit logging

## Prerequisites

- Minimum 2GB RAM (1GB for MySQL, 512MB for Redis, 512MB for Laravel/NGINX)
- PHP and Laravel framework knowledge including Eloquent ORM and Blade templating
- MySQL database design experience with understanding of indexes and relationships
- Port 80 available for web traffic and familiarity with NGINX configuration
- Composer dependency manager knowledge for Laravel package management
- Basic understanding of Redis data structures for caching and session implementation

## docker-compose.yml

```yaml
services:
  app:
    build: .
    container_name: laravel
    volumes:
      - .:/var/www/html
    networks:
      - laravel

  nginx:
    image: nginx:alpine
    container_name: nginx
    volumes:
      - .:/var/www/html:ro
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf:ro
    ports:
      - "80:80"
    depends_on:
      - app
    networks:
      - laravel

  mysql:
    image: mysql:8.0
    container_name: mysql
    environment:
      MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
      MYSQL_DATABASE: ${MYSQL_DATABASE}
      MYSQL_USER: ${MYSQL_USER}
      MYSQL_PASSWORD: ${MYSQL_PASSWORD}
    volumes:
      - mysql_data:/var/lib/mysql
    networks:
      - laravel

  redis:
    image: redis:alpine
    container_name: redis
    networks:
      - laravel

volumes:
  mysql_data:

networks:
  laravel:
    driver: bridge
```

## .env

```bash
MYSQL_ROOT_PASSWORD=rootpassword
MYSQL_DATABASE=laravel
MYSQL_USER=laravel
MYSQL_PASSWORD=changeme
```

## Usage notes

- Docs: https://laravel.com/docs
- Create Dockerfile with PHP-FPM + composer
- Run migrations: docker compose exec app php artisan migrate
- Access at http://localhost
- Generate app key: docker compose exec app php artisan key:generate
- Queue worker: docker compose exec app php artisan queue:work

## Troubleshooting

- Laravel key not set error: Run 'docker compose exec app php artisan key:generate' to generate application encryption key
- MySQL connection refused: Verify MYSQL_DATABASE, MYSQL_USER, and MYSQL_PASSWORD environment variables match Laravel's .env configuration
- Redis connection timeout: Ensure Redis container is running and Laravel REDIS_HOST is set to 'redis' (container name)
- NGINX 502 Bad Gateway: Check that PHP-FPM is running in the Laravel container and NGINX upstream configuration points to correct FastCGI socket
- Laravel storage permissions error: Run 'docker compose exec app chown -R www-data:www-data storage bootstrap/cache' to fix file permissions
- MySQL data persistence lost: Verify mysql_data volume is properly mounted and not being recreated between container restarts

