[Docker] Setting up LEMP environment

First, we need to init the folder structure for docker compose

mkdir nginx-compose
cd nginx-compose
touch docker-compose.yml

Create some sub-folders and their specific files

mkdir -p {logs,nginx,public}
touch logs/{access,error}.log
touch nginx/site.conf
echo '<h1>LEMP Docker-Compose</h1><p><b>LONG LE</b>' > public/index.html
echo '<?php phpinfo(); ?>' > public/info.php

Edit nginx/site.conf file

server {
    index index.php index.html;
    server_name kakaka.vn;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /code;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

Edit docker-compose.yml file with content below:

version: "2"

services:
    web: 
        image: nginx:latest
        ports: 
            - "8080:80"
        volumes: 
            - ./nginx/site.conf:/etc/nginx/conf.d/site.conf
            - ./logs/error.log:/var/log/nginx/error.log
            - ./logs/access.log:/var/log/nginx/access.log
            - ./public:/code
        networks:
            - code-network
    php:
        image: php:fpm
        volumes:
            - ./public:/code
        networks:
            - code-network

networks:
    code-network:
        driver: bridge

Now, set host for the fake domain kakaka.vn and enjoy your last work.

If you want to add MySQL and PHPMyAdmin, you should add this code block into the docker-compose.yml file (in services)

version: "2"

services:
    web: 
        image: nginx:latest
        ports: 
            - "8080:80"
        volumes: 
            - ./nginx/site.conf:/etc/nginx/conf.d/site.conf
            - ./logs/error.log:/var/log/nginx/error.log
            - ./logs/access.log:/var/log/nginx/access.log
            - ./public:/code
        networks:
            - code-network
    php:
        image: php:fpm
        volumes:
            - ./public:/code
        networks:
            - code-network
    mysql:
        image: 'mariadb'
        ports:
            - '3308:3308'
        volumes:
            - ./db-data:/var/lib/mysql
        environment:
            - MYSQL_ROOT_PASSWORD=long@123
        networks:
            - code-network
    phpmyadmin:
        image: 'phpmyadmin/phpmyadmin'
        restart: always
        ports:
            - '8888:80'
        links:
            - mysql:mysql
        environment:
            MYSQL_USERNAME: root
            MYSQL_ROOT_PASSWORD: long@123
            PMA_HOST: mysql
        networks:
            - code-network
networks:
    code-network:
        driver: bridge

Testing PHPMyAdmin by browsing this link

http://kakaka.vn:8888

Testing MariaDB by

docker container exec -it <mariadb_container_name> /bin/bash
mysql -u root -P 3308 -p
(enter password ...)

DONE.

Leave a Reply

Your email address will not be published. Required fields are marked *