docker部署LNMP架构

环境要求:

IP  hostname
192.168.1.1 node1

项目规划:

容器网段:172.16.10.0/24

NGINX:172.16.10.10

MySQL:172.16.10.20

PHP:172.16.10.20

网站根目录:/www

nginx配置文件:/conf

mysql持久化目录:/var/lib/mysql

提前准备服务配置文件:

nginx

docker run -itd --name  test nginx             #运行test容器     
docker cp test:/etc/nginx /conf      #copy主配置文件
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网站目录
ls /www/
50x.html  index.html          

 mysql

[[email protected] ~]# docker rm -f test 
test
[[email protected] ~]#  docker run -itd --name test -e MYSQL_ROOT_PASSWORD=pwd123 mysql:5.7 
6b8d73ecd541d454f121302963a85d53131286d3118a968525a24ad2315b047b
[[email protected] ~]# 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
[[email protected] ~]# docker cp test:/var/lib/mysql /var/lib/mysql

  1、配置lnmp虚拟网卡,网段172.16.10.0/24,网关172.16.10.254

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

 2、创建nginx容器测试访问 

[[email protected] ~]#  docker run -itd --name nginx -p 80:80 -v /www/:/usr/share/nginx/html -v /conf:/etc/nginx --network lnmp --ip 172.16.10.10  nginx:latest 
e6e8824abd10ec4f8cb610fdd3bb77bd5919cce94c50a86732e8fd2138b8091b
[[email protected] ~]#  echo  hello > /www/index.html 
[[email protected] ~]# curl  192.168.1.1
hello

 3、运行MySQL容器 

#创建mysql挂载数据目录、开发端口、指定IP
[[email protected] ~]# 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
#查看运行状态
[[email protected] ~]# 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
#测试登录
[[email protected] ~]# yum -y install  mariadb
[[email protected] ~]# 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

 4、创建php-fpm容器

[[email protected] 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
[[email protected] 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

 5、nginx和PHP的连接 

[[email protected] ~]# vim /conf/conf.d/default.conf 

 

 

 

 5.1、添加nginx静态的测试界面

[[email protected] ~]# echo  Hello LNMP! > /www/index.html 
[[email protected] ~]# cat /www/index.html 
Hello LNMP!
[[email protected] ~]# curl  192.168.1.1
Hello LNMP!

5.2、添加PHP测试页面 

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

 

能够访问到以上两个界面,说明nginx和php的连接,没有问题,接下来是 php和mysql的连接。在这里我们使用一个phpMyAdmin的数据库管理工 具。

 6、测试PHP容器与Mysql容器的协调性、工具phpMyadmin

[[email protected] www]# pwd
/www
[[email protected] www]# unzip phpMyAdmin-4.9.0.1-all-languages.zip
[[email protected] www]# mv phpMyAdmin-4.9.0.1-all-languages phpMyAdmin
[[email protected] www]# rm -rf phpMyAdmin  

6.1、更新nginx配置文件 

[[email protected] www]# cd /conf/conf.d/
[[email protected] conf.d]# vim default.conf 

 

 

 

 内容如下:

 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;
 }  

测试访问:

 

 

 其实这主要是php镜像不支持连接mysql,我们要重新写一个。

7、解决php不关联mysql

[[email protected] /]# cd /file/
[[email protected] file]# ls
Dockerfile
[[email protected] 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
[[email protected] file]# docker build -t php_mysql .
#删除php容器
[[email protected] /]# docker rm php-fpm -f
php-fpm
#运行phpmysql容器
[[email protected] /]# docker run -itd --name phpfpm -p 9000:9000 -v /www:/usr/share/nginx/html --network lnmp --ip 172.16.10.30 phpmysql:latest 
c4e943880fd51f947cba64ba0006abd26a923439a3e39a0350ca2561b42b8026
[[email protected] /]# 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配置指向mysql地址

[[email protected] www]# cd /www/phpmyadmin/
[[email protected] phpmyadmin]# mv config.sample.inc.php config.inc.php 
[[email protected] phpmyadmin]# vim config.inc.php
[[email protected] phpmyadmin]# docker restart phpfpm 
phpfpm

8、访问测试  

 

 

 9、趁热打铁,在发布一个dz论坛

dz论坛链接地址://down.chinaz.com/soft/41403.htm

 

#上传
[[email protected] /]# ls
bin       Discuz_X3.4_SC_UTF8_20210119.zip
#移动到/www网页目录下
mv upload/ /www/dz
chmod  777 /www/dz
#保证数据库有dz库
#授权一个dz库的管理员
#注意安装是IP为数据库的IP不要写错