阅读目录
1 创建桥接网络2 安装nginx3 安装php容器4 配置nginx容器的default.conf文件5 参考
回到顶部↑
1 创建桥接网络
docker network create phpClassExamples_network
回到顶部↑
2 安装nginx
2.1 生成nginx临时容器
docker run -it --name test_nginx -d nginx
查看临时容器内部,找到关键目录
1、工作目录:
ls usr/share/nginx/html

2、配置目录
ls etc/nginx/conf.d

3、日志目录
ls var/log/nginx

2.2 复制nginx关键目录
退出临时容器。创建宿主机目录
cd / ;
mkdir myweb2 ;
cd myweb2 ;
mkdir {html,nginx,log,php} ;
cd nginx ;
mkdir conf.d ;
cd /myweb2/php ;
mkdir conf ;
cd /myweb2/log ;
mkdir nginx;
查看创建的目录结构
tree /myweb2

复制重要文件到宿主机
docker cp test_nginx:/usr/share/nginx/html /myweb2
docker cp test_nginx:/etc/nginx/conf.d /myweb2/nginx
如果不复制出来,配置文件会丢失 造成无法启动
然后卸磨杀驴,把这个测试容器删除
docker stop test_nginx
docker rm test_nginx
2.3 生成真正的nginx容器:phpClassicExamples_nginx
docker run --network phpClassExamples_network -p 8080:80 -p 443:443 --name phpClassicExamples_nginx --restart=always -v /myweb2/html:/usr/share/nginx/html -v /myweb2/nginx/conf.d:/etc/nginx/conf.d -v /myweb2/log/nginx:/var/log/nginx -d nginx:latest
备注:–restart=always 重启自动启动容器
8080 是宿主机端口;80是容器端口
443 用于https端口
容器名为 :phpClassicExamples_nginx
安装完即可访问静态网页

回到顶部↑
3 安装php容器
3.1 生成php临时容器
docker run -dit --name test_php php:7.4-fpm
查看临时容器内部,找到关键目录
/usr/loacl/etc 配置文件夹
/var/www/html 工作目录

3.2 复制php关键目录
把配置文件夹里的文件复制到宿主机目录 /myweb2/php/conf
docker cp test_php:/usr/local/etc /myweb2/php/conf

3.3 删除临时php容器
docker stop test_php
docker rm test_php
3.4 生成真正的php容器
docker run --network phpClassExamples_network -p 9001:9000 -itd --name phpClassicExamples_php -v /myweb2/html:/var/www/html -v /myweb2/php/conf:/usr/loacl/etc --restart=always php:7.4-fpm
3.5 安装php扩展
进入php容器
docker exec -it phpClassicExamples_php bash
安装扩展
docker-php-ext-install pdo pdo_mysql
3.6 简单配置
进入容器
locale

export LANG=en_US.UTF-8

编辑PHP.ini文件,设定默认字符集:
default_charset = “UTF-8”
回到顶部↑
4 配置nginx容器的default.conf文件
修改宿主机 /myweb2/nginx/conf.ddefault.conf文件 ,



完整代码如下


server {
listen 80; # 宿主机的端口
listen [::]:80;
#server_name localhost;
server_name 10.10.10.234 # 改成宿主机的ip
#charset koi8-r;
# 解决中文乱码
charset utf-8;
# 解决中文路径乱码
source_charset utf-8;#实际并没有用
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;# nginx容器的工作目录(绝对路径)
# index index.html index.htm;
# 将中文路径转换为utf-8编码
# try_files $uri $uri/ /index.html?$args;
# 以下三行为显示目录,相当于apache中的indexes
autoindex on; # 开启目录浏览功能;
autoindex_exact_size off; #关闭详细文件大小统计,让文件大小显示MB,GB单位,默认为b;
autoindex_localtime on; #开启以服务器本地时区显示文件修改日期!
}
#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;
#}
# 与php-fpm通信的关键设置
location ~ .php$ {
root /var/www/html; #php容器工作目录
fastcgi_pass phpClassicExamples_php:9000;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name; #改成php容器的工作目录
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /.ht {
# deny all;
#}
}
default.conf
然后重启nginx服务器
docker restart phpClassicExamples_nginx phpClassicExamples_php
回到顶部↑
5 参考
搭建php环境(踩坑经验!!)
docker搭建nginx、php、mysql等常用环境
基于Docker的Nginx部署教程_nginx使用docker部署应用,查漏补缺
1