解决CENTOS下NGINX运行PHP显示FILE NOT FOUND的方法

最近配置 LNMP 网站的时候遇到了 php-fpm 总是返回“File not found”的错误,Nginx 的日志文件中的记录是:

1
FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream

上网搜索解决方法,发现大部分这个问题都是由于脚本路径设置错误引起的。解决方法是:把 fastcgi_param 设置为:

1
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

可是我明明已经像这样配置了 Nginx,更奇怪的是,有时 Nginx 的错误日志里会显示这样的错误:

1
2
FastCGI sent in stderr: "Unable to open primary script:
/var/www/html/test/index.php (No such file or directory)"

路径都显示出来了,这个文件明明是存在的啊,为什么显示没有此文件呢?找了很多网上的回答,找到了这一篇:

SELinux will cause this error on CentOS/RHEL 7+ by default 🙁

To test if SELinux is the source of your woes, do

setenforce 0

… and see if everything works. If that fixed it, you can leave SELinux off (weak, you’re better than that), or you can turn it back on with

setenforce 1

… and then properly fix the issue.

If you do

tail -f /var/log/audit/audit.log

… you’ll see the SELinux issue. In my case, it was denying PHP-FPM access to web files. You can run the following directives to fix it:

setsebool -P httpd_can_network_connect_db 1
setsebool -P httpd_can_network_connect 1

This actually didn’t fix it for me at first, but then restoring SELinux context did it

restorecon -R -v /var/www

Hope that helps.

作者:匿名用户
链接:https://www.zhihu.com/question/22128267/answer/105600681
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

就是执行“setenforce 0”来判断这个错误是否是 selinux 引起的(最好再执行“setenforce 1”来还原设置,可能是出于安全的原因?),如果是的话就执行

1
2
3
setsebool -P httpd_can_network_connect_db 1
setsebool -P httpd_can_network_connect 1
restorecon -R -v /var/www

来解决问题,注意“/var/www”要换成网站文件所在的目录。


解决CENTOS下NGINX运行PHP显示FILE NOT FOUND的方法
https://maphical.cn/2017/11/deal-with-php-file-not-found/
作者
MaphicalYng
发布于
2017年11月11日
许可协议