今天一位朋友在linux服务器部署thinkphp5的时候PHP报了这个错误,如下:" x$ \3 U6 Q; g8 W& b: l" i# T b
. C- D D n9 `/ O
9 k ]% P% o- u* lWarning: require(): open_basedir restriction in effect. File(/www/wwwroot/zhuyuyun/thinkphp/start.php) is not within the allowed path(s): (/www/wwwroot/zhuyuyun/public/:/tmp/:/proc/) in /www/wwwroot/zhuyuyun/public/index.php on line 20
2 D" p- ^" U* I4 C5 sWarning: require(/www/wwwroot/zhuyuyun/thinkphp/start.php): failed to open stream: Operation not permitted in /www/wwwroot/zhuyuyun/public/index.php on line 20& M; s" @4 a* ~: V3 ]
Fatal error: require(): Failed opening required '/www/wwwroot/zhuyuyun/public/../thinkphp/start.php' (include_path='.:/www/server/php/56/lib/php') in /www/wwwroot/zhuyuyun/public/index.php on line 20% w& |4 D- F+ `
& u: {, e5 {* x h0 k$ w" D+ ^0 c6 u5 G. k, m- N( e
0 i" q7 {0 c3 k1 S% U. m
+ b; e1 U7 D- U+ L2 D& R: f4 p: S
6 k4 G: |0 R. S5 P8 ]
或者报如下类似:
# ]5 S2 X: s4 w7 ?1 |6 ZWarning: include_once(): open_basedir restriction in effect. File(../../staticpayb/WxPay.Config) is not within the allowed path(s):% l0 F/ v T1 H2 z6 ~/ x
0 p) m8 _$ X& ?9 l出现问题的原因:! K; {1 F! p3 {7 T4 H7 o
查看问题描述以及资料,发现是php open_basedir 配置的问题,PHP不能引入其授权目录上级及其以上的文件;
; h# F+ a; Z8 F8 `一般情况下是不会出现这种问题的,之所以出现这个问题绝大多数情况是由于服务器的原因,为了安全才做了这样的限制!, d) B/ G, P9 z6 E
6 a/ m; I+ k1 T解决方法:/ R H0 B& U+ ]) w0 l
我首先要申明的是,下面的方法适合所有类似PHP报“open_basedir restriction in effect”错误的项目,并不仅仅只是适合thinkphp5的人。只要你的PHP报此错误都可以得到解决。
' R1 @2 l) l/ g U) a0 Y3 g9 n/ R+ C1 b H( N
如果把ThinkPHP5部署在了LAMP/LNMP环境上很有可能出现白屏或500的情况,这个时候需要开启 php 错误提示来判断是否是因为设置了open_basedir选项出错?
( L& P; R/ R$ k; l7 q! g打开 php.ini 搜索 display_errors,把 Off 修改为 On就开启了 php 错误提示,这时再访问之前白屏的页面就会出现错误信息。如果错误信息如下那么很有可能就是因为open_basedir的问题。' \5 n$ l# ?' z: A/ Y% E
, [1 [9 A& H R4 T一、php.ini 修改方法' q4 a$ [# A( _% x( C3 L% D
把权限作用域由入口文件目录修改为框架根目录* M6 g3 N. v8 C3 ?# E9 L8 E" d1 f
C7 h5 z! K8 R6 z打开 php.ini 搜索 open_basedir,把
' k9 k& J" B" Z: X4 q1 U' \) ]2 C% v$ V: `
3 f* U4 O4 j: T. c: b1 dopen_basedir = "/home/wwwroot/tp5/public/:/tmp/:/var/tmp/:/proc/"
; q3 Z% {8 B4 A( o: k2 ~% I6 k4 ]+ C% I3 @
修改为2 G" {. B$ b3 [
$ V( y! ~/ a1 M5 `/ e- j" N" e4 q3 k6 K3 A) }/ C/ n
open_basedir = "/home/wwwroot/tp5/:/tmp/:/var/tmp/:/proc/"
' l9 M z0 I6 [8 O. k$ Q0 p1 T" a s/ `3 E
3 ]7 a8 L2 p! l2 T. z! r" o9 L# E0 c注意:
b' a: A0 D; d6 S# P; V如果你的 php.ini 文件的 open_basedir 设置选项是被注释的或者为 none,那么你需要通过 Apache 或者 Nginx 来修改> php.ini 文件通常是在 /usr/local/php/etc 目录中,当然了这取决于你 LAMP 环境配置。8 U0 S9 l5 K0 g5 U. f' ]
3 o: k1 m! M) J' _% [3 r
二、Apache 修改方法% m" r$ x1 r: Z C/ Z
Apache 需要修改 httpd.conf 或者同目录下的 vhost 目录下 你的域名.conf 文件,如果你的生成环境是 LAMP 一键安装包配置那么多半就是直接修改 你的域名.conf 文件
! o/ E6 X$ |( ?2 j/ m! r
, c7 \% t( N, ^2 ^apache# h, v$ d/ l: f$ e7 h
├─vhost
( r+ y7 U" E' I2 c/ ~. g# O$ U├─www.thinkphp.cn.conf
6 E) v( A M9 K& B├─......
/ L1 M/ U' N' `& G├─httpd.conf
$ y8 _' _+ W- z2 C
3 M R: R& Z" Q; V& b6 ]! x* T& d0 d+ L% M) m, H6 v( u* `
打开 你的域名.conf 文件 搜索 open_basedir,把
6 y5 o9 x [2 n5 j
/ {2 A' K0 q' k u: Y
$ c% x" d% o$ D2 {3 @) tphp_admin_value open_basedir "/home/wwwroot/http://www.thinkphp.cn/public/:/tmp/:/var/tmp/:/proc/"% v! v$ q1 o4 H9 P+ }+ |
) Y4 C* T- z# |1 }8 U! k& F* m& ~
修改为
: V. @: ]8 D" M# Z4 i( D( ^
( E- a. _* p3 K4 w4 F! U3 Z! i/ d3 j1 q& b2 h( p! S
php_admin_value open_basedir "/home/wwwroot/http://www.thinkphp.cn/:/tmp/:/var/tmp/:/proc/"
! i' E8 q3 i) W1 E) N1 `
: t2 ^! I @- l' r6 n! V. \* `& E8 n4 E& Q4 h
然后重新启动 apache 即可生效
+ J; Q- s5 g- o0 p9 ?> 域名.conf 文件通常是在 /usr/local/apache/conf 目录中,当然了这取决于你 LAMP 环境配置
& m) W8 i& h. r! @& ^" _" x0 y
% X$ ~* {: q1 i ^" Y o! F三、Nginx/Tengine 修改方法* B. R. n; l7 a9 N( Y
Nginx 需要修改 nginx.conf 或者 conf/vhost 目录下 你的域名.conf 文件,如果你的生成环境是 LNMP/LTMP 一键安装包配置那么多半就是直接修改 你的域名.conf 文件
( Q! g) v/ F1 S4 ]% {& O9 h: N6 r" z/ |, ^6 \; k1 W0 X
nginx1 o$ u, W0 s& ^0 l. o
├─conf
) q; a) d; r1 \% A' `* x' J% r├─vhost8 D( `5 W' Y& m6 @; f( w5 L
├─www.thinkphp.cn.conf
O3 ?% m& n5 t├─nginx.conf
5 [2 L) a$ g& C* v. t" P8 p0 _├─......" s o- {( C2 |5 Q2 g4 @9 _6 l
├─nginx.conf
# x+ w5 D# Q0 l, C9 D$ N O3 X; G% z% d- d9 g& H
) U- y6 k4 y, G6 P0 g9 r! i8 o* z
打开 你的域名.conf 文件 搜索 open_basedir,把
/ `4 Q* Z f1 n& u- P: n$ W. q. N
; j; ?9 g; h- y. zfastcgi_param PHP_VALUE "open_basedir=/home/wwwroot/http://www.thinkphp.cn/public/:/tmp/:/proc/";9 u" u3 O( U# B( Y" l3 Q
8 @8 z/ H1 I& t" S. e修改为+ A. a; y/ H) y) d u
- Z& W( I3 g8 u$ p1 ? {
& o' z1 |+ G9 |$ afastcgi_param PHP_VALUE "open_basedir=/home/wwwroot/http://www.thinkphp.cn/:/tmp/:/proc/";9 i% E2 k, N9 ^! o: u' s) X
6 j! ^/ F9 Q' I& L2 I
! ? C6 K- q- O然后重新启动 Nginx 即可生效
" |, O0 `6 {" ~! e7 ]8 i域名.conf 文件通常是在 /usr/local/nginx/conf/vhost 目录中,当然了这取决于你 LNMP/LTMP 环境配置
& w9 A7 o* T$ g+ M- V
3 {$ j! t/ ` |# L0 b注意:上面我们修改的时候用的是绝对地址,但是也可以这样直接去找网站根目录(如果你这样配置,你一定要懂不同项目有不同根目录的原理,否则也是行不通的。):
$ T. W& Z) a d% Dfastcgi_param PHP_ADMIN_VALUE "open_basedir=$document_root/:/tmp/:/proc/";, m* b& Q9 Y6 ?& ^3 b5 _0 X H$ y% D
上面这行代码拿thinkphp来讲,如果你根目录是public,那么$document_root就会指到public;
' [ P1 O. h+ q7 O5 A G8 Y依然会生成错误的配置如下:8 L5 i1 G, \4 }7 U
* U5 [* Q, I( R. p; M u4 t
9 }# n N2 ~2 l& Z: G! _: xfastcgi_param PHP_VALUE "open_basedir=/home/wwwroot/http://www.thinkphp.cn/public/:/tmp/:/proc/";
5 ?8 P2 a. u" Q: l0 i9 e. @# \1 h6 f: z1 Z1 T* `
* J I8 T; P7 ~+ R; Q
四、fpm/fastcgi user.ini 修改方法
3 u- |- B0 p2 D, `+ p! o7 ?3 W打开 项目根目录下找到 user.ini 文件,搜索 open_basedir,把
9 h6 O/ k: m) L5 |
" a* `9 I) n- T+ X0 H0 _1 s9 E( F0 M* Q8 `! P+ \7 v- p
open_basedir=/home/wwwroot/http://www.thinkphp.cn/public/:/tmp/:/proc/+ e* Y0 T; V' g% j0 ]1 y1 O
5 P$ Z& Z/ M x2 X/ [, A% A修改为
: }5 @/ d; m0 b) h# b( r* ~* N* \/ Q6 b& l- y* T+ r
) y" o$ R; Q9 G; A, z' j
open_basedir=/home/wwwroot/http://www.thinkphp.cn/:/tmp/:/proc/8 P/ F: }; H5 A' o. m
- J% F% j/ j1 f$ i1 {; c& u# L
^* g; w# x& E7 Y# J$ O2 R
然后重新启动 web 服务器 即可生效!
4 y; `4 `- @, [" j1 u% K
% t q K2 w# i' m: y$ [. [8 ]总结:
% ?5 E) d+ a4 p; s. H一定要仔细读此篇文章,否则你会选错适合你的方法。
4 b) |# S8 P- r6 Q* Q) W一定要根据自己的服务器环境来选择你的修改方法。! t9 Q3 [1 g/ R
# W: K6 f$ U; w. T* M2 n0 g$ I由于有很多的朋友用的是“宝塔面板”或“LNMP/LAMP“一键安装包的,设置方法可以看评论!
- ~, L V4 H* s" w& u$ \. T; E& R5 @3 S. J, ]1 f* c& V
; ~/ q' H1 g& B. D( Q! D* W% w- g8 T, e% d$ M" W- n
最近发现有很多人用“宝塔面板”经常出现这个错误,一直来Q上私聊我,我就再给大家分享一下。1 q" l0 H) i/ L0 O
宝塔面板出现“open_basedir restriction in effect. ”的解决方法4 c+ p4 V4 v) |! \/ O C4 }
1、只需要在相应的网站目录,勾选掉防跨站攻击(open_basedir)即可!
7 N d# W- R) ?2、必须重启PHP;
0 S T4 a& s9 `2 h
3 d& U0 ^" @9 @- n& v
6 n: O; d+ v y) V: F- Q9 i/ {8 h2 c) k, l
% n$ a3 G" q F
LNMP/LAMP 出现“open_basedir restriction in effect. ”的解决方法+ ^/ ?5 E+ s! y- S
我今天也遇到了这样的情况,我用的是LNMP一键安装包+Thinkphp,然后根目录绑定到的是public目录下,再访问首页的时候出现500错误,后来去 php.ini 把 display_errors 设置为 on 后,报的错误和文章讲的一模一样;9 Z4 Q( C$ W8 n* o' \3 ^
& M& M7 G9 c# y7 F0 q
我的解决方法其实和文章也差不多,原理都是一样的:# w: G+ ] q4 `8 ~, h
/ C/ x/ [0 v% t. |% E我是看到LNMP官方提供的“防跨目录设置”方法,如下:
" B3 o. I; g" v+ `4 [LNMP 1.1及之前的版本使用php.ini里面,open_basedir设置8 F0 q, h# {! E0 P0 Y" P2 D5 O0 u
% U" o; ^; I! j6 }! H) t) k7 t% d8 ?LNMP 1.2及更高版本防跨目录功能使用.user.ini,该文件在网站根目录下,可以修改.user.ini 里面的open_basedir的值来设置限制访问的目录或删除来移除防跨目录的设置。
3 I; c* v) m1 C6 z. R0 q; O0 g% n$ H: e6 N, I' @$ ]
.user.ini文件无法直接修改,如要修或删除需要先执行:chattr -i /网站目录/.user.ini' z" ~ y* n1 j, S2 B' g; A
可以使用winscp文件管理、vim编辑器或nano编辑器进行修改。
- p6 i' ]5 k+ M4 v, \删除的话rm -f /网站目录/.user.ini 就可以。
( e; O" v8 d% g0 ?7 Z# R/ q修改完成后再执行:chattr +i /网站目录/.user.ini7 s% G* u' k2 \4 q
.user.ini不需要重启一般5分钟左右生效,也可以重启一下php-fpm立即生效。" R" u& b0 g5 G( s' n( o- h2 f
如果要更改网站目录必须要按上述方法修改防跨目录的设置,否则肯定报错!!
: L5 ~' T) ?' u6 m) _ |: J5 A! B6 K, N j9 z
LNMP 1.4或更高版本如果不想用防跨目录除需要删除.user.ini的防跨目录的目录还需要将 /usr/local/nginx/conf/fastcgi.conf 里面的fastcgi_param PHP_ADMIN_VALUE "open_basedir=$document_root/:/tmp/:/proc/"; 在该行行前添加 # 或删除改行,需要重启nginx。
& e9 g! t) {, Q) o& @4 w! A" j1 U" s$ u- O( e; M; v
LNMP 1.4或更高版本也可以直接使用lnmp安装包 tools/ 目录下的 ./remove_open_basedir_restriction.sh 进行移除,需要输入原网站目录完整路径(注意不是新的网站路径),回车即可。" }4 Y" @6 A" {2 W- A
在Thinkphp、codeigniter、Laravel等框架下,网站目录一般是在public下,但是public下的程序要跨目录调用public上级目录下的文件,因为LNMP默认是不允许跨目录访问的,所以都是必须要将防跨目录访问的设置去掉,有时候这些框架类的程序提示500错误也可能是这个问题引起的。
- M1 J, f( m! }$ }# T# T( e c! N; C9 K
6 Z. W) W: m% l5 a2 Y& O5 c
& w/ ?) O3 J& I& q, MLNMPA或LAMP 模式1.2版本或更高版本的防跨目录的设置使用的对应apache虚拟主机配置文件(lnmp管理工具添加的话文件是 /usr/local/apache/conf/vhost/域名.conf )里的php_admin_value open_basedir参数进行设置。如果不需要防跨目录设置可以在 php_admin_value open_basedir 该行前面加 # 进行注释,或自行修改参数后面的目录。+ v1 X% }! f6 d+ V; Q/ Z
) R8 n+ Z6 n, ?. e重启apache生效。 |