失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > centos7 nginx php整合 Centos7下 宿主机nginx配合docker环境的php-fpm

centos7 nginx php整合 Centos7下 宿主机nginx配合docker环境的php-fpm

时间:2024-05-24 22:16:33

相关推荐

centos7 nginx php整合 Centos7下 宿主机nginx配合docker环境的php-fpm

一.安装docker并启动

yum install docker

systemctl start docker

二.安装nginx

CentOS 7默认不能从yum中安装nginx,原因可以自己搜索一下,需要执行以下命令

sudo rpm -Uvh /packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

之后就可以用yum安装了

yum install nginx

三.用docker拉取php-fpm

因为是老项目,不支持php7,所以用的php5.6的fpm镜像

docker pull phpdockerio/php56-fpm

四.运行phpfpm

docker run --name php56fpm -v /var/www/html/xx/www/:/var/www/html/ --privileged=true -d phpdockerio/php56-fpm

解释:

docker run

运行镜像

--name php56fpm

将运行的容器命名为php56fpm

-v /var/www/html/xx/www/:/var/www/html/

映射本地目录到容器内部的/var/www/html/,php-fpm容器内部会通过这个目录访问PHP文件.

--privileged=true

增加特权,不然没有权限访问/var/www/html/这里的文件,会导致nginx也无法访问文件(File not found.).(共三种方法,见PS.1)

-d

后台运行

phpdockerio/php56-fpm

镜像的名字

可以不映射端口,如果需要映射,增加参数-p 9001:9000 本地的9001端口映射到容器内部的9000端口,但nginx只用内部的9000端口就够了.

五.修改宿主机的nginx配置

vi /etc/nginx/conf.d/xx.conf

location ~ \.php${

root/var/www/html/xx/www;

fastcgi_pass172.17.0.2:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME/var/www/html/$fastcgi_script_name;

include fastcgi_params;

}

其中的fastcgi_pass和fastcgi_param和平时配置nginx的时候有所区别,

fastcgi_pass要指定容器的IP,通过 docker inspect php56fpm 可以查看具体IP("IPAddress": "172.17.0.2")

fastcgi_param SCRIPT_FILENAME 后面的值要改成容器内部的地址/var/www/html/

PS.1

在Cent OS 7中运行,如果不加--privileged=true,则会出现nginx没有访问内部文件的权限

原因是CentOS7中的安全模块selinux把权限禁掉了,至少有以下三种方式解决挂载的目录没有权限的问题:

1,在运行容器的时候,给容器加特权:--privileged=true

2,临时关闭selinux:

su -c "setenforce 0"

3,添加selinux规则,将要挂载的目录添加到白名单:

chcon -Rt svirt_sandbox_file_t /var/www/html/xx/www/

PS.2

如果遇到明明没用开启端口却提示端口占用,有可能是之前删除的容器还没完全退出,重启一下docker就好了

比如提示:Error response from daemon: driver failed programming external connectivity......

iptables failed: iptables --wait -t nat -A DOCKER

如果觉得《centos7 nginx php整合 Centos7下 宿主机nginx配合docker环境的php-fpm》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。