当前位置: 首页> 健康> 母婴 > docker架构下LNMP环境遇到 index.php 报错“File not found.“

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

时间:2025/7/29 3:13:50来源:https://blog.csdn.net/Lichen0196/article/details/140579750 浏览次数:0次

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

  • 1.0 问题描述
  • 2.0 分析与解决
    • 2.1 分析
    • 2.2 解决

同步发布在个人笔记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 pathlocation / {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 THISfastcgi_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-fpmcontainer_name: phprestart: unless-stoppedvolumes:- old_path ... - server_path:web_root_path		# <---- WATCH THIS ports:- php_port_setnginx:image: nginxcontainer_name: nginxrestart: unless-stoppeddepends_on:- php-fpmvolumes:- 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 就发现可以成功访问到,而不会报错了。

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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: