Primary script unknown 错误概述
在配置 PHP 运行环境的过程中,许多开发者都曾遭遇过令人头疼的故障现象。
- 通过浏览器访问 PHP 脚本时,页面提示 File not found。
- 检查 /var/log/nginx/error.log 日志文件,通常会发现包含 “Primary script unknown” 的错误记录,具体示例如下:
2026/01/03 10:24:02 [error] 11931#11931: *260 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 进程缺乏读取或执行该文件的系统权限。
Nginx 配置文件路径设置
1. 文件路径查找问题
Nginx 站点配置文件中关于 PHP 处理的代码块应遵循以下规范:
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ { #root 路径配置必须要有,而且必须要写对(别笑,真的能写错)
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
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 19107 0.0 0.1 207644 5852 ? Ss 1月02 0:03 php-fpm: master process (/usr/local/etc/php-fpm.conf)
phpuser 19108 0.0 0.1 207644 7108 ? S 1月02 0:00 php-fpm: pool www
phpuser 19109 0.0 0.1 207644 7112 ? S 1月02 0:00 php-fpm: pool www
root 24676 0.0 0.0 56660 1024 ? Ss 13:08 0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
phpuser 24677 0.0 0.7 84680 29976 ? S 13:08 0:00 nginx: worker process
phpuser 24678 0.0 0.7 84324 29236 ? S 13:08 0:00 nginx: worker process
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 19107/php-fpm: mast
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 24676/nginx: master
tcp6 0 0 :::80 :::* LISTEN 24676/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 phpuser adm 这行要改:
create 640 phpuser adm
2) 目录与文件权限
PHP 文件无需设置为 777,只要确保 Nginx 和 PHP-FPM 的运行用户具备读写执行权限即可,通常设置为 770 较为合适。
PHP 文件及其目录的权限样例如下:
drwxrwx--- 6 phpuser phpuser 4.0K 2026-01-03 13:09 /usr/share/nginx/html
-rwxrwx--- 1 phpuser phpuser 40 2026-01-03 13:09 /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. 13 root root 155 2026-07-10 15:42 /usr
drwxr-xr-x. 86 root root 4.0K 2026-12-17 07:33 /usr/share/
drwxr-xr-x 4 root root 40 2026-12-17 08:06 /usr/share/nginx/
drwxrwx--- 6 phpuser phpuser 4.0K 2026-01-03 13:11 /usr/share/nginx/html/
测试验证方法:
sudo -u phpuser ls -l /usr/share/nginx/html/
SELinux 上下文处理方案
3) SELinux 安全策略
对于 Nginx/Apache 托管的网页文件,若更换了存放目录,需要配置相应的 SELinux 上下文。(在 CentOS 7 + PHP 7.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 "Good Luck :)
<?php phpinfo(); ?>" > /usr/share/nginx/html/phpinfo.php
以上即为解决 PHP Primary script unknown 错误的完整知识点,感谢对本站的支持。