nginx报错(Nginx报错502 bad gateway)
2024-06-02
更新时间:2024-06-02 00:11:55作者:未知
本文分享的是利用docker搭建php7和nginx运行环境的全过程,分享出来供大家参考学习,下面来看看详细的介绍:
环境介绍
根目录: /docker
网站根目录:/docker/www
nginx相关目录:/docker/nginx/conf.d
准备工作
1、使用docker加速器
curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://68abbefd.m.daocloud.io service docker restart
2、下载相关镜像
docker pull nginx docker pull php:7.1.0-fpm
3、建立相关目录
mkdir -p /docker/www mkdir -p /docker/nginx/conf.d
4、编辑default.conf
vim /docker/nginx/conf.d/default.conf # 以下为示例内容 server { listen 80 default_server; server_name _; root /usr/share/nginx/html; location / { index index.html index.htm index.php; autoindex off; } location ~ \.php(.*)$ { root /var/www/html/; fastcgi_pass 172.17.0.2:9000; fastcgi_index index.php; fastcgi_split_path_info ^((?U).+\.php)(/?.+)$; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; include fastcgi_params; } }
搭建环境
1、启动php镜像
docker run -p 9000:9000 --name myphp \ -v /docker/www/:/var/www/html/ \ --privileged=true \ -d php:7.1.0-fpm #查看php镜像的ip地址 docker inspect --format='{{.NetworkSettings.IPAddress}}' myphp 172.17.0.2 #修改default.conf配置文件,使fastcgi_pass的值为 172.17.0.2:9000 vim /docker/nginx/conf.d/default.conf fastcgi_pass 172.17.0.2:9000;
2、启动nginx镜像
docker run -p 80:80 --name mynginx \ -v /docker/www:/usr/share/nginx/html \ -v /docker/nginx/conf.d:/etc/nginx/conf.d \ --privileged=true \ -d nginx
3、查看镜像运行状态
docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 93213e1eac73 nginx "nginx -g 'daemon off" 3 seconds ago Up 2 seconds 0.0.0.0:80->80/tcp mynginx e93281652098 php:7.1.0-fpm "docker-php-entrypoin" 8 minutes ago Up 8 minutes 0.0.0.0:9000->9000/tcp myphp
4、生成php测试文件info.php
echo "<?php phpinfo();" > /docker/www/info.php
浏览器访问 http://localhost/info.php 验证
nginx虚拟机配置
以配置www.test.com虚拟机为例,项目目录地址为/docker/www/test.com/
vim /docker/nginx/conf.d/test.com.conf # 示例内容如下 server { listen 80; server_name www.test.com; root /usr/share/nginx/html/test.com/; location / { index index.html index.htm index.php; autoindex off; } location ~ \.php(.*)$ { root /var/www/html/test.com/; fastcgi_pass 172.17.0.2:9000; fastcgi_index index.php; fastcgi_split_path_info ^((?U).+\.php)(/?.+)$; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; include fastcgi_params; } } #重启nginx镜像 docker restart mynginx
docker常用命令
1、停止所有正在运行的容器
docker kill $(docker ps -a -q)
2、删除所有已停止运行的容器
docker rm $(docker ps -a -q)
3、查看容器运行状态
docker stats
4、进入容器内进行命令行操作
docker exec -it content-name-or-id /bin/bash
常见问题
CentOS7 环境下因为宿主的SELINUX,导致在nginx容器内无法访问配置文件(default.conf),进而容器无法提供web服务
解决方法:
#############方法一############# #在宿主主机关闭SELINUX #临时关闭 setenforce 0 #永久关闭 修改/etc/sysconfig/selinux文件 SELINUX=disabled #############方法二############# #以特权方式运行容器 #--privileged参数为true docker run -it --privileged=true -d nginx