当前位置: 首页 > Linux

PHPPrimaryscriptunknown终极解决方法

时间:2023-04-06 18:34:41 Linux

相信很多配置php环境的人都遇到过这个恼人的问题:浏览器访问php文件,返回Filenotfound查看/var/log/nginx/error.log,有是“未知主脚本”,类似于以下内容:2019/01/0310:24:02[error]11931#11931:*260FastCGIsentinstderr:"Primaryscriptunknown"whilereadingresponseheaderfromupstream,client:1.2.3.4,服务器:localhost,请求:“GET/index.phpHTTP/1.1”,上游:“fastcgi://127.0.0.1:9000”,主机:“www.example.com”原因只有两个,一种是php-fpm找不到php文件,一种是php-fpm没有读取和执行该文件的权限。1.找不到文件。nginx站点配置文件的php部分应该是这样的:#passthePHPscriptstoFastCGIserverlisteningon127.0.0.1:9000location~\.php${#rootpathconfigurationmusthave,andmustWriteitright(don‘笑,你还真能写错)root/usr/share/nginx/html;fastcgi_pass127.0.0.1:9000;fastcgi_indexindex.php;#SCRIPT_FILENAME使用$document_root而不是具体路径fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;包括fastcgi_params;}2。权限问题也是坑最多的。1)进程usernginx.conf中的用户配置要和php-fpm.d/www.conf中的一样,比如使用nginx,或者自定义用户phpuser(又是废话,这个用户需要创建提前)。nginx.conf:userphpuser;worker_processesauto;php-fpm.d/www.conf:;Unix用户/进程组;注意:用户是必填项。如果不设置组,则默认用户所在的组;将被使用。user=phpusergroup=phpusernginx和php-fpm进程/监听信息:root191070.00.12076445852?Ss1月02日0:03php-fpm:主进程(/usr/local/etc/php-fpm.conf)phpuser191080.00.12076447108?S1月02日0:00php-fpm:池wwwphpuser191090.00.12076447112?S1月02日0:00php-fpm:池wwwroot246760.00.0566601024?Ss13:080:00nginx:主进程/usr/sbin/nginx-c/etc/nginx/nginx.confphpuser246770.00.78468029976?S13:080:00nginx:workerprocessphpuser246780.00.78432429236?S13:080:00nginx:workerprocesstcp00127.0.0.1:90000.0.0.0:*LISTEN19107/php-fpm:masttcp000.0.0.0:800.0.0.0:*LISTEN24676/nginx:mastertcp600:::80:::*LISTEN24676/nginx:master如果修改nginx运行用户,还必须更改一些目录权限:chown-Rphpuser:phpuser/var/log/nginxchown-Rphpuser:phpuser/var/cache/nginxchown-Rphpuser:phpuser/usr/share/nginx/htmlandlogrotate.d/nginx,create640nginxadm这一行需要改成:create640phpuseradm2)目录和php文件的文件权限不必设置为777,令人担忧。只要nginx和php-fpm是运行用户,就可以读、写、执行。一般770个php文件目录和文件示例:drwxrwx---6phpuserphpuser4.0K2019-01-0313:09/usr/share/nginx/html-rwxrwx---1phpuserphpuser402019-01-0313:09/usr/share/nginx/html/phpinfo.php这里一个深坑,对于那些使用其他目录放置php文件的人,很有可能被坑,就是/path/to中的每一层目录/phpfiles必须允许phpuser访问,如果少了一层,Permissiondenied。本例中/usr/share/nginx/html上面每个目录的属主都是root,并且有o+rx,即所有人都有读取和执行权限(读取和执行权限是目录访问root),所以phpuser可以访问html目录。drwxr-xr-x.13rootroot1552018-07-1015:42/usrdrwxr-xr-x.86rootroot4.0K2018-12-1707:33/usr/share/drwxr-xr-x4rootroot402018-12-1708:06/usr/share/nginx/drwxrwx---6phpuserphpuser4.0K2019-01-0313:11/usr/share/nginx/html/测试方法:sudo-uphpuserls-l/usr/share/nginx/html/3)SELINUXnginx/apache网页文件的selinux上下文,如果换目录需要匹配。(在Cenots7+php7.3上测试,在没有selinux上下文的情况下,静态文件404,但是php文件没有问题,不深究)#ll-dZ/usr/share/nginx/htmldrwxr-xr-x。rootrootsystem_u:object_r:httpd_sys_content_t:s0/usr/share/nginx/html配置selinux上下文:chcon-R-thttpd_sys_content_t/path/to/phpfiles或者直接关闭selinux(需要重启服务器)/etc/selinux/config:SELINUX=disabled3.finallyecho"GoodLuck:)

">/usr/share/nginx/html/phpinfo.php