nginx+php-fpm出现file not found错误

最近有朋友在搭建lnmp环境的时候,访问php文件,一直提示 File not found. ,这里记录下解决方法,帮助其他遇到同样问题的小伙伴。

遇到这种问题,一般就是两种情况:

  • nginx的配置文件不对

    在nginx.conf中,我们能看到一段被注释掉的默认配置

    location ~ \.php$ {
                root           html;
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
                include        fastcgi_params;
            }

    我们将这段配置注释去掉,并修改成下面这段

    location ~ \.php$ {
                root           html;
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
                include        fastcgi_params;
            }
  • php-fpm运行的用户与nginx的不一致

    假定nginx当前运行用户是 root ,我们修改下 nginx.conf 的 user 配置。

    user root;

    同时也需要将 php-fpm 的运行用户改成 root ,相关配置在 php-fpm.conf 中。

    ; Unix user/group of processes
    ; Note: The user is mandatory. If the group is not set, the default user's group
    ;       will be used.
    user = root
    group = root

    配置修改后,记得重启相关的服务,问题就解决了。