nginx報(bào)錯(cuò)(NGINX報(bào)錯(cuò)提示502)
2024-06-02
更新時(shí)間:2024-06-02 00:09:32作者:未知
本文分享的是利用docker搭建php7和nginx運(yùn)行環(huán)境的全過程,分享出來供大家參考學(xué)習(xí),下面來看看詳細(xì)的介紹:
環(huán)境介紹
根目錄: /docker
網(wǎng)站根目錄:/docker/www
nginx相關(guān)目錄:/docker/nginx/conf.d
準(zhǔn)備工作
1、使用docker加速器
curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://68abbefd.m.daocloud.io service docker restart
2、下載相關(guān)鏡像
docker pull nginx docker pull php:7.1.0-fpm
3、建立相關(guān)目錄
mkdir -p /docker/www mkdir -p /docker/nginx/conf.d
4、編輯default.conf
vim /docker/nginx/conf.d/default.conf # 以下為示例內(nèi)容 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; } }
搭建環(huán)境
1、啟動(dòng)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、啟動(dòng)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、查看鏡像運(yùn)行狀態(tài)
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測(cè)試文件info.php
echo "<?php phpinfo();" > /docker/www/info.php
瀏覽器訪問 http://localhost/info.php 驗(yàn)證
nginx虛擬機(jī)配置
以配置www.test.com虛擬機(jī)為例,項(xiàng)目目錄地址為/docker/www/test.com/
vim /docker/nginx/conf.d/test.com.conf # 示例內(nèi)容如下 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、停止所有正在運(yùn)行的容器
docker kill $(docker ps -a -q)
2、刪除所有已停止運(yùn)行的容器
docker rm $(docker ps -a -q)
3、查看容器運(yùn)行狀態(tài)
docker stats
4、進(jìn)入容器內(nèi)進(jìn)行命令行操作
docker exec -it content-name-or-id /bin/bash
常見問題
CentOS7 環(huán)境下因?yàn)樗拗鞯腟ELINUX,導(dǎo)致在nginx容器內(nèi)無法訪問配置文件(default.conf),進(jìn)而容器無法提供web服務(wù)
解決方法:
#############方法一############# #在宿主主機(jī)關(guān)閉SELINUX #臨時(shí)關(guān)閉 setenforce 0 #永久關(guān)閉 修改/etc/sysconfig/selinux文件 SELINUX=disabled #############方法二############# #以特權(quán)方式運(yùn)行容器 #--privileged參數(shù)為true docker run -it --privileged=true -d nginx