require(): open_basedir restriction in effect 的问题解决方法

前言
做了一下午的宝塔问题处理 ,逛遍了CSDN、STACKOVERLFLOW、宝塔论坛、WORDPRESS论坛,我们遇到问题,先要明白自己本地环境,问题产生的前提条件,才能有方向的交叉验证。我们通常会在配置LNMP/LAMP的web环境的时候会遇到这个问题,现象为web界面会出现白屏或500。
下面介绍的就是这个问题的原因及解决方法。

报错
【1】开启web界面的php的错误提示
<1>前提条件:ThinkPHP5部署在了LAMP/LNMP环境

<2>修改 php.ini(看具体的安装位置)
将 Off 修改为 On

vim php.ini

display_errors = On

 

<3>访问之前白屏的页面就会出现错误信息

【2】报错如下
Warning: require(): open_basedir restriction in effect. File(/home/zhceong.com/inc/common.php) is not within the allowed path(s): (/home/zhceong.com/domain/:/tmp/:/proc/) in /home/zhceong.com/domain/inc/common.php on line 3

Warning: require(/home/zhceong.com/inc/common.php): failed to open stream: Operation not permitted in /home/zhceong.com/domain/inc/common.php on line 3

Fatal error: require(): Failed opening required ‘/home/zhceong.com/domain/inc/../../inc/common.php’ (include_path=’.:/usr/local/php/lib/php’) in /home/zhceong.com/domain/inc/common.php on line 3

问题原因
由报错可以看出问题的重点在于open_basedir及 /home/zhceong.com/inc/common.php等文件。

原因是php在配置的问题,open_basedir的配置导致php不能成功引入文件(文件为报错日志中的文件;open_basedir 的作用是将php所能打开的文件限制在指定的目录或文件)。

从网上找的资料中,发现这种情况是由于服务器为了安全做的限制导致的,一般情况是不会出现这种问题的。

解决方法
解决方法来源于网络。根据环境,已测方法一可行。

open_basedir一般在php.ini里,也可能在了web服务器的配置文件里。

选择解决方法要先找到open_basedir的已被设置的位置,根据不同的环境选择不同的解决方法。

方法一:修改php.ini
<1>修改open_basedir

vim /etc/php.ini

open_basedir = “/home/zhceong.com/domain/:/tmp/:/proc/”

修改为

open_basedir = “/home/zhceong.com/:/tmp/:/var/tmp/:/proc/”

通常改为项目文件夹的上级目录

如果php.ini中的open_basedir 选项被注释或者为none,那么就根据Web环境选择其他的解决方法。

方法二:LAMP环境的解决方法
此环境需要修改 httpd.conf或者 域名.conf 文件

<1> 搜索open_basedir,查看配置在哪 (从httpd.conf或者 域名.conf 文件选)

grep -rn open_basedir httpd.conf

<2>修改配置了open_basedir的文件

php_admin_value open_basedir “/home/zhceong.com/domain/:/tmp/:/proc/”

修改为

php_admin_value open_basedir “/home/zhceong.com/:/tmp/:/proc/”

<3>重新启动 apache

sevice httpd restart(centos6)
systemctl restart httpd(centos7)

方法三:LNMP环境的解决方法
此环境同Apache一样,需要修改web的配置文件(nginx.conf 或者 域名.conf 文件)

<1>修改域名.conf 文件

fastcgi_param PHP_VALUE “open_basedir=/home/zhceong.com/domain/:/tmp/:/proc/”;

修改为

fastcgi_param PHP_VALUE “open_basedir=/home/zhceong.com/:/tmp/:/proc/”;

<2>重启 Nginx

/etc/init.d/nginx reload (centos6)
systemctl restart nginx (centos7)

修改的时候最好是绝对路径,也可以去根据网站根目录配置(根据网站根目录配置,要清楚不同项目使用的对应的根目录)
document_root/ 意为网站根目录

如配置文件中为
fastcgi_param PHP_ADMIN_VALUE ”open_basedir=$document_root/:XXX“;

要改为
fastcgi_param PHP_VALUE “open_basedir=/home/zhceong.com/:/tmp/:/proc/”;

方法四:user.ini 修改方法
未测试,但原理上可行

<1>打开对应项目根目录的 user.ini 文件

open_basedir=/home/zhceong.com/domain/:/tmp/:/proc/

修改为

open_basedir=/home/zhceong.com/:/tmp/:/proc/

<2>重启此web服务
————————————————
版权声明:本文为CSDN博主「漠效」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/GX_1_11_real/article/details/80950105

版权声明:vlifanv 发表于 2021-09-16 16:44:36。
转载请注明:require(): open_basedir restriction in effect 的问题解决方法 | 堆栈导航

暂无评论

暂无评论...