一、容器运行:
# 1、nginx
mkdir /docker/nginx
cd /docker/nginx && mkdir conf.d && mkdir www && mkdir logs
# 不带映射文件启动容器
docker run -d --name nginx -p 99:80 nginx
# 主配置文件
docker cp nginx:/etc/nginx/nginx.conf ./
# 其他配置文件
docker cp nginx:/etc/nginx/conf.d/default.conf ./conf.d/
# 停止运行并删除容器
docker stop nginx && docker rm nginx
# 启动容器 (--privileged=true 让容器内的root拥有真正的root权限,对挂载的目录有读写权限)
docker run -d --name nginx -p 99:80 \
-v /docker/nginx/nginx.conf:/etc/nginx/nginx.conf \
-v /docker/nginx/conf.d:/etc/nginx/conf.d \
-v /docker/nginx/logs:/var/log/nginx \
-v /docker/nginx/www:/www \
-v /etc/localtime:/etc/localtime:ro \
--restart=always --privileged=true nginx
2、php (与 nginx 共同挂载到一个文件夹下)
docker run -d --name php -p 9100:9000 \
-v /docker/nginx/www:/usr/share/nginx/www \
--restart=always --privileged=true php:7.4.24-fpm
二、nginx 与 php 环境搭建注意事项:
1、首先确保 nginx 与 php 运行用户一致
[root@/docker/nginx #395]# ps aux | grep nginx
root 22165 0.0 0.1 11524 3880 ? Ss 14:55 0:00 nginx: master process nginx -g daemon off;
www-data 22216 0.0 0.0 11528 2296 ? S 14:55 0:00 nginx: worker process
www-data 22217 0.0 0.0 11528 1812 ? S 14:55 0:00 nginx: worker process
root 22510 0.0 0.0 112832 980 pts/0 S+ 14:59 0:00 grep --color=auto nginx
[root@/docker/nginx #396]# ps aux | grep php
root 17084 0.0 0.3 73196 11724 ? Ss 14:07 0:00 php-fpm: master process (/usr/local/etc/php-fpm.conf)
www-data 17116 0.0 0.2 147160 10428 ? S 14:07 0:01 php-fpm: pool www
www-data 17117 0.0 0.2 147160 10396 ? S 14:07 0:01 php-fpm: pool www
root 22547 0.0 0.0 112832 976 pts/0 S+ 14:59 0:00 grep --color=auto php
如果不一致,先查看 php 用户在容器内的 uid 和 gid ,再对 nginx 容器 和 linux 的用户进行修改。
2、配置文件注意事项
server
{
listen 80;
server_name pic.hausen.top;
index index.php index.html index.htm default.php default.htm default.html;
root /www/pic/public; # 一定要填写在 nginx 容器内的路径,index.php 所在位置
access_log /var/log/nginx/pic.hausen.top.log main;
error_log /var/log/nginx/pic.hausen.top.error.log debug;
location ~ .*\.php$ {
fastcgi_pass 172.17.0.2:9000; # 获得php 容器的 ip --> docker inspect php | grep "IPAddress"
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/www/pic/public$fastcgi_script_name; #路径一定要写对,该项目在 php 容器的路径(nginx 与 php 共同挂载一个目录)
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
include fastcgi_params;
}
}
3、文件权限
这里没有测试,有点迷糊。
不知道 --privileged=true 有没有效果,不过最好将需要的文件夹都做如下配置。
chown -R www-data:www-data xxx
chmod -R 770 xxx
4、报错相关
1)Primary script unknown
所有 php 文件都不能访问
- 文件不存在
- 文件存在,但路径错误。上面配置 SCRIPT_FILENAME 一定要好好检查,是 php 容器的路径。
- php-fpm 与nginx不能通信,重点检查 fastcgi_pass 地址和端口
只有某个文件夹下的 php 文件不能访问
- 在 root 目录下,有 .user.ini 文件,将其删除。 从宝塔复制过来的项目带有这个文件,如果不删除,死活报这个错。
2)directory index of “xxx” is forbidden
可能是配置文件没有设置 index,添加代码后如下:
server {
listen 80;
server_name pic.hausen.top;
index index.php index.html index.htm default.php default.htm default.html; # 需要配置
root /www/pic/public;
.....
}
5、fastcgi_param 配置解释
fastcgi_param QUERY_STRING $query_string; #请求的参数;如?app=123
fastcgi_param REQUEST_METHOD $request_method; #请求的动作(GET,POST)
fastcgi_param CONTENT_TYPE $content_type; #请求头中的Content-Type字段
fastcgi_param CONTENT_LENGTH $content_length; #请求头中的Content-length字段。
fastcgi_param SCRIPT_NAME $fastcgi_script_name; #脚本名称
fastcgi_param REQUEST_URI $request_uri; #请求的地址不带参数
fastcgi_param DOCUMENT_URI $document_uri; #与$uri相同。
fastcgi_param DOCUMENT_ROOT $document_root; #网站的根目录。在server配置中root指令中指定的值
fastcgi_param SERVER_PROTOCOL $server_protocol; #请求使用的协议,通常是HTTP/1.0或HTTP/1.1。
fastcgi_param GATEWAY_INTERFACE CGI/1.1; #cgi 版本
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version; #nginx 版本号,可修改、隐藏
fastcgi_param REMOTE_ADDR $remote_addr; #客户端IP
fastcgi_param REMOTE_PORT $remote_port; #客户端端口
fastcgi_param SERVER_ADDR $server_addr; #服务器IP地址
fastcgi_param SERVER_PORT $server_port; #服务器端口
fastcgi_param SERVER_NAME $server_name; #服务器名,域名在server配置中指定的server_name
fastcgi_param PATH_INFO $path_info; #可自定义变量
-- PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;