运行php-fpm容器:
docker run -d \
--name php-fpm \
--restart=always \
-p 59000:9000 \
-v /home/docker/nginx/html:/var/www/html \
php:7.3.5-fpm-alpine3.9 \
php-fpm
/home/docker/nginx/html是宿主机映射到nginx卷对应的目录。
找到nginx的默认配置文件conf.d/defautl.conf:
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 192.168.56.100:59000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html/$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;
#}
}
注意:# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000之后的配置节被注释了,取消注释,并对配置作如下修改:
fastcgi_pass
改为php-fpm的侦听地址和端口,这里192.168.56.100是宿主机的地址,59000是宿主机的映射端口,参看上面的容器运行指令。
fastcgi_param SCRIPT_FILENAME
改为php-fpm容器内部存放web文件的目录+脚本文件名,这里是/var/www/html/$fastcgi_script_name,注意/var/www/html是php-fpm容器内对应的web文件存放目录。