环境要求:
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
提前准备服务配置文件:
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
[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
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容器测试访问
[root@node1 ~]# 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
[root@node1 ~]# echo hello > /www/index.html
[root@node1 ~]# curl 192.168.1.1
hello
3、运行MysqL容器
#创建MysqL挂载数据目录、开发端口、指定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
#查看运行状态
[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
#测试登录
[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
4、创建PHP-fpm容器
[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
[root@node1 ~]# vim /conf/conf.d/default.conf
[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!
[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
能够访问到以上两个界面,说明Nginx和PHP的连接,没有问题,接下来是 PHP和MysqL的连接。在这里我们使用一个PHPMyAdmin的数据库管理工 具。
6、测试PHP容器与MysqL容器的协调性、工具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
[root@node1 www]# cd /conf/conf.d/
[root@node1 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,我们要重新写一个。
[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 .
#删除PHP容器
[root@node1 /]# docker rm PHP-fpm -f
PHP-fpm
#运行PHPMysqL容器
[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
[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、访问测试
9、趁热打铁,在发布一个dz论坛
#上传
[root@node1 /]# ls
bin Discuz_X3.4_SC_UTF8_20210119.zip
#移动到/www网页目录下
mv upload/ /www/dz
chmod 777 /www/dz
#保证数据库有dz库
#授权一个dz库的管理员
#注意安装是IP为数据库的IP不要写错
原文链接:https://www.f2er.com/docker/997443.html