# LEMP Stack (Linux, Nginx, MySQL, PHP)

> Classic LEMP stack for PHP applications.

- Category: Full Web Stacks
- Difficulty: intermediate
- Components: nginx, php-fpm, mysql, phpmyadmin
- Tags: lemp, nginx, php, mysql
- Source: https://docker.recipes/full-stacks/nginx-php-mysql-stack

## Overview

NGINX is a high-performance HTTP server and reverse proxy that revolutionized web serving with its event-driven, asynchronous architecture. Originally developed by Igor Sysoev to solve the C10K problem, NGINX excels at handling thousands of concurrent connections with minimal resource consumption, making it the preferred choice for high-traffic websites and modern web applications.

This LEMP stack combines NGINX's superior performance characteristics with PHP-FPM's process management capabilities, MySQL's proven reliability for data storage, and phpMyAdmin's intuitive web-based administration interface. The stack leverages FastCGI Process Manager (PHP-FPM) to handle PHP requests efficiently, while NGINX serves static content directly and proxies dynamic requests to the PHP processor. MySQL 8.0 provides robust data persistence with InnoDB's ACID compliance and advanced features like JSON data types and full-text search.

Web developers, freelancers, and hosting providers will find this configuration particularly valuable for WordPress sites, custom PHP applications, and multi-tenant hosting environments. The combination delivers exceptional performance for read-heavy workloads while maintaining the simplicity that made LAMP stacks popular, but with NGINX's superior handling of concurrent connections and static file serving capabilities.

## Features

- Event-driven NGINX architecture handling thousands of concurrent connections with minimal memory footprint
- PHP-FPM process management with optimized worker pools and graceful process recycling
- MySQL 8.0 InnoDB storage engine with ACID compliance and crash recovery
- FastCGI protocol integration between NGINX and PHP-FPM for efficient request processing
- phpMyAdmin web interface with SQL editor, import/export tools, and database visualization
- NGINX reverse proxy capabilities with built-in load balancing and health checks
- MySQL JSON data type support with native indexing and query optimization
- PHP OPcache integration for bytecode caching and improved script execution performance

## Use cases

- WordPress hosting environments requiring high concurrent user support
- Custom PHP web applications with complex database interactions
- E-commerce platforms built on PHP frameworks like Laravel or CodeIgniter
- Content management systems needing reliable database administration tools
- Development environments for PHP applications with MySQL backends
- Small to medium business websites requiring robust database-driven functionality
- Multi-tenant hosting platforms serving multiple PHP applications

## Prerequisites

- Minimum 1.5GB RAM (256MB for MySQL, 256MB for NGINX, 512MB for PHP-FPM, 256MB for phpMyAdmin, plus OS overhead)
- Docker Engine 20.10+ and Docker Compose V2 for proper service orchestration
- Available ports 80, 443 (NGINX) and 8080 (phpMyAdmin) on the host system
- Basic understanding of PHP application deployment and MySQL database concepts
- Familiarity with NGINX configuration syntax for customizing virtual hosts and proxy settings

## docker-compose.yml

```yaml
services:
  nginx:
    image: nginx:alpine
    container_name: lemp-nginx
    restart: unless-stopped
    ports:
      - "${NGINX_PORT:-80}:80"
      - "${NGINX_SSL_PORT:-443}:443"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
      - ${APP_PATH:-./app}:/var/www/html
    depends_on:
      - php

  php:
    image: php:8.2-fpm-alpine
    container_name: lemp-php
    restart: unless-stopped
    volumes:
      - ${APP_PATH:-./app}:/var/www/html

  mysql:
    image: mysql:8.0
    container_name: lemp-mysql
    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

  phpmyadmin:
    image: phpmyadmin:latest
    container_name: lemp-pma
    restart: unless-stopped
    ports:
      - "${PMA_PORT:-8080}:80"
    environment:
      - PMA_HOST=mysql
    depends_on:
      - mysql

volumes:
  mysql_data:
```

## .env

```bash
# LEMP Stack
NGINX_PORT=80
NGINX_SSL_PORT=443
APP_PATH=./app
DB_ROOT_PASSWORD=root_password
DB_NAME=app
DB_USER=app
DB_PASSWORD=app_password
PMA_PORT=8080
```

## Usage notes

- App at http://localhost
- phpMyAdmin at http://localhost:8080
- Place PHP files in ./app
- Configure nginx for your app

## Troubleshooting

- 502 Bad Gateway errors: Check PHP-FPM container status and ensure FastCGI configuration in NGINX points to correct PHP service name
- MySQL connection refused: Verify MySQL container is fully initialized before PHP attempts connections, add healthcheck or depends_on conditions
- phpMyAdmin 'Cannot connect: invalid settings' error: Confirm PMA_HOST environment variable matches MySQL service name in docker-compose
- PHP files downloading instead of executing: Verify NGINX configuration includes proper location blocks for .php files and FastCGI parameters
- Permission denied errors on app files: Ensure proper file ownership and permissions in mounted volume, PHP-FPM runs as www-data user
- MySQL 'Authentication plugin caching_sha2_password' errors: Use MySQL 8.0 compatible connection methods or configure mysql_native_password for legacy PHP applications

