相信很多配置php环境的都遇到过这个恼人的问题:

  • 浏览器访问php文件,返回来 File not found
  • 查看/var/log/nginx/error.log ,有 “Primary script unknown”,类似如下:
// :: [error] #: * FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream,
client: 1.2.3.4, server: localhost, request: "GET /index.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.example.com"

原因只有两个,一个是php-fpm找不到php文件,一个是php-fpm没有权限读取和执行文件

1. 找不到文件问题

nginx 的站点配置文件php段要这样:

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:
location ~ \.php$ {
        #root 路径配置必须要有,而且必须要写对(别笑,真的能写错)
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:;
fastcgi_index index.php;         #SCRIPT_FILENAME用$document_root,而不是具体路径
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

2. 权限问题

也是坑最多的。

1) 进程用户
nginx.conf 里的 user 配置要跟 php-fpm.d/www.conf 一致,比如都用 nginx,或者自定义用户 phpuser(再来句废话,这个用户需要提前建好)。
nginx.conf :

user phpuser;
worker_processes auto;

php-fpm.d/www.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 = phpuser
group = phpuser

nginx 和 php-fpm 进程/监听信息:

root       0.0  0.1    ?        Ss   1月02   : php-fpm: master process (/usr/local/etc/php-fpm.conf)
phpuser 0.0 0.1 ? S 1月02 : php-fpm: pool www
phpuser 0.0 0.1 ? S 1月02 : php-fpm: pool www
root 0.0 0.0 ? Ss : : nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
phpuser 0.0 0.7 ? S : : nginx: worker process
phpuser 0.0 0.7 ? S : : nginx: worker process
tcp 127.0.0.1: 0.0.0.0:* LISTEN /php-fpm: mast
tcp 0.0.0.0: 0.0.0.0:* LISTEN /nginx: master
tcp6 ::: :::* LISTEN /nginx: master

如果修改了 nginx 运行用户还必须要改些目录权限:

chown -R phpuser:phpuser /var/log/nginx
chown -R phpuser:phpuser /var/cache/nginx
chown -R phpuser:phpuser /usr/share/nginx/html

还有/etc/logrotate.d/nginx,create 640 nginx adm 这行要改:

create  phpuser adm

2) 目录和文件权限
php文件不必非得设为 777,让人怪担心的,只要是nginx和php-fpm运行用户可读写执行即可,一般可以770 。
php文件目录和文件样例:

drwxrwx---  phpuser phpuser .0K -- : /usr/share/nginx/html
-rwxrwx--- phpuser phpuser -- : /usr/share/nginx/html/phpinfo.php

这里有个深坑,对于使用其他目录放置php文件的很可能中招,就是 /path/to/phpfiles 的每一层目录都要允许 phpuser 访问,缺一层就会 Permission denied。

本例,/usr/share/nginx/html 之上的每一层目录,所有者都是root,都有 o+rx ,即所有人都有读取和执行权限(读取和执行权限是目录访问的根本),因此 phpuser 可以访问到 html 目录。

drwxr-xr-x.  root root         -- : /usr
drwxr-xr-x. root root .0K -- : /usr/share/
drwxr-xr-x root root -- : /usr/share/nginx/
drwxrwx--- phpuser phpuser .0K -- : /usr/share/nginx/html/

测试方法:

sudo -u phpuser ls -l /usr/share/nginx/html/

3) SELINUX
nginx/apache 网页文件的 selinux 上下文,如果更换目录需要配上。(在Cenots7+php7.3上测试,没有 selinux 上下文时,静态文件404,而php文件反倒没有遇到问题,没有深究)

# ll -dZ /usr/share/nginx/html
drwxr-xr-x. root root system_u:object_r:httpd_sys_content_t:s0 /usr/share/nginx/html

配置 selinux 上下文:

chcon -R -t httpd_sys_content_t /path/to/phpfiles

或者干脆关闭selinux(需要重启服务器)
/etc/selinux/config :

SELINUX=disabled

3. 最后

echo "<p align='center'>Good Luck :)</p><?php phpinfo(); ?>" > /usr/share/nginx/html/phpinfo.php

