当前位置:网站首页>记录一次,php程序访问系统文件访问错误的问题
记录一次,php程序访问系统文件访问错误的问题
2022-07-27 01:08:00 【jacklin_001】
运行环境centos lnmp
问题:
测试脚本直接运行可以正常获取系统目录里的文件
通过浏览器运行不能获取系统目录文件
**Warning**: require(): open_basedir restriction in effect. File(/home/wwwroot/ssmanager/vendor/autoload.php) is not within the allowed path(s): (/home/wwwroot/ssmanager/public/:/tmp/:/proc/) in **/home/wwwroot/ssmanager/public/index.php**on line **24**
**Warning**: require(/home/wwwroot/ssmanager/vendor/autoload.php): failed to open stream: Operation not permitted in **/home/wwwroot/ssmanager/public/index.php** on line **24**
**Fatal error**: require(): Failed opening required '/home/wwwroot/ssmanager/public/../vendor/autoload.php' (include_path='.:/usr/local/php/lib/php') in **/home/wwwroot/ssmanager/public/index.php** on line **24**
解决方法:
1、查是否有权限限制,通过修改文件目录属组属主和给定权限
chown 和chmod
2、有关nginx fastcgi设置问题
解决方法
找到Nginx中的fastcgi.conf配置文件,一般位于fastcgi.conf文件中
/usr/local/nginx/conf/fastcgi.conf
打开文件找到如下部分
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
fastcgi_param PHP_ADMIN_VALUE "open_basedir=$document_root/:/tmp/:/proc/";
将其$document_root变量修改为项目目录(或者删掉这行),例如我现在的项目是ssmanager(Laravel项目),只需将$document_root修改为ssmanager所在的目录。
fastcgi_param PHP_ADMIN_VALUE "open_basedir=/home/wwwroot/ssmanager/:/tmp/:/proc/";
重启Nginx服务器
/etc/init.d/nginx restart
错误分析
fastcgi_param是什么
fastcgi_param属于ngx_http_fastcgi_module模块的参数,用于设置传给FastCgi服务器的参数及设置PHP配置项,其中包含用户访问IP等及一些其他的Webserver信息;通过PHP_ADMIN_VALUE选项可以给PHP进行配置
(自PHP 5.3.3 起,可以通过 web 服务器设置 PHP 的配置)PHP官方文档
fastcgi_param 语法:
名称 选项
Syntax fastcgi_param parameter value [if_not_empty]
Default -
Context http, server, location
其中value可以包含文本,变量及其组合。仅当在当前级别上没有定义fastcgi_param指令时,定义的指令才可以从前一级继承。
下面列出一些常规参数,可以看出fastcgi_param的具体用法
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REQUEST_SCHEME $scheme;
fastcgi_param HTTPS $https if_not_empty;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
fastcgi_param PHP_ADMIN_VALUE "open_basedir=$document_root/:/tmp/:/proc/";通过观察可以看出,我们在使用PHP中的$_SERVER[SERVER_ADDR]获取WEBSERVER相关参数时,其实就是与fastcgi_param的参数对应的。
在编译 PHP 时添加--enable-force-cgi-redirect选项,REDIRECT_STATUS和PHP_ADMIN_VALUE则会生效
PHP_ADMIN_VALUE
PHP_ADMIN_VALUE 为一个运行池传递附加的环境变量,或者更新 PHP 的配置值
在FPM中,可以使用不同的设置来运行多个进程池。 这些设置可以针对每个进程池单独设置。
还可以在为一个运行池传递附加的环境变量,或者更新 PHP 的配置值。PHP官方文档
可以得出 Nginx 通过制定 PHP_ADMIN_VALUE 来给 PHP 设定 open_basedir 配置项
OPEN_BASEDIR
open_basedir作用是将 PHP 所能打开的文件限制在指定的目录树中,包括文件本身。
当一个脚本试图用例如 fopen() 或者 gzopen() 打开一个文件时,该文件的位置将被检查。当文件在指定的目录树之外时 PHP 将拒绝打开它。所有的符号连接都会被解析,所以不可能通过符号连接来避开此限制。
其实open_basedir有些注意的地方
open_basedir 指定的限制实际上是前缀,不是目录名。也就是说“open_basedir = /dir/incl”也会允许访问“/dir/include”和“/dir/incls”,如果它们存在的话。如果要将访问限制在仅为指定的目录,用斜线结束路径名。例如:“open_basedir = /dir/incl/”
总结
知道了fastcgi_param,PHP_ADMIN_VALUE,open_basedir是什么,就可以知道问题出现在, Nginx 中使用 PHP_ADMIN_VALUE 配置了 PHP 的用于控制PHP自身能打开目录树的open_basedir选项,想要解决问题,可以通过指定对应目录树或者删除此配置即可(简单粗暴)。
open_basedir php授权目录设置
php为了安全性考虑,有一项 open_basedir 的设置。根据你web服务器环境,open_basedir可以在几个地方设置。
首先 在php.ini中配置。
;open_basedir =
fastcgi_param PHP_ADMIN_VALUE "open_basedir=$document_root:/tmp/:/proc/:/you_web_path"; 如果发现配置项前是有分号,表明php.ini中没有该设置。那就很可能是在 php-fpm 中的 fastcgi.conf中配置了。php-fpm中的配置会覆盖php.ini的配置。
/you_web_path 是你要添加的让php可以访问的路径。多个路径直接分号隔开
如果也给服务器有多个项目,无论是在php.ini或者fastcgi.cong中设置,都是针对所有项目。那能不能只针对某个项目设置呢?
答案是肯定的。还可以在项目根目录中通过 .user.ini 进行配置。
.user.ini配置
首先,要使.user.ini生效,要设置php.ini 中的
user_ini.filename = ".user.ini"
user_ini.cache_ttl = 300
关于这两个配置的意义,请看php手册 http://php.net/manual/zh/configuration.file.per-user.php
注释掉 fastcgi.conf 中的 open_basedir 的配置。
在项目根目录 创建 .user.ini文件,写入如下内容
open_basedir=/tmp/:/proc/:/you_web_path
/you_web_path 是你要添加的让php可以访问的路径。多个路径直接分号隔开
重启一下php-fpm 服务即可。
测试中还发现这三个地方配置的优先级如下
“php.ini” > “nginx fastcgi fastcgi_param” > “php-fpm.conf”

