LEMP Stack Setup with Docker Compose on Debian / Ubuntu

In this guide, you will learn LEMP Stack (Nginx, MariaDB, and PHP) Setup with Docker Compose on Debian / Ubuntu. Docker Compose is a tool for creating and running multi-container Docker applications. It allows you to use a YAML file to configure the services (containers) that make up your application, along with their dependencies and other settings, and then run and manage them as a single unit. Here we try to show you how to install LEMP Stack with Docker Compose on Debian and Ubuntu distros.

A Comprehensive Guide For LEMP Stack Setup with Docker Compose on Debian / Ubuntu

Before you start your LEMP Stack setup with Docker Compose, you must access your server as a non-root user with sudo privileges. Also, you need to install Docker and Docker Compose on your server.

In this guide, to show you the installation steps, we use Debian 12. For this purpose, you can check the following guides:

Initial Server Setup with Debian 12

Install Docker on Debian 12

Install Docker Compose on Debian 12

Once you are done with these requirements, follow the steps below to complete this guide.

Step 1 – Create Required Directories For LEMP Stack Setup

At this point, you can check your Docker Compose installation by checking its version:

docker compose version
Example Output
Docker Compose version v2.24.5

Then, you need to create a main directory for LEMP Stack. To do this, run the command below:

mkdir lemp

Next, create the subdirectories for Nginx config and PHP files with the command below:

mkdir lemp/{nginx-conf,php-files}

Step 2 – Create LEMP Stack YAML File on Debian / Ubuntu

At this point, you need to switch to your LEMP directory with the command below:

cd lemp

Then, you need to create a Docker YAML file for your LEMP Stack to deploy the Nginx, MariaDB, and PHP services. To do this, you can use your desired text editor like Vi Editor or Nano Editor:

sudo vi docker-compose.yml

Add the following configuration content to the file:

version: '3.8'

# Services
services:

    # PHP Service
    php:
        build:
            dockerfile: php-dockerfile
        volumes:
            - './php-files:/var/www/html'
        depends_on:
            - mariadb

    # Nginx Service
    nginx:
        image: nginx:latest
        ports:
            - 80:80
        links:
            - 'php'
        volumes:
            - './php-files:/var/www/html'
            - './nginx-conf:/etc/nginx/conf.d'
        depends_on:
            - php

    # MariaDB Service
    mariadb:
        image: mariadb:10.9
        environment:
            MYSQL_ROOT_PASSWORD: your_password
        volumes:
            - mysqldata:/var/lib/mysql

    # phpMyAdmin Service
    phpmyadmin:
        image: phpmyadmin/phpmyadmin:latest
        ports:
            - 8080:80
        environment:
            PMA_HOST: mariadb
        depends_on:
            - mariadb

# Volumes
volumes:

  mysqldata:

When you are done, save and close the file. As you can see, we added the images to deploy Nginx, PHP, MariaDB, and phpMyAdmin.

Step 3 – Create PHP-FPM Docker File on Debian / Ubuntu

At this point, you need to create a Docker file for PHP-FPM inside your LEMP Stack directory. To do this, you can use the latest official image:

sudo vi php-dockerfile

Add the following content to the file:

FROM php:8.2-fpm

# Installing dependencies for the PHP modules
RUN apt-get update && \
    apt-get install -y zip libzip-dev libpng-dev

# Installing additional PHP modules
RUN docker-php-ext-install mysqli pdo pdo_mysql gd zip

If you need more PHP extensions, you can add them to the file. Save and close the file.

The “php-files” directory points to “/var/www/html” in the PHP container, where PHP looks for executable PHP scripts. So for testing, we create the index.html file and the simple PHP info content to the file:

sudo vi php-files/index.php

Add the following sample code:

<?php phpinfo(); ?>

Save and close the file.

Step 4 – Create Nginx Config File For LEMP Stack Setup

Nginx must work with PHP files. So you need to create a Nginx config file inside the nginx-conf directory:

sudo vi nginx-conf/nginx.conf

Add the following Nginx configuration to the file:

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    
    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;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
    }
}

When you are done, save and close the file.

Step 5 – Run LEMP Stack with Docker Compose

At this point, you can simply start and run your service by using the following command inside the LEMP directory:

docker compose up -d

The images will start to download. Once everything is completed, you will get the following output:

Start and Run LEMP with Docker Compose

Now you can access your PHP information and phpMyAdmin from your web browser by following the URLs below:

http://server-ip:8080
http://server-ip
phpMyAdmin with Docker Compose

To manage the Docker Containers, you can check this guide on Manage Docker Containers.

Conclusion

Docker Compose is commonly used in development and testing for multiple services. It will simplify the process of defining, running, and managing multi-container Docker applications. At this point, you have learned LEMP Stack Setup with Docker Compose on Debian / Ubuntu. Hope you enjoy it. Also, you may like to read the following articles:

Run Docker Containers with Portainer

Start a Docker Container with Exited Status

Install MySQL in Docker Container on Debian 12

Run a Python Script in a Linux Docker Container

Install Portainer Docker GUI on Debian 12

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles

POPULAR TAGS

Most Popular