PHP Primary script unknown 终极解决方法的更多相关文章

  1. [PHP] 安装PHP报错“Primary script unknown”的解决方法

    当安装完最新版的nginx和最新版的PHP以后,使用niginx默认配置文件中的PHP配置会有错误访问指定目录下的php文件时,显示File not found.错误.查看nginx的错误日志显示 9 ...

  2. Nginx-Primary script unknown的报错的解决方法

    配置nginx时一直报错:file not found 错误日志: [error] 12691#0: *6 FastCGI sent in stderr: "Primary script u ...

  3. nginx FastCGI错误Primary script unknown解决办法

    在centos上成功编译安装nginx 1.4.php 5.4并成功启动nginx和php-fpm后,访问php提示"File not found.",同时在错误日志中看到: 复制 ...

  4. 解决nginx FastCGI sent in stderr: “Primary script unknown”

    今天重启了mac,突然发现本地的 lnmp 服务不能用了,什么请求都返回了: FastCGI sent in stderr: "Primary script unknown" 这个 ...

  5. 【nginx】 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream

    2013/10/22 20:05:49 [error] 12691#0: *6 FastCGI sent in stderr: "Primary script unknown" w ...

  6. FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream,

    在对nginx添加fastCGI的支持后,然后进行php页面验证,发现页面识别不到,不清楚什么情况,随后google了下,原来是Nginx内置变量问题惹的祸. 1.平台介绍: 1 2 3 4 5 6 ...

  7. Nginx+PHP配置错误,日志:[error] 24324#0: *31 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream

    一.问题现象 1.安装nginx.php.php-fpm后,浏览器访问php报错,“File not found”: 二.问题排查 1.检查nginx.php-fpm服务是否正常启动,均正常启动: 2 ...

  8. nginx+php-fpm 报错Primary script unknown

    报错信息(nginx日志): // :: [crit] #: * stat() : Permission denied), client: 172.21.205.25, server: localho ...

  9. Win8.1 Metro应用无法联网终极解决方法

    Win8.1 Metro应用无法联网终极解决方法: 一.删除注册表中:HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\WinSock2\Par ...

随机推荐

  1. sql update 代替游标写法

    update TB_AreaUserDevice_Relation set OrderID = t.r from TB_AreaUserDevice_Relation rel inner join ( ...

  2. Springboot 整合 中国移动MAS HTTP1.0 实现短信发送服务(二)

    原因:身份验证传入的参数包含中文企业名,因为本地编码格式是支持中文的:而客户的服务器中文却乱码,导致传给中国移动MAS服务器的是乱码的信息. 解决:非常简单,将中文信息转为UTF-8.例如(%E5%8 ...

  3. 用IDM下载博客图片

    前言 写博客的人一定都会有一个图床,将图片存在那里.发现自己以前没有注意图片来源问题,随手就贴在博客上面了.现在有不少图片都挂了,换句话来说有可能自己目前用的图床不提供服务了,那所有的图片都有可能丢失 ...

  4. α测试,Beta测试

    α测试(内测)是由一个用户在开发环境下进行的测试,也可以是公司内部的用户在模拟实际操作环境下进行的测试.α测试的目的是评价软件产品的FLURPS(即功能.局域化.可使用性.可靠性.性能和支持).尤其注 ...

  5. Android 程序调试技巧汇总

    1.Android Studio 引入一个项目作为library https://www.cnblogs.com/lixiangyang521/p/7453322.html 2.Android Stu ...

  6. Jupyter 安装与应用

    用pip安装Jupyter pip install jupyter 从命令行启动笔记本服务器 jupyter notebook 前提要先启动python,这里有一个 token值,如果不是使用默认浏览 ...

  7. 【总结】MYSQL注入

    关于MYSQL注入的总结,网上的资料很多,这里和大家简单分享下自己实战中常用的思路和命令 0x00   UNION联合查询型注入常用语句 order by n //定字段,n为正整数 union se ...

  8. Knime 连接 MYSQL 8

    mysql8 腾空出世,话说mysql 跨过 6 7 版本直迈8,对这个数据库有跨时代的意思,引擎机制有个革命性的变革.决定尝试一把. 用大数据ETL工具Knime抽取数据.结果尴尬了: ERROR ...

  9. html自定义垂直导航菜单(加强版--自定义传入menu参数,支持JSONArray、JSArray、JSONObject、JSObject)

    在上一篇中我简单写了个html自定义垂直导航菜单,缺点很明显,里面的数据是固定死的,不能动态更改数据. 这里我重写了一个修改版的垂直二级导航菜单,将原先的menuBox.init(config);修改 ...

  10. oracle 基础知识(八)----Library Cache *

    一,介绍 Library cache是Shared pool的一部分,它几乎是Oracle内存结构中最复杂的一部分,主要存放shared curosr(SQL)和PLSQL对象(function,pr ...