Hi guys, In this series, we are going to setup LEMP Stack (Linux, Nginx, MySQL, PHP). Mainly it is used by web developers. I am assuming you have a basic idea about Docker & How it works.
In this blog, We are going to setup PHP and Nginx.
Why Docker?
I will not go too much deep, You can find more resources over the internet about the docker.
Docker makes the installation process very smooth and it gives your isolated environment as the container.
With the docker, There will be no more excuses like:
”It’s working on my machine.” :D
Because your docker environment remains the same across the platforms, So above excuses will going to turn with:
”It’s working on every machine” ;)
In Short, Docker makes tech person’s life easy like the developer, tester, etc.
My Machine configuration:
Docker Version: 18.09.7
Docker Compose Version: 1.24.1
RAM: 8GB
OS: Ubuntu 18.04
Make sure docker and docker-compose is installed on your machine.
Step 1: Create folders:
mkdir LEMP
cd LEMP
mkdir {public_html,nginx_conf}
public_html
: It will contains your PHP code.nginx_conf
: It will contains your Nginx configuration file.
Step 2: Create Nginx conf file
Go to nginx_conf
:
cd nginx_conf
Create file lemp-docker.conf
(You can give any name according to your requirement) & Paste below configuration block.
server {
listen 80;
server_name _;
root /public_html;
location / {
index index.php index.html;
}
location ~* \.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;
}
}
Save and Exit from folder.
Step 3: Create file docker-compose.yml
Create file docker-compose.yml
in LEMP
folder & paste the below lines:
version: '3'
services:
web:
image: nginx
ports:
- "80:80"
volumes:
- ./public_html:/public_html
- ./nginx_conf:/etc/nginx/conf.d
- /tmp/nginx_logs:/var/log/nginx
networks:
- nginx-php
php:
image: php:7.2-fpm
volumes:
- ./public_html:/public_html
networks:
- nginx-php
networks:
nginx-php:
Here we have created the network with the name nginx-php
. You can check more about the networks here.
You can set volumes
path according to your requirement.
Step 4: Let’s Up the containers
Create container:
cd LEMP/
docker-compose up
Note: Make sure no other service is running on port 80.
Output:
You can hit the localhost
on your browser.
Once you hit the localhost
check nginx logs:
cat /tmp/nginx_logs/access.log
172.25.0.1 - - [16/May/2020:12:51:04 +0000] "GET / HTTP/1.1" 404 556 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36" "-"
Now stop the containers by simply hitting Ctrl + c
. Now run docker-compose in detached mode. It will run in the background.
docker-compose up -d
Lists containers
docker-compose ps
Step 5: Let’s Create test.php
Go to LEMP/public_html
cd LEMP/public_html
Create file test.php
& paste below code:
<?php
echo phpinfo();
Step 6: Run test.php
Visit http://localhost/test.php.
Output:
At the End
We have successfully created Nginx and PHP Environment. In Part 2, We will check, How we can add MySQL to this environment.