docker deployment LNMP environment

The first step is to confirm that there are tar packages needed in your environment, and you can use [docker pull]() to download these images

Now we're using a downloaded image, so we need to import it

[root@docker01 ~]# docker load -i nginx.tar  && docker load -i wordpress.tar  && docker load -i mysql-5.7.tar && docker load -i php.7.2-fpm.tar
//Import nginx,wordpress,mysql,php mirrors

Whole process:
Client http requests server port 80, which is mapped to Nginx container port 80 for Nginx processing.
Nginx parses the request, reading the content directly from the server if it is a static resource; if it is a PHP script, invoke the server through the PHP container to get the script, and then FastCGI processes it.
FastCGI parses PHP scripts and accesses MySQL containers to read and write data when necessary.

Deploy LNMP




[Php :]()

Visit Home Directory of Web Site: /wwroot

Configuration file for Nginx: /docker

/etc/nginx/conf.d #nginx configuration file

[root@docker01 ~]# docker run -itd --name test nginx:latest 
//Start a nginx to copy the configuration file and access the home directory
[root@docker01 ~]# mkdir  -p /wwwroot /docker
//Create mount directory
[root@docker01 ~]# docker cp test:/etc/nginx /docker/
//Copy profile to mount directory
[root@docker01 ~]# ls /docker/

/usr/share/nginx/html #nginx home directory

[root@docker01 ~]# docker cp test:/usr/share/nginx/html /wwwroot/
//Copy Access Directory to Mount Directory
[root@docker01 ~]# ls /wwwroot/

1) Create a custom network

[root@docker01 ~]# docker network create -d bridge --subnet --gateway lnmp

2) Run nginx container

[root@docker01 ~]# netstat -anpt | grep 80
//Check if port 80 is occupied
[root@docker01 ~]# docker run -itd --name nginx -v /docker/nginx:/etc/nginx -v /wwwroot/html:/usr/share/nginx/html   -p 80:80 --network lnmp --ip nginx
//Run a nginx service, specify ip, map port, mount directory
[root@docker01 ~]# docker ps
//See if the container exists

[root@docker01 ~]# cd /wwwroot/html
[root@docker01 wwwroot]# vim index.html
hello lnmp!
//Create Test Page

[root@docker01 wwwroot]# curl
hello lnmp!
//Test Access

3) Run mysql container

[root@docker01 html]# docker run --name mysql -e -d -p 3306:3306 --network lnmp --ip mysql:5.7
//Run a nginx service, specify ip, map port

-e: Set environment variables

[root@docker02 ~]# docker ps

Install mysql and set the password

[root@docker01 html]# yum -y install mysql
//Install mysql
[root@docker01 ~]# mysql -u root -h -P 3306

Create a new library to verify:

MySQL [(none)]> create database name;

Check to see if there are any libraries just created:

MySQL [(none)]> show databases;

4) Run the php container and create the php page

[root@docker01 html]# docker run -itd --name phpfpm -p 9000:9000 -v /wwwroot/html:/usr/share/nginx/html --network lnmp --ip php:7.2-fpm 
[root@docker01 ~]# cd /wwwroot/html
[root@docker01 wwwroot]# vim test.php
//Add php test interface
[root@docker02 ~]# docker ps

5) Modify nginx configuration files, nginx and php connections

[root@docker01 html]# cd /docker/nginx/conf.d/
[root@docker01 conf.d]# vim default.conf 
location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm index.php; #10 Add index.php

location ~ \.php$ {        
     root    /usr/share/nginx/html;        
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;        include        fastcgi_params;    }

Restart nginx after setup

[root@docker01 conf.d]# docker restart nginx
//Restart nginx
[root@docker01 conf.d]# docker ps

Browser Test Access to nginx and php

The instructions are a connection between nginx and php, no problem, then a connection between PHP and MySQL.Here we use a database management tool called phpmyadmin

6) Modify nginx configuration files, php and mysql connections

[root@docker01 html]# cd /wwwroot/html

Upload phpMyAdmin package If not, download it at

[root@docker01 html]# unzip 
//Unzip phpmyadmin package
[root@docker01 html]# mv phpMyAdmin-4.9.1-all-languages phpmyadmin
//Change the name of the file you just Unzipped
[root@docker01 html]#  cd  /docker/nginx/conf.d/
[root@docker01 conf.d]#  vim default.conf 
//Modify nginx configuration file
        location /phpmyadmin {
                root   /usr/share/nginx/html;
                index   index.html index.htm index.php;

    location ~ /phpmyadmin/(?<after_ali>(.*)\.(php|php5)?$) {
        root           /usr/share/nginx/html;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;

[root@docker01 conf.d]# docker restart nginx 
[root@docker01 conf.d]# docker ps 

Browser Access

Red box is a normal phenomenon, don't panic, we will solve it next

We need to make changes to the php mirror to add php and MySQL connection modules

Write a Dockerfile

[root@docker01 conf.d]# cd 
[root@docker01 ~]# vim Dockerfile
FROM php:7.2-fpm
RUN apt-get update && apt-get install -y \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libpng-dev \
    && docker-php-ext-install -j$(nproc) iconv \
    && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
    && docker-php-ext-install -j$(nproc) gd \
        && docker-php-ext-install mysqli pdo pdo_mysql

Creating php mirror based on dockerfile

[root@docker01 ~]# docker build -t phpmysql .
//Create a mirror based on Dockerfile

Remove previous php containers

[root@docker01 ~]# docker stop phpfpm
[root@docker01 ~]# docker rm phpfpm 
//Close and remove php container

Run container with new php image

[root@docker01 ~]# docker run -itd --name phpfpm -p 9000:9000 -v /wwwroot/html:/usr/share/nginx/html --network lnmp --ip phpmysql
//Rerun with new php image

//Modify the phpmyadmin configuration file, specify the IP of the connected database, and restart the php container

[root@docker01 html]# cd /wwwroot/html/phpmyadmin/
[root@docker01 phpmyadmin]# cp
[root@docker01 phpmyadmin]# vim
$cfg['Servers'][$i]['auth_type'] = 'cookie';
/* Server parameters */
$cfg['Servers'][$i]['host'] = '';   #31 Write the IP address of the mysql database
$cfg['Servers'][$i]['compress'] = false;
$cfg['Servers'][$i]['AllowNoPassword'] = false;

[root@docker01 phpmyadmin]# docker restart phpfpm 
//Restart phpfpm container

Browser Test Access

User name: root password:

After successful login you will see the database previously created by mysql

Keywords: Nginx PHP Docker MySQL

Added by Havery Jay on Fri, 20 Dec 2019 22:21:35 +0200