docker架构下LNMP环境遇到 index.php 报错“File not found.“

docker架构下LNMP环境遇到 index.php 报错"File not found."


同步发布在个人笔记docker架构下LNMP环境遇到 index.php 报错"File not found."

1.0 问题描述

在搭建服务器时,很多时候都会部署 LNMP (Linux+Nginx+Mysql+PHP) 环境,如果是把环境直接安装在服务器上,则不容易出现此问题,但是如果是通过 Docker 分别搭建的 LNMP 组件(步骤),那么在配置 web 服务时,则有可能出现下面的问题。

问题:当需要部署多个 web 应用,尤其是需要增加一个 web 应用时,有时需要在 nginx/conf.d 中新配置一个根目录,用以部署代码资源。如果此时忘了对 php 进行根目录的配置,就会使得 index.html 等 HTML 资源可以被找到访问,而 index.php 等 PHP 资源则会在浏览器报错 File not found.

2.0 分析与解决

2.1 分析

我们看一个典型的 nginx 配置文件。

server {
    listen       your_port;
    listen  [::]:your_port;
    server_name  localhost;

    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   web_root_path;
        index  index.php 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:php_port
    #
    location ~ \.php$ {
         root   web_root_path;
         fastcgi_pass   localhost:php_port;
         fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
         fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
	 fastcgi_param  SCRIPT_NAME      $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;
    #}
}

其中和我们的资源根目录相关的是这两处代码:

	# Web root path
    location / {
        root   web_root_path;		# <---- WATCH THIS 
        index  index.php index.html index.htm;
    }

 
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:php_port
    #
    location ~ \.php$ {
         root   web_root_path;		# <---- WATCH THIS
         fastcgi_pass   localhost:php_port;
         fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
         fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
	 fastcgi_param  SCRIPT_NAME      $fastcgi_script_name;
         include        fastcgi_params;
    }

在 docker 配置的环境下,为了增加一个根目录,我们一般会想起来为 nginx 容器新加一个挂载,把物理机的某个目录挂载在容器的 web_root_path 下,这一步保证了 index.html 等 HTML 资源可以被访问到;
但是如果忘了把物理机目录同样挂载在 PHP 容器的 web_root_path 下的话,就会导致 index.php 等 PHP 资源不能被找到,从而报错 File not found.

2.2 解决

解决方式就是注意要对 nginx 和 PHP 容器,均配置新的 根目录挂载。
以 docker compose 搭建的 LNMP 为例,要在 docker-compose.yml 中在 nginx 和 PHP 两个部分添加新的挂载条目:

# Creat LNMP + phpmyadmin containers
version: '3'
services:
  php-fpm:
    image: php:7.4-fpm
    container_name: php
    restart: unless-stopped
    volumes:
      - old_path ... 
      - server_path:web_root_path		# <---- WATCH THIS 
    ports:
      - php_port_set
  
  nginx:
    image: nginx
    container_name: nginx
    restart: unless-stopped
    depends_on:
      - php-fpm
    volumes:
      - old_path ...
      - server_path:web_root_path		# <---- WATCH THIS 
    ports:
      - nginx_port_set

只需要关注标注的两条 volumes 配置代码。他们把物理机上的 web 资源目录 server_path 同时挂载在了 nginx 和 PHP 容器内部。这保证了 HTML 和 PHP 资源均可以被访问到。
修改好 yaml 文件后执行:

docker compose down			# Destroy old LNMP  
docker compose up -d		# Create new LNMP

此时再去访问 index.php 就发现可以成功访问到,而不会报错了。

  • 7
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值