How to deploy LNMP architecture in docker

How to deploy LNMP architecture in docker

Environmental requirements:

IP hostname
192.168.1.1 node1

Project Planning:

Container network segment: 172.16.10.0/24

NGINX: 172.16.10.10

MySQL: 172.16.10.20

PHP: 172.16.10.20

Website root directory:/www

Nginx configuration file: /conf

mysql persistence directory: /var/lib/mysql

Prepare the service configuration file in advance:

nginx

docker run -itd --name test nginx       
#Run the test container docker cp test:/etc/nginx /conf 
     #copy the main configuration file ls /conf/
conf.d koi-win nginx.conf win-utf
fastcgi_params mime.types scgi_params
koi-utf modules uwsgi_params
docker cp test:/usr/share/nginx/html /www  
 #copy website directory ls /www/
50x.html 
index.html     
</strong>

mysql

[root@node1 ~]# docker rm -f test
test
[root@node1 ~]# docker run -itd --name test -e MYSQL_ROOT_PASSWORD=pwd123 mysql:5.7
6b8d73ecd541d454f121302963a85d53131286d3118a968525a24ad2315b047b
[root@node1 ~]# docker exec -it test sh
# mysql -uroot -ppwd123 -h127.0.0.1
..........
mysql> create database test;
Query OK, 1 row affected (0.00 sec)
 
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
|mysql |
| performance_schema |
|sys|
| test |
+--------------------+
5 rows in set (0.00 sec)
 
mysql> exit
Bye
# exit
[root@node1 ~]# docker cp test:/var/lib/mysql /var/lib/mysql
</strong>

1. Configure lnmp virtual network card, network segment 172.16.10.0/24, gateway 172.16.10.254

docker network create -d bridge --subnet 172.16.10.0/24 --gateway 172.16.10.254 lnmp
</strong>

2. Create an nginx container to test access

#Create mysql mount data directory, development port, and specify IP
[root@node1 ~]# docker run -itd --name mysql -p 3306:3306 -v /var/lib/mysql:/var/lib/mysql --network lnmp --ip 172.16.10.20 mysql:5.7
448227483a9c3141c2155d2c7b027aec263bfcfe4ebc49371b6817c17565ff81
#Check the running status [root@node1 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
448227483a9c mysql:5.7 "docker-entrypoint.s..." 10 seconds ago Up 9 seconds 0.0.0.0:3306->3306/tcp, 33060/tcp mysql
4d1e99a06972 nginx:latest "/docker-entrypoint.…" 3 minutes ago Up 3 minutes 0.0.0.0:80->80/tcp nginx
6b8d73ecd541 mysql:5.7 "docker-entrypoint.s..." 9 minutes ago Up 9 minutes 3306/tcp, 33060/tcp test
#Test login [root@node1 ~]# yum -y install mariadb
[root@node1 ~]# mysql -uroot -ppwd123 -h127.0.0.1
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.33 MySQL Community Server (GPL)
 
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
MySQL [(none)]> Bye
</strong>

3. Run the MySQL container

[root@node1 www]# docker run -itd --name php-fpm -p 9000:9000 -v /www:/usr/share/nginx/html --network lnmp --ip 172.16.10.30 php:7.2-fpm 
ae09213d7c8c84299b1522ca474fccf7f26e27973cd02563891c37d51799b766
[root@node1 www]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ae09213d7c8c php:7.2-fpm "docker-php-entrypoi…" 7 seconds ago Up 6 seconds 0.0.0.0:9000->9000/tcp php-fpm

4. Create a php-fpm container

[root@node1 ~]# vim /conf/conf.d/default.conf
</strong>

5. Connection between nginx and PHP

5.1. Add nginx static test interface

<strong>[root@node1 ~]# echo Hello LNMP! > /www/index.html
[root@node1 ~]# cat /www/index.html
Hello LNMP!
[root@node1 ~]# curl 192.168.1.1
Hello LNMP!
</strong>

5.2. Add PHP test page

[root@node1 ~]# cd /www
[root@node1 www]# vim test.php
[root@node1 www]# cat test.php
<?php
phpinfo();
?>
[root@node1 www]# docker restart nginx
nginx
[root@node1 www]# pwd
/www
[root@node1 www]# ls
50x.html index.html test.php
</strong> 