边栏推荐
- 185. All employees with the top three highest wages in the Department (mandatory)
- Social wechat applet of fanzhihu forum community
- Localstorage and sessionstorage
- 2649: segment calculation
- 一体式水利视频监控站 遥测终端视频图像水位水质水量流速监测
- [binary search simple question] leetcode 35. search insertion position, 69. Square root of X, 367. Effective complete square, 441. Arrange coins
- 调用JShaman的Web API接口,实现JS代码加密。
- cocos小游戏实战-04-碰撞检测与NPC渲染
- 朴素贝叶斯——文档分类
- Redis四大特殊数据类型的学习和理解
猜你喜欢

196. Delete duplicate email addresses

Inftnews | ggac and China Aerospace ases exclusively produce "China 2065 Collection Edition"

Hcip day 14 notes

Okaleido tiger is about to log in to binance NFT in the second round, which has aroused heated discussion in the community

仿知乎论坛社区社交微信小程序

从ACL 2022 Onsite经历看NLP热点

IDEA 连接数据库查询数据后控制台表头中文乱码的解决方法

Worthington木瓜蛋白酶解离系统解决方案

“date: write error: No space left on device”解决

Plato farm has a new way of playing, and the arbitrage eplato has secured super high returns
随机推荐
八皇后编程实现
[栈和队列简单题] LeetCode 232. 用栈实现队列,225. 用队列实现栈
1.28亿美元!芬兰量子计算公司IQM获世界基金支持
Abbkine AbFluor 488 细胞凋亡检测试剂盒特点及实验建议
Plato Farm通过LaaS协议Elephant Swap,为社区用户带来全新体验
如何使用DevExpress WPF在WinUI中创建第一个MVVM应用程序?
HCIP第十四天笔记
{“errcode“:44001,“errmsg“:“empty media data, hint: [1655962096234893527769663], from ip: 222.72.xxx.
一道数学题,让芯片巨头亏了5亿美金!
typora详细教程
196. 删除重复的电子邮箱
一个测试类了解BeanUtils.copyProperties
Favicon web page collection icon online production PHP website source code /ico image online generation / support multiple image format conversion
毕业2年转行软件测试获得12K+,不考研月薪过万的梦想实现了
[binary search simple question] leetcode 35. search insertion position, 69. Square root of X, 367. Effective complete square, 441. Arrange coins
Abbkine AbFluor 488 细胞凋亡检测试剂盒特点及实验建议
Portraiture5 new and upgraded leather filter plug-in artifact
哈希表与一致性哈希的原理理解以及应用
Common questions and answers of software testing interview (divergent thinking, interface, performance, concept,)
An error in the fourth edition of the red book?