docker镜像
docker pull nginx:1.21.5
docker pull php:7.4-fpm
创建docker网络
docker network create test
宿主机准备两个站点目录
/root/web1
/root/web2
运行php-fpm容器
docker run -d --name php-web1 --net test -v /root/web1:/var/www/html php:7.4-fpm
docker run -d --name php-web2 --net test -v /root/web2:/var/www/html php:7.4-fpm
docker run 参数说明:
-d 后台运行
--name 指定容器名称
--net 加入docker网络,同一网络内,通过容器名称可互相访问
-v 挂在容器数据卷,将宿主机/root/web挂在到容器内/var/www/html
运行nginx容器
# 运行一个空容器,目的:拷贝配置文件
docker run --name nginx nginx:1.21.5
docker cp nginx:/etc/nginx/conf.d /root/nginx
# 删除容器
docker rm -f nginx
# 运行nginx
docker run --name nginx -d --net test \
-p 8081:81 -p 8082:82 \
-v /root/web1:/data/web1 \
-v /root/wew2:/data/web2 \
-v /root/nginx/conf.d:/etc/nginx/conf.d \
--restart always nginx:1.21.5
# 参数说明
# -p 8081:81 将宿主机8081端口映射到容器81端口
# --restart always:docker服务重启后,自动启动该容器
nginx配置
切换至/root/nginx/conf.d目录,复制default.conf并改名为web1.conf
server {
listen 81; # 监听端口
listen [::]:81;
server_name localhost; # 域名
location / {
root /data/web1; # web访问目录
index index.php index.html index.htm; # 首页
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
include fastcgi_params;
# uastcgi_pass 127.0.0.1:9000; 这是个坑,uastcgi_pass模块不存在
fastcgi_pass php-web1:9000; # php-web1容器名称
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name;
}
}
web2.conf
server {
listen 82; # 监听端口
listen [::]:82;
server_name localhost; # 域名
location / {
root /data/web1/public; # web访问目录
index index.php index.html index.htm; # 首页
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
include fastcgi_params;
# uastcgi_pass 127.0.0.1:9000; 这是个坑,uastcgi_pass模块不存在
fastcgi_pass php-web2:9000; # php-web2容器名称
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html/public/$fastcgi_script_name;
}
}
注意:web1.conf和web2.conf的区别
- 端口
- web访问目录
-
fastcgi_pass
- fastcgi_param SCRIPT_FILENAME
配置完成后,重启nginx容器:docker restart nginx