Docker Compose lets you define and run multi‑container applications. This guide sets up a complete LNMP stack (Nginx, MySQL, PHP‑FPM) for local development or production.
Prerequisites
A VPS with Docker and Docker Compose installed. Check our VPS reviews if you need one. Basic knowledge of YAML and the command line.
Step 1: Create Project Directory
mkdir lnmp && cd lnmp
Step 2: Create docker-compose.yml
version: '3.8'
services:
nginx:
image: nginx:alpine
container_name: nginx
ports:
- "80:80"
- "443:443"
volumes:
- ./www:/var/www/html
- ./nginx/default.conf:/etc/nginx/conf.d/default.conf
depends_on:
- php
restart: unless-stopped
php:
image: php:8.2-fpm
container_name: php-fpm
volumes:
- ./www:/var/www/html
restart: unless-stopped
mysql:
image: mysql:8.0
container_name: mysql
environment:
MYSQL_ROOT_PASSWORD: rootpassword
MYSQL_DATABASE: appdb
MYSQL_USER: user
MYSQL_PASSWORD: password
volumes:
- db_data:/var/lib/mysql
restart: unless-stopped
volumes:
db_data:
Step 3: Create Nginx Configuration
Create a directory nginx and a file default.conf:
server {
listen 80;
server_name localhost;
root /var/www/html;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Step 4: Create Sample PHP File
mkdir www
echo "<?php phpinfo(); ?>" > www/index.php
Step 5: Start Containers
docker-compose up -d
Open your browser at http://your_server_ip. You should see the PHP info page.
Step 6: Add WordPress (Optional)
Download WordPress into the www directory and update database credentials in wp-config.php:
define('DB_HOST', 'mysql');
Then visit http://your_server_ip and complete WordPress installation.
Managing Containers
Stop all containers: docker-compose down. View logs: docker-compose logs -f. Rebuild after changes: docker-compose up -d --build.
Next Steps
You can extend this stack by adding phpMyAdmin, Redis, or other services to the compose file. For production, add SSL (with Certbot container) and secure MySQL.
Need a VPS to practice on? Check our recommended VPS providers.