Being able to access the above two interfaces indicates that there is no problem with the connection between nginx and php. The next step is the connection between php and mysql. Here we use a phpMyAdmin database management tool.

6. Test the coordination between PHP container and MySQL container, tool phpMyadmin

[root@node1 www]# pwd
/www
[root@node1 www]# unzip phpMyAdmin-4.9.0.1-all-languages.zip
[root@node1 www]# mv phpMyAdmin-4.9.0.1-all-languages ​​phpMyAdmin
[root@node1 www]# rm -rf phpMyAdmin

6.1. Update nginx configuration file

[root@node1 www]# cd /conf/conf.d/
[root@node1 conf.d]# vim default.conf 

The content is as follows:

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

Test access:

In fact, this is mainly because the PHP image does not support connecting to MySQL, so we need to write a new one.

7. Solve the problem that PHP is not associated with MySQL

[root@node1 /]# cd /file/
[root@node1 file]# ls
Dockerfile
[root@node1 file]# cat 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-freetypedir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-install -j$(nproc) gd \
&& docker-php-ext-install mysqli pdo pdo_mysql
[root@node1 file]# docker build -t php_mysql .
#Delete php container [root@node1 /]# docker rm php-fpm -f
php-fpm
#Run phpmysql container [root@node1 /]# docker run -itd --name phpfpm -p 9000:9000 -v /www:/usr/share/nginx/html --network lnmp --ip 172.16.10.30 phpmysql:latest
c4e943880fd51f947cba64ba0006abd26a923439a3e39a0350ca2561b42b8026
[root@node1 /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c4e943880fd5 phpmysql:latest "docker-php-entrypoi…" 14 seconds ago Up 13 seconds 0.0.0.0:9000->9000/tcp phpfpm

7.1、phpmysql configuration points to mysql address

[root@node1 www]# cd /www/phpmyadmin/
[root@node1 phpmyadmin]# mv config.sample.inc.php config.inc.php
[root@node1 phpmyadmin]# vim config.inc.php
[root@node1 phpmyadmin]# docker restart phpfpm
phpfpm

8. Access test

9. Strike while the iron is hot and publish a dz forum

dz forum link address: http://down.chinaz.com/soft/41403.htm

#Upload [root@node1 /]# ls
bin Discuz_X3.4_SC_UTF8_20210119.zip
#Move to the /www web directory mv upload/ /www/dz
chmod 777 /www/dz<br># Ensure that the database has a dz library<br># Authorize an administrator of the dz library<br># Note that the installation IP is the database IP and do not make a mistake<br> 

This is the end of this article about how to deploy LNMP architecture in docker. For more information about deploying LNMP architecture in docker, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to quickly build an LNMP environment with Docker (latest)
  • Use Docker to create a distributed lnmp image
  • How to build lnmp environment in docker
  • Use Docker to create an integrated service lnmp environment
  • Detailed explanation of using Docker to build LNMP environment
  • Example of compiling LNMP in Docker container

<<:  CSS achieves footer "bottom absorption" effect

>>:  Node.js returns different data according to different request paths.

Recommend

Docker uses a single image to map to multiple ports

need: The official website's resource server ...

Nested display implementation of vue router-view

Table of contents 1. Routing Configuration 2. Vue...

MySQL stored procedure method example of returning multiple values

This article uses an example to describe how to r...

Detailed explanation of built-in methods of javascript array

Table of contents 1. Array.at() 2. Array.copyWith...

How to solve the problem of automatic package update in Debian system

I don't know when it started, but every time ...

Example of using js to natively implement year carousel selection effect

Preface Use js to achieve a year rotation selecti...

Three common methods for HTML pages to automatically jump after 3 seconds

In practice, we often encounter a problem: how to...

How to build ssh service based on golang image in docker

The following is the code for building an ssh ser...

Tutorial on installing MySQL under Linux

Table of contents 1. Delete the old version 2. Ch...

Centos7 installation of FFmpeg audio/video tool simple document

ffmpeg is a very powerful audio and video process...

How to apply TypeScript classes in Vue projects

Table of contents 1. Introduction 2. Use 1. @Comp...

A brief discussion on the role of HTML empty links

Empty link: That is, there is no link with a targ...