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 就发现可以成功访问到,而不会报错了。

相关推荐

  1. Ubuntu conda创建环境失败相关问题

    2024-07-23 05:26:05       32 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-23 05:26:05       52 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-23 05:26:05       54 阅读
  3. 在Django里面运行非项目文件

    2024-07-23 05:26:05       45 阅读
  4. Python语言-面向对象

    2024-07-23 05:26:05       55 阅读

热门阅读

  1. ElasticSearch-分词器介绍

    2024-07-23 05:26:05       15 阅读
  2. Git基本使用

    2024-07-23 05:26:05       20 阅读
  3. 深入理解Spring Boot中的Fastjson

    2024-07-23 05:26:05       16 阅读
  4. 【Golang 面试基础题】每日 5 题(四)

    2024-07-23 05:26:05       15 阅读
  5. vue中预览视频,word,pdf,xlsx

    2024-07-23 05:26:05       15 阅读
  6. service

    service

    2024-07-23 05:26:05      14 阅读
  7. vue3 页面引入组件

    2024-07-23 05:26:05       17 阅读