笺注:Docker的安装可参考 CentOS8_在Docker中使用Nginx的反向代理
查看Docker的版本信息:
[root@centos8 ~]# docker -v
Docker version 20.10.21, build baeda1f
[root@centos8 ~]#
已经设置开机自动启动Docker:
[root@centos8 ~]# systemctl is-enabled docker
enabled
[root@centos8 ~]#
Docker多容器+LNMP的架构图:
[root@centos8 ~]# yum -y install net-tools
[root@centos8 ~]# ifconfig
docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:3c:09:e1:bc txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.168.154 netmask 255.255.255.0 broadcast 192.168.168.255
inet6 fe80::92ab:5fb4:5373:ad53 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:0e:fa:49 txqueuelen 1000 (Ethernet)
RX packets 33078 bytes 46961858 (44.7 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 10022 bytes 650627 (635.3 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@centos8 ~]#
给Docker创建自定义虚拟网卡,并且指定网段:
[root@centos8 ~]# docker network create --subnet=192.168.80.0/24 zhuohua_docker
d69d5bb05e6f084e63b5b405478f18672bd8260fa9e8509cfe084fe7ad72db24
[root@centos8 ~]#
备注:给Docker创建自定义虚拟网卡后,容器才能使用固定IP地址;
查看Docker所有的虚拟网卡:
[root@centos8 ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
1547258ea97b bridge bridge local
ecf4d5fc9a71 host host local
f3a83278f2d8 none null local
d69d5bb05e6f zhuohua_docker bridge local
[root@centos8 ~]#
备注:
网卡ID“d69d5bb05e6f”是自动生成的;
网卡名“zhuohua_docker”是自定义的;
[root@centos8 ~]# ifconfig
br-d69d5bb05e6f: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 192.168.80.1 netmask 255.255.255.0 broadcast 192.168.80.255
ether 02:42:e7:a8:8f:73 txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:3c:09:e1:bc txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.168.154 netmask 255.255.255.0 broadcast 192.168.168.255
inet6 fe80::92ab:5fb4:5373:ad53 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:0e:fa:49 txqueuelen 1000 (Ethernet)
RX packets 33196 bytes 46970314 (44.7 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 10057 bytes 655517 (640.1 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@centos8 ~]#
######
从公网下载镜像:( 以下是下载MariaDB10.3的镜像 )
[root@centos8 ~]# docker pull mariadb:10.3
查看宿主机的所有镜像:
[root@centos8 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mariadb 10.3 2c702dbbd1f8 3 weeks ago 369MB
[root@centos8 ~]#
使用镜像“mariadb:10.3”创建并启动容器“MariaDB_1”:
[root@centos8 ~]# docker run -itd --name MariaDB_1 -e MYSQL_ROOT_PASSWORD=P@ssw0rd --net zhuohua_docker --ip 192.168.80.2 mariadb:10.3
95f4fedeeeab65fc0703ef177d6eb4d7d7dfe3495d50c0344155ac4f1ede288f
[root@centos8 ~]#
注释:
-itd : 以交互模式情况下后台运行。
--name : 指定容器名称。
-e 环境配置 : 指定数据库管理员root@localhost的密码为P@ssw0rd
--net zhuohua_docker --ip : 指定容器的固定IP地址(使用虚拟网卡“zhuohua_docker”)。
mariadb:10.3 : 镜像名称:版本号
在宿主机查看正在运行的容器:
[root@centos8 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
95f4fedeeeab mariadb:10.3 "docker-entrypoint.s…" About a minute ago Up About a minute 3306/tcp MariaDB_1
[root@centos8 ~]#
注释:容器“MariaDB_1”打开了TCP 3306端口。
###
进入容器“MariaDB_1”:
[root@centos8 ~]# docker exec -it MariaDB_1 /bin/bash
root@95f4fedeeeab:/#
查看容器“MariaDB_1”的系统版本信息:
root@95f4fedeeeab:/# cat /etc/issue
Ubuntu 20.04.5 LTS \n \l
root@95f4fedeeeab:/# uname -r
4.18.0-193.el8.x86_64
root@95f4fedeeeab:/#
查看容器“MariaDB_1”的IP地址:
root@95f4fedeeeab:/# ifconfig
bash: ifconfig: command not found
root@95f4fedeeeab:/#
root@95f4fedeeeab:/# apt-get update
root@95f4fedeeeab:/# apt-get -y install net-tools
root@95f4fedeeeab:/# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.80.2 netmask 255.255.255.0 broadcast 192.168.80.255
ether 02:42:c0:a8:50:02 txqueuelen 0 (Ethernet)
RX packets 11301 bytes 29765241 (29.7 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 7182 bytes 394856 (394.8 KB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 10 bytes 822 (822.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 10 bytes 822 (822.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
root@95f4fedeeeab:/#
备注:
宿主机或容器重启后,IP地址都是不变的;
查看MariaDB的版本信息:
root@95f4fedeeeab:/# mysql -V
mysql Ver 15.1 Distrib 10.3.38-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2
root@95f4fedeeeab:/#
MariaDB的主配置文件:(以下是初始状态)
root@95f4fedeeeab:/# find / -name my.cnf
/etc/mysql/my.cnf
find: ‘/proc/1/map_files’: Permission denied
root@95f4fedeeeab:/#
root@95f4fedeeeab:/# cat /etc/mysql/my.cnf |grep -v '^#' |grep -v '^$'
[client]
port = 3306
socket = /var/run/mysqld/mysqld.sock
[mysqld_safe]
socket = /var/run/mysqld/mysqld.sock
nice = 0
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc_messages_dir = /usr/share/mysql
lc_messages = en_US
skip-external-locking
max_connections = 100
connect_timeout = 5
wait_timeout = 600
max_allowed_packet = 16M
thread_cache_size = 128
sort_buffer_size = 4M
bulk_insert_buffer_size = 16M
tmp_table_size = 32M
max_heap_table_size = 32M
myisam_recover_options = BACKUP
key_buffer_size = 128M
table_open_cache = 400
myisam_sort_buffer_size = 512M
concurrent_insert = 2
read_buffer_size = 2M
read_rnd_buffer_size = 1M
query_cache_limit = 128K
query_cache_size = 64M
slow_query_log_file = /var/log/mysql/mariadb-slow.log
long_query_time = 10
expire_logs_days = 10
max_binlog_size = 100M
default_storage_engine = InnoDB
innodb_buffer_pool_size = 256M
innodb_log_buffer_size = 8M
innodb_file_per_table = 1
innodb_open_files = 400
innodb_io_capacity = 400
innodb_flush_method = O_DIRECT
[galera]
[mysqldump]
quick
quote-names
max_allowed_packet = 16M
[mysql]
[isamchk]
key_buffer = 16M
!include /etc/mysql/mariadb.cnf
!includedir /etc/mysql/conf.d/
root@95f4fedeeeab:/#
本地登录MariaDB数据库:(使用的是数据库管理员root@localhost,密码P@ssw0rd)
root@95f4fedeeeab:/# mysql -uroot -p"P@ssw0rd"
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.3.38-MariaDB-1:10.3.38+maria~ubu2004 mariadb.org binary distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
查看数据库里有哪些库:(以下3个库是默认就有的)
MariaDB [(none)]> Show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.000 sec)
MariaDB [(none)]>
查看所有数据库用户及其主机信息:(以下是初始状态)
MariaDB [(none)]> Select user,host from mysql.user;
+------+-----------+
| user | host |
+------+-----------+
| root | % |
| root | localhost |
+------+-----------+
2 rows in set (0.000 sec)
MariaDB [(none)]>
创建一个数据库用户并授权:
授权数据库用户zhuohua@'192.168.80.3'(密码168),仅仅在使用IP地址192.168.80.3时可以进行访问,对所有的库有完全控制的权限:
MariaDB [(none)]> Grant all on *.* to zhuohua@'192.168.80.3' identified by '168';
Query OK, 0 rows affected (0.002 sec)
MariaDB [(none)]>
MariaDB [(none)]> Select user,host from mysql.user;
+---------+--------------+
| user | host |
+---------+--------------+
| root | % |
| zhuohua | 192.168.80.3 |
| root | localhost |
+---------+--------------+
3 rows in set (0.001 sec)
MariaDB [(none)]>
查看数据库用户zhuohua@'192.168.80.3'的权限:
MariaDB [(none)]> Show grants for zhuohua@'192.168.80.3';
+----------------------------------------------------------------------------------------------------------------------------+
| Grants for zhuohua@192.168.80.3 |
+----------------------------------------------------------------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO `zhuohua`@`192.168.80.3` IDENTIFIED BY PASSWORD '*242E46A1E8D30FE06F7CE37B55BFC25BA981D70C' |
+----------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.000 sec)
MariaDB [(none)]>
######
从公网下载镜像:( 以下是下载PHP7.2的镜像 )
[root@centos8 ~]# docker pull php:7.2-fpm
查看宿主机的所有镜像:
[root@centos8 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mariadb 10.3 2c702dbbd1f8 3 weeks ago 369MB
php 7.2-fpm 28f52b60203d 2 years ago 398MB
[root@centos8 ~]#
使用镜像“php:7.2-fpm”创建并启动容器“PHP_1”:
[root@centos8 ~]# docker run -itd --name PHP_1 -v /opt/share:/var/www/html --net zhuohua_docker --ip 192.168.80.3 php:7.2-fpm
91a2bf7af44be75d12b9790a17e961f25f64731d860f3bf7ce4c2535731845d5
[root@centos8 ~]#
注释:
-itd : 以交互模式情况下后台运行。
--name : 指定容器名称。
-v 挂载目录 : 这里是把宿主机的目录/opt/share挂载到容器的目录/var/www/html;假如目录不存在,就会自动创建。
--net zhuohua_docker --ip : 指定容器的固定IP地址(使用虚拟网卡“zhuohua_docker”)。
php:7.2-fpm : 镜像名称:版本号
在宿主机查看正在运行的容器:
[root@centos8 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
91a2bf7af44b php:7.2-fpm "docker-php-entrypoi…" 2 minutes ago Up 2 minutes 9000/tcp PHP_1
95f4fedeeeab mariadb:10.3 "docker-entrypoint.s…" 19 minutes ago Up 19 minutes 3306/tcp MariaDB_1
[root@centos8 ~]#
注释:容器“PHP_1”打开了TCP 9000端口。
###
进入容器“PHP_1”:
[root@centos8 ~]# docker exec -it PHP_1 /bin/bash
root@91a2bf7af44b:/var/www/html#
root@91a2bf7af44b:/var/www/html# pwd
/var/www/html
root@91a2bf7af44b:/var/www/html# ls
root@91a2bf7af44b:/var/www/html#
查看容器“PHP_1”的系统版本信息:
root@91a2bf7af44b:~# cat /etc/issue
Debian GNU/Linux 10 \n \l
root@91a2bf7af44b:~# more /etc/debian_version
10.7
root@91a2bf7af44b:~# uname -r
4.18.0-193.el8.x86_64
root@91a2bf7af44b:~#
查看容器“PHP_1”的IP地址:
root@91a2bf7af44b:~# ifconfig
bash: ifconfig: command not found
root@91a2bf7af44b:~#
root@91a2bf7af44b:~# apt-get update
root@91a2bf7af44b:~# apt-get -y install net-tools
root@91a2bf7af44b:~# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.80.3 netmask 255.255.255.0 broadcast 192.168.80.255
ether 02:42:c0:a8:50:03 txqueuelen 0 (Ethernet)
RX packets 4359 bytes 9085430 (8.6 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 2832 bytes 154393 (150.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 6 bytes 526 (526.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 6 bytes 526 (526.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
root@91a2bf7af44b:~#
备注:
宿主机或容器重启后,IP地址都是不变的;
查看PHP的版本信息:
root@91a2bf7af44b:~# php -v
PHP 7.2.34 (cli) (built: Dec 11 2020 10:56:30) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
root@91a2bf7af44b:~#
######
从公网下载镜像:( 以下是下载Nginx/1.14.2的镜像 )
[root@centos8 ~]# docker pull nginx:1.14.2
查看宿主机的所有镜像:
[root@centos8 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mariadb 10.3 2c702dbbd1f8 3 weeks ago 369MB
php 7.2-fpm 28f52b60203d 2 years ago 398MB
nginx 1.14.2 295c7be07902 3 years ago 109MB
[root@centos8 ~]#
使用镜像“nginx:1.14.2”创建并启动容器“Nginx_1”:
[root@centos8 ~]# docker run -itd --name Nginx_1 -p 80:80 -v /opt/share:/usr/share/nginx/html --net zhuohua_docker --ip 192.168.80.4 nginx:1.14.2
a4d0039eb9d39498745c53fe40a7136a599f44934499060a7c6fd330752097bf
[root@centos8 ~]#
注释:
-itd : 以交互模式情况下后台运行。
--name : 指定容器名称。
-p 端口映射 : 第一个80是宿主机的端口,暴露给外部直接访问;第二个80是容器的端口。
--net zhuohua_docker --ip : 指定容器的固定IP地址(使用虚拟网卡“zhuohua_docker”)。
nginx:1.14.2 : 镜像名称:版本号
在宿主机查看所有的容器:(无论是否正在运行)
[root@centos8 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a4d0039eb9d3 nginx:1.14.2 "nginx -g 'daemon of…" 2 minutes ago Up 2 minutes 0.0.0.0:80->80/tcp, :::80->80/tcp Nginx_1
91a2bf7af44b php:7.2-fpm "docker-php-entrypoi…" 25 minutes ago Up 25 minutes 9000/tcp PHP_1
95f4fedeeeab mariadb:10.3 "docker-entrypoint.s…" 41 minutes ago Up 41 minutes 3306/tcp MariaDB_1
[root@centos8 share]#
注释:宿主机的TCP 80端口,映射到容器“Nginx_1”的TCP 80端口。
###
进入容器“Nginx_1”:
[root@centos8 ~]# docker exec -it Nginx_1 /bin/bash
root@a4d0039eb9d3:/#
查看容器“Nginx_1”的系统版本信息:
root@a4d0039eb9d3:/# cat /etc/issue
Debian GNU/Linux 9 \n \l
root@a4d0039eb9d3:/# more /etc/debian_version
9.8
root@a4d0039eb9d3:/# uname -r
4.18.0-193.el8.x86_64
root@a4d0039eb9d3:/#
查看容器“Nginx_1”的IP地址:
root@a4d0039eb9d3:/# ifconfig
bash: ifconfig: command not found
root@a4d0039eb9d3:/#
root@a4d0039eb9d3:/# apt-get update
root@a4d0039eb9d3:/# apt-get -y install net-tools
root@a4d0039eb9d3:/# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.80.4 netmask 255.255.255.0 broadcast 192.168.80.255
ether 02:42:c0:a8:50:04 txqueuelen 0 (Ethernet)
RX packets 4247 bytes 8645308 (8.2 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 3362 bytes 185793 (181.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1000 (Local Loopback)
RX packets 14 bytes 1230 (1.2 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 14 bytes 1230 (1.2 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
root@a4d0039eb9d3:/#
备注:
宿主机或容器重启后,IP地址都是不变的;
###
查看容器“Nginx_1”的Nginx版本:
root@a4d0039eb9d3:/# nginx -v
nginx version: nginx/1.14.2
root@a4d0039eb9d3:/#
root@a4d0039eb9d3:/# nginx -V
nginx version: nginx/1.14.2
built by gcc 6.3.0 20170516 (Debian 6.3.0-18+deb9u1)
built with OpenSSL 1.1.0f 25 May 2017 (running with OpenSSL 1.1.0j 20 Nov 2018)
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-g -O2 -fdebug-prefix-map=/data/builder/debuild/nginx-1.14.2/debian/debuild-base/nginx-1.14.2=. -specs=/usr/share/dpkg/no-pie-compile.specs -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-specs=/usr/share/dpkg/no-pie-link.specs -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -pie'
root@a4d0039eb9d3:/#
查看容器“Nginx_1”的Nginx的主配置文件:
root@a4d0039eb9d3:/# cat /etc/nginx/nginx.conf |grep -v "^#" |grep -v "^$"
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
root@a4d0039eb9d3:/#
查看Nginx的默认站点的默认首页文件:
root@a4d0039eb9d3:/# ls /etc/nginx/conf.d
default.conf
root@a4d0039eb9d3:/#
root@a4d0039eb9d3:/# cat /etc/nginx/conf.d/default.conf |grep -v '^$'
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
root@a4d0039eb9d3:/#
###
在宿主机上创建容器“Nginx_1”的Nginx的默认站点的默认首页文件:
[root@centos8 ~]# cd /opt/share/
[root@centos8 share]# echo 'Welcome to zhuohua.' > index.html
[root@centos8 share]#
创建容器“Nginx_1”与容器“PHP_1”的PHP测试页:
[root@centos8 share]# cat test.php
<?php
phpinfo();
?>
[root@centos8 share]#
创建容器“PHP_1”与容器“MariaDB_1”的数据库连接测试页:(PHP7.x的语法)
[root@centos8 share]# cat mysql.php
<?php
header("Content-Type:text/html;charset=utf-8");
$con=mysqli_connect('192.168.80.2','zhuohua','168')
OR die('远程MariaDB连接失败。');
echo '远程MariaDB连接正常。';
?>
[root@centos8 share]#
###
在容器“Nginx_1”上查看Nginx的默认站点里的文件:
root@a4d0039eb9d3:~# cd /usr/share/nginx/html
root@a4d0039eb9d3:/usr/share/nginx/html# ls
index.html mysql.php test.php
root@a4d0039eb9d3:/usr/share/nginx/html#
###
在容器“PHP_1”上查看目录/var/www/html里的文件:
root@91a2bf7af44b:/var/www/html# ls
index.html mysql.php test.php
root@91a2bf7af44b:/var/www/html#
注释:
因为在创建容器“Nginx_1”的时候,把宿主机的目录/opt/share挂载到容器的目录/usr/share/nginx/html;
因为在创建容器“PHP_1”的时候,把宿主机的目录/opt/share挂载到容器的目录/var/www/html;
所以这三个目录里的数据是共享的。
###
在宿主机的防火墙firewalld打开TCP 80端口:
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload
查看防火墙所有打开的端口:
[root@centos8 ~]# firewall-cmd --zone=public --list-ports
80/tcp
[root@centos8 ~]#
###
在客户端访问容器“Nginx_1”的Nginx的默认站点的默认首页文件:
http://192.168.168.154/
在客户端访问容器“Nginx_1”的Nginx的默认站点里的PHP测试页:
http://192.168.168.154/test.php
笺注:这是因为容器“Nginx_1”的Nginx本身无法解析PHP文件。
解决方法:(在宿主机上修改容器“Nginx_1”的Nginx的主配置文件)
[root@centos8 ~]# docker cp Nginx_1:/etc/nginx/nginx.conf /root
[root@centos8 ~]# vi nginx.conf
插入以下代码:
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm index.php;
}
# #当请求容器“Nginx_1”默认站点下PHP文件的时候,反向代理到容器“PHP_1”的php-fpm
location ~ \.php$ {
fastcgi_pass 192.168.80.3:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name;
include fastcgi_params;
}
}
在宿主机上,往容器“Nginx_01”上传修改好的文件:
[root@centos8 ~]# docker cp /root/nginx.conf Nginx_1:/etc/nginx/nginx.conf
[root@centos8 ~]#
再次,查看容器“Nginx_1”的Nginx的主配置文件:
root@a4d0039eb9d3:/# cat /etc/nginx/nginx.conf |grep -v "^#" |grep -v "^$"
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm index.php;
}
location ~ \.php$ {
fastcgi_pass 192.168.80.3:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name;
include fastcgi_params;
}
}
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
root@a4d0039eb9d3:/#
在宿主机重启容器“Nginx_1”:
[root@centos8 ~]# docker restart Nginx_1
Nginx_1
[root@centos8 ~]#
再次,在客户端访问容器“Nginx_1”的Nginx的默认站点里的PHP测试页:
###
在客户端通过容器“Nginx_1”的反向代理,从而访问容器“PHP_1”与容器“MariaDB_1”的数据库连接测试页:
http://192.168.168.154/mysql.php
错误:
Fatal error: Uncaught Error: Call to undefined function mysqli_connect() in /var/www/html/mysql.php:3 Stack trace: #0 {main} thrown in /var/www/html/mysql.php on line 3
这是因为容器“PHP_1”的PHP没有安装相关的MySQL扩展。
进入容器“PHP_1”安装相关的MySQL扩展:
root@91a2bf7af44b:~# docker-php-ext-install pdo_mysql
root@91a2bf7af44b:~# docker-php-ext-install mysql
root@91a2bf7af44b:~# docker-php-ext-install mysqli
root@91a2bf7af44b:~# docker-php-ext-install pdo
在宿主机重启容器“PHP_1”:
[root@centos8 ~]# docker restart PHP_1
PHP_1
[root@centos8 ~]#
再次,在客户端通过容器“Nginx_1”的反向代理,从而访问容器“PHP_1”与容器“MariaDB_1”的数据库连接测试页:
######
宿主机重启后,其实不用考虑容器的启动顺序;但也可以让三个容器都随宿主机的启动而启动:
[root@centos8 ~]# docker update --restart=always Nginx_1
Nginx_1
[root@centos8 ~]#
[root@centos8 ~]# docker update --restart=always PHP_1
PHP_1
[root@centos8 ~]#
[root@centos8 ~]# docker update --restart=always MariaDB_1
MariaDB_1
[root@centos8 ~]#
相关文章:
CentOS8_在Docker中安装LNMP(使用参数--link)
CentOS8_在Docker中安装MariaDB |