Linux-Docker deploys LNMP environment

Docker deploys LNMP

Network Planning:

192.168.1.40 Docker
172.16.10.10 Nginx container
172.16.10.20 MySQL container
172.16.10.30 PHP container

Mirroring requirements:

Nginx:nginx:latest

MySQL:mysql:5.7

PHP:php:7.2-fpm

[root@docker ~]# docker pull nginx
[root@docker ~]# docker pull mysql:5.7
[root@docker ~]# docker pull php:7.2-fpm

Directory planning:

  • The main directory of the website: /docker/
  • Nginx configuration file: /docker/
[root@docker ~]# mkdir /docker
#运行一个创建目录的容器
[root@docker ~]# docker run  -itd --name  test nginx
1ed78b1e1280829ccaed3fe7108d7bdccd776f792eda9e0a188e7225600c177f
#把nginx的配置文件拷贝出来
[root@docker ~]# docker cp test:/etc/nginx /docker/
[root@docker ~]# ls /docker/
nginx
#把网站目录拷贝出来
[root@docker ~]# docker cp  test:/usr/share/nginx/html /docker/
[root@docker ~]# ls /docker/
html  nginx

1. Set up the network

Create a custom bridged network

[root@docker ~]# docker network create  -d bridge  --subnet 172.16.10.0/24 --gateway 172.16.10.1 LNMP
d81e599d1ad6a73ff36128c04e571021ecda614539f716dab4a95715a66d2c33
[root@docker ~]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
d81e599d1ad6        LNMP                bridge              local

2. Nginx container

[root@docker ~]# docker run  -itd --name  nginx -v /docker/nginx:/etc/nginx -v /docker/html:/usr/share/nginx/html -p 80:80 --network LNMP  --ip 172.16.10.10 nginx:latest 
232b016e56d3ef8b5db3a7f10757bc450fadd5350c227fecd19162e082c474af

3.MySQL container

[root@docker ~]# docker run --name  mysql -e MYSQL_ROOT_PASSWORD=123.com -d -p 3306:3306 --network LNMP  --ip 172.16.10.20 mysql:5.7 
56505dc496054b7f0fd6881b2b73073ee9c08ef698338f7ed54a0bf61d5f8f0c
  • -e MYSQL_ROOT_PASSWORD=123.com: is to set a password for the mysql account.

4. PHP container

[root@docker ~]# docker run  -itd --name  php -p 9000:9000 -v /docker/html:/usr/share/nginx/html --network LNMP  --ip 172.16.10.30 php:7.2-fpm 
5ca6bf3edab5830160cd9650c448059ec284b31a3e6aae5fbbd329a95bb2456a

5. Connect

1. nginx and php connection verification

[root@docker ~]# cd /docker/nginx/conf.d/
[root@docker conf.d]# vim default.conf 
......
 11         index  index.html index.htm index.php;
......
 
 31   location ~ \.php$ {
 32         root           /usr/share/nginx/html;
 33         fastcgi_pass   172.16.10.30:9000;
 34         fastcgi_index  index.php; 35         fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_s    cript_name;
 36         include        fastcgi_params;
 37     }
  • Delete the comment # number on lines 31-37, and then modify its content
  • d o c u m e n t r o o t document_root documentro o t : open fastcgi configuration

Add nginx static website page and php test page

[root@docker /]# cd /docker/html/
[root@docker html]# echo LNMP Hello !! >> index.html 
[root@docker html]# vim test.php 
<?php
phpinfo();
?>
[root@docker html]# docker restart nginx
nginx

Firewall

Open the firewall and make port pass rules

[root@docker ~]# systemctl start firewalld
[root@docker ~]# firewall-cmd --add-port=80/tcp --permanent 
success
[root@docker ~]# firewall-cmd --add-port=3306/tcp --permanent 
success
[root@docker ~]# firewall-cmd --add-port=9000/tcp --permanent 
success
[root@docker ~]# firewall-cmd  --reload 
success
[root@docker ~]# firewall-cmd  --list-ports 
80/tcp 3306/tcp 9000/tcp

windows access:

Insert picture description here

Insert picture description here

2. php and mysql connection verification

In order to realize the connection between php and mysql, here I use phpMyAdmin database management tool to achieve!

Download the resource package of phpMyAdmin to the official website to download:

https://www.phpmyadmin.net/

[root@docker ~]# yum -y install unzip
[root@docker ~]# cd /docker/
[root@docker docker]# ls
html 	nginx  	phpMyAdmin-4.9.1-all-languages.zip
[root@docker docker]# unzip phpMyAdmin-4.9.1-all-languages.zip 
#该目录名
[root@docker docker]# mv phpMyAdmin-4.9.1-all-languages phpmyadmin
#把phpmyadmin移动到网站目录下
[root@docker docker]# mv phpmyadmin/ ./html/

Change nginx configuration file

[root@docker ~]# vim /docker/nginx/conf.d/default.conf 
......
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;
    }    
.......
[root@docker ~]# docker restart  nginx 
nginx

Visit will find that the extension is missing

Insert picture description here

Add PHP and MySQL connection modules to the mirror

[root@docker Dockerfile]# docker build -t phpmysql .[root@docker docker]# mkdir Dockerfile
[root@docker docker]# cd Dockerfile/
[root@docker Dockerfile]# 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
[root@docker Dockerfile]# docker build -t phpmysql .
#接下来时间可能较长,请耐心等待

Delete the previous php container and run it with the php image just made with mysql extension

[root@docker ~]# docker rm -f php 
php
[root@docker ~]# docker run  -itd --name  php -p 9000:9000 -v /docker/html:/usr/share/nginx/html --network LNMP  --ip 172.16.10.30 phpmysql:latest 
2a5feac4c98e11956f97e8f1c3169f8869ff28e9a838e6efc3fb5fbfbe6e7a32

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

[root@docker ~]# cd /docker/html/phpmyadmin/
[root@docker phpmyadmin]# cp config.sample.inc.php config.inc.php 
[root@docker phpmyadmin]# vim config.inc.php 
......
31 $cfg['Servers'][$i]['host'] = '172.16.10.20';
......
[root@docker phpmyadmin]# docker restart php 
php

The account and password are set when running MySQL

Account: root

Password: 123.com

Insert picture description here

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_45191791/article/details/108539185