操作系统 centos-release-7-7.1908.0.el7.centos.x86_64
安装PHP
docker pull php:fpm-7.4
安装Nginx
docker pull nginx:1.17
查看镜像
docker images [root@VM_1_201_centos ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE redis latest 96812ab2b931 14 hours ago 104MB mysql 5.7 f965319e89de 3 days ago 448MB redis 5.0 a4d3716dbb72 7 days ago 98.3MB php 7.4-fpm 9f53a362fb2f 7 days ago 405MB nginx 1.17 602e111c06b6 7 days ago 127MB rabbitmq 3.7.15 b3639fca0afd 10 months ago 149MB mongo 3.2 fb885d89ea5c 17 months ago 300MB kibana 6.4.0 a7e4cd1a7b45 20 months ago 667MB elasticsearch 6.4.0 1ac676545731 20 months ago 791MB [root@VM_1_201_centos ~]#
创建并启动php容器
docker run -p 9000:9000 --name php7 -v /mydata/nginx/html:/www -d php:7.4-fpm
创建启动nginx容器
docker run -p 80:80 --name nginx \ -v /mydata/nginx/html:/usr/share/nginx/html \ -v /mydata/nginx/logs:/var/log/nginx \ -d nginx:1.17
复制nginx容器内的默认配置文件到宿主主机
docker container cp nginx:/etc/nginx /mydata/nginx/
修改文件夹名称
cd /mydata/nginx/ mv nginx conf
停止并删除nginx容器,重新创建并启动nginx容器,关联php和挂载nginx配置文件数据卷
docker stop nginx docker rm nginx docker run -p 80:80 --name nginx \ -v /mydata/nginx/html:/usr/share/nginx/html \ -v /mydata/nginx/logs:/var/log/nginx \ -v /mydata/nginx/conf/conf.d:/etc/nginx/conf.d \ --link php7:php \ -d nginx:1.17
查看php容器的ip
docker inspect --format '{{.NetworkSettings.IPAddress}}' php7
配置nginx vhost文件
cd /mydata/nginx/conf/conf.d/ vim adamfly.conf server { listen 80; server_name www.adamfly.xyz; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; root /usr/share/nginx/html/www.adamfly.xyz; location / { index index.php index.html index.htm; } #error_page 404 /404.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$ { fastcgi_index index.php; fastcgi_pass 172.17.0.6:9000; include fastcgi_params; fastcgi_param SCRIPT_FILENAME /www/www.adamfly.xyz$fastcgi_script_name; fastcgi_param SCRIPT_NAME $fastcgi_script_name; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }
重启容器或者重新加载nginx配置
docker restart nginx 或者 docker exec -it nginx service nginx reload
需要注意的坑是nginx配置文件里面的fastcgi_pass是php容器ip,SCRIPT_FILENAME 开头是 /www/下的文件夹 否则会报file not found
本文为Adamin90原创文章,转载无需和我联系,但请注明来自http://www.lixiaopeng.top