Home DockerHow to Set Up a LEMP Stack with Docker Compose

How to Set Up a LEMP Stack with Docker Compose

by Knowledge Base Linux
How to Set Up a LEMP — How to Set Up a LEMP Stack with Docker Compose

This article covers How to Set Up a LEMP in detail. Installing Nginx, PHP and MySQL by hand on every new server is slow and error-prone. With a docker compose lemp stack, the entire environment is described in one YAML file and comes up with a single command — identical on your laptop, a staging VPS and production. In this guide you will build a complete LEMP stack (Linux, Nginx, MySQL, PHP-FPM) with Docker Compose, persistent storage and a test PHP page. Make sure Docker and the Compose plugin are installed first by following our Docker installation guide for Ubuntu 24.04.

Prerequisites – How to Set Up a LEMP

  • Ubuntu 22.04 or 24.04 server with Docker Engine and the Compose plugin
  • A non-root user with sudo privileges
  • Port 80 free on the host

Verify Compose is available:

docker compose version
Orchestrating services in a Docker Compose LEMP stack

Step 1: Create the Project Structure – How to Set Up a LEMP

mkdir -p ~/lemp/{src,nginx,mysql-data}
cd ~/lemp

The src folder holds your PHP code, nginx holds the virtual host configuration, and the database files will persist in a named volume we define in Compose.

Step 2: Configure Nginx for the Docker Compose LEMP Stack

Create nginx/default.conf:

server {
    listen 80;
    server_name _;
    root /var/www/html;
    index index.php index.html;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
}

The important detail is fastcgi_pass php:9000 — inside the Compose network, services reach each other by service name, so php resolves to the PHP-FPM container automatically.

Step 3: Write the Compose File

Create docker-compose.yml in the project root:

services:
  nginx:
    image: nginx:1.27-alpine
    ports:
      - "80:80"
    volumes:
      - ./src:/var/www/html
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf:ro
    depends_on:
      - php

  php:
    image: php:8.3-fpm-alpine
    volumes:
      - ./src:/var/www/html
    working_dir: /var/www/html

  mysql:
    image: mysql:8.0
    environment:
      MYSQL_ROOT_PASSWORD: rootsecret
      MYSQL_DATABASE: appdb
      MYSQL_USER: appuser
      MYSQL_PASSWORD: appsecret
    volumes:
      - db_data:/var/lib/mysql

volumes:
  db_data:

Three services, one private network created automatically by Compose, and a named volume db_data so the database survives container rebuilds. In a real deployment, move the passwords into an .env file and never commit them to git.

Step 4: Add a Test Page and Start the Stack

Create src/index.php:

<?php
$pdo = new PDO('mysql:host=mysql;dbname=appdb', 'appuser', 'appsecret');
echo 'Connected to MySQL version: ' . $pdo->query('SELECT VERSION()')->fetchColumn();
phpinfo();

Start everything in the background:

docker compose up -d
docker compose ps

All three containers should show as Up. Open http://your-server-ip/ in a browser — you should see the MySQL version line followed by the PHP information page, proving that Nginx, PHP-FPM and MySQL are talking to each other.

Step 5: Daily Operations

# Follow logs of one service
docker compose logs -f nginx

# Open a shell in the PHP container
docker compose exec php sh

# Rebuild after changing code or config
docker compose up -d --build

# Stop the stack (data volume is preserved)
docker compose down

Never run docker compose down -v unless you really want to delete the database volume.

Step 6: Add PHP Extensions with a Custom Image

The stock php:8.3-fpm-alpine image does not include the MySQL PDO driver our test page uses, nor common extensions such as gd or intl that frameworks like Laravel and WordPress expect. The clean solution is a tiny custom Dockerfile for the PHP service. Create php/Dockerfile:

FROM php:8.3-fpm-alpine
RUN apk add --no-cache icu-dev libpng-dev     && docker-php-ext-install pdo_mysql mysqli gd intl opcache

Then point the php service in docker-compose.yml at this Dockerfile instead of the stock image:

  php:
    build: ./php
    volumes:
      - ./src:/var/www/html
    working_dir: /var/www/html

Rebuild with docker compose up -d --build and the extensions become part of your image — every future deployment gets exactly the same PHP environment, and new team members no longer need a wiki page full of manual apt commands.

Step 7: Move Secrets into an .env File

Hard-coding database passwords in the compose file is risky because the file usually ends up in version control. Create an .env file next to docker-compose.yml:

MYSQL_ROOT_PASSWORD=rootsecret
MYSQL_DATABASE=appdb
MYSQL_USER=appuser
MYSQL_PASSWORD=appsecret

Reference the variables in the compose file as ${MYSQL_PASSWORD} and add .env to your .gitignore. Compose reads the file automatically, so nothing else changes — but your repository no longer contains production credentials.

Troubleshooting

502 Bad Gateway. PHP-FPM is not up yet or the service name in fastcgi_pass is wrong. Check docker compose logs php.

Access denied for user. MySQL credentials are only applied on first initialization of an empty data directory. If you changed them later, either update the app config to match or remove the volume and start fresh.

Port 80 already in use. A host-level web server is bound to the port. Stop it, or change the mapping to "8080:80". When you are ready to add HTTPS, our guide on securing Nginx with Let’s Encrypt covers the concepts.

Conclusion

You now have a reproducible LEMP environment defined entirely in code: Nginx serving, PHP-FPM processing and MySQL persisting data in a named volume. The same compose file deploys anywhere Docker runs. For deeper coverage of volumes, networking and the Compose specification, read the official Docker Compose documentation.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.