解释php中可变变量并举例说明(PHP Primary script unknown 解决方法总结)
解释php中可变变量并举例说明
PHP Primary script unknown 解决方法总结相信很多配置php环境的都遇到过这个恼人的问题:
- 浏览器访问php文件,返回来 File not found
- 查看/var/log/nginx/error.log ,有 “Primary script unknown”,类似如下:
|
2019/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: <a href= "http://www.example.com/" rel= "external nofollow" >www.example.com</a> |
原因只有两个,一个是php-fpm找不到php文件,一个是php-fpm没有权限读取和执行文件。
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 nginx adm 这行要改:
|
create 640 phpuser adm |
2) 目录和文件权限
php文件不必非得设为 777,让人怪担心的,只要是nginx和php-fpm运行用户可读写执行即可,一般可以770 。
php文件目录和文件样例:
|
drwxrwx--- 6 phpuser phpuser 4.0K 2019-01-03 13:09 /usr/share/nginx/html -rwxrwx--- 1 phpuser phpuser 40 2019-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 2018-07-10 15:42 /usr drwxr-xr-x. 86 root root 4.0K 2018-12-17 07:33 /usr/share/ drwxr-xr-x 4 root root 40 2018-12-17 08:06 /usr/share/nginx/ drwxrwx--- 6 phpuser phpuser 4.0K 2019-01-03 13:11 /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 终极解决方法的全部知识点内容,感谢大家对开心学习网的支持。
原文链接:https://www.cnblogs.com/panblack/p/php_primary_script_unknown_solution.html
- windows搭建php环境(windows 2008r2+php5.6.28环境搭建详细过程)
- php数组写法(php文件操作之文件写入字符串、数组的方法分析)
- php一直提示错误(关于php开启错误提示的总结)
- 深入理解php 变量(不常用但很实用的PHP预定义变量分析)
- php微信支付流程(PHP实现微信退款的方法示例)
- php中最常用的标记符(php中目录操作opendir、readdir及scandir用法示例)
- php中如何打开文件读文件(PHP通过文件保存和更新信息的方法分析)
- php 大文件切片(PHP大文件切割上传功能实例分析)
- php创建多个文件(PHP实现单文件、多个单文件、多文件上传函数的封装示例)
- php面对对象的三大特性(php中对象引用和复制实例分析)
- apache搭建php环境(Windows上安装Apache2、PHP5、MySQL5及与Resin配合实现多系统之整合)
- php最好的探针(php探针不显示内存解决方法)
- thinkphp5对接支付宝扫码支付(ThinkPHP框架下整合支付宝支付功能图文教程)
- phpstudy使用教程(phpstudy是什么)
- thinkphp框架案例(thinkphp5.1框架容器与依赖注入实例分析)
- php新建文件夹代码(php新建文件的方法实例)
- 痴情男神 吴彦祖 与妻子恋爱8年,结婚10年,家庭幸福美满(痴情男神吴彦祖)
- 成功破圈,小牛电动SQi强势开 跨(小牛电动SQi强势开)
- 挑战新国标电自天花板,九号机械师MMAX 110P深度体验(挑战新国标电自天花板)
- 《满江红》不要只当电影看,学生应该这样做(满江红不要只当电影看)
- 电影《民间怪谈录之走阴人》定档8月5日,开启一场中式惊悚之旅(电影民间怪谈录之走阴人定档8月5日)
- 原创图画书,以儿童视角讲述中国故事(以儿童视角讲述中国故事)
热门推荐
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9