今天一位朋友在linux服务器部署thinkphp5的时候PHP报了这个错误,如下:' h6 b# O! G: }, R
0 t8 V8 U% \6 p `# U- N" y
2 X% N. T+ e8 N3 K; M/ x
Warning: 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# H( g. A) u( Y! B6 |1 [- `. j" u
Warning: require(/www/wwwroot/zhuyuyun/thinkphp/start.php): failed to open stream: Operation not permitted in /www/wwwroot/zhuyuyun/public/index.php on line 20
! H# p# D1 G6 }6 @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- [, @+ u s2 r
- l- q5 M, Q& T: O* @
/ I. r+ h$ |6 d* t2 `0 t: |, ^. x
* | M3 p) K+ Q4 c8 e7 n$ h- v* h1 l
- M C+ w6 d# x2 C; Y c1 e! r- [
或者报如下类似:7 @7 g: d$ Q$ b4 q3 M' \" E7 o2 F
Warning: include_once(): open_basedir restriction in effect. File(../../staticpayb/WxPay.Config) is not within the allowed path(s):
; R' D, x* ]5 c* ~- o+ O4 b0 j' K' P* R4 m2 S5 G: c
出现问题的原因: i. d* u! H4 m; o5 t
查看问题描述以及资料,发现是php open_basedir 配置的问题,PHP不能引入其授权目录上级及其以上的文件;2 P( a4 r6 W& M! A7 \$ |' o
一般情况下是不会出现这种问题的,之所以出现这个问题绝大多数情况是由于服务器的原因,为了安全才做了这样的限制!
) \; z' u5 i# h3 Y1 C k1 I
; O, w/ c" {- r& ?. c! R# \解决方法:: h! u# e1 H9 X2 @
我首先要申明的是,下面的方法适合所有类似PHP报“open_basedir restriction in effect”错误的项目,并不仅仅只是适合thinkphp5的人。只要你的PHP报此错误都可以得到解决。
+ [2 y/ k- G" H5 Q( k* L5 j- N U. g6 S1 \+ d
如果把ThinkPHP5部署在了LAMP/LNMP环境上很有可能出现白屏或500的情况,这个时候需要开启 php 错误提示来判断是否是因为设置了open_basedir选项出错?1 N. z0 Y9 N8 @6 _# ]! z
打开 php.ini 搜索 display_errors,把 Off 修改为 On就开启了 php 错误提示,这时再访问之前白屏的页面就会出现错误信息。如果错误信息如下那么很有可能就是因为open_basedir的问题。8 ]( y( g; k5 R% \
* X. l; i( l, _& E% u6 T
一、php.ini 修改方法' f0 H9 V, g8 i
把权限作用域由入口文件目录修改为框架根目录
# ?& x- h0 V4 ]+ T' _5 w# g1 g& P8 C# q
打开 php.ini 搜索 open_basedir,把
- p0 |7 Z! z p% S) h$ ~9 k" _2 E, B& L) y3 B
$ V4 d! C/ P, K+ H- g1 [
open_basedir = "/home/wwwroot/tp5/public/:/tmp/:/var/tmp/:/proc/"
9 R7 Q9 w" p3 R" u T @% s# M) |/ b0 b' D) v4 y& M5 O4 Y0 I/ P
修改为( l E# I4 Y: c. V* T2 s. u
2 H8 R' V. E5 h! u. w. ~0 y# L9 O, I
open_basedir = "/home/wwwroot/tp5/:/tmp/:/var/tmp/:/proc/"5 ^/ z/ x4 Q+ {$ B* Z
! h: U. D/ u/ f6 U3 ~! B0 F( S3 O9 `/ r% E7 m7 M% B' E3 D
注意:
0 ?- N- P/ ?2 @4 \) b7 \/ Z如果你的 php.ini 文件的 open_basedir 设置选项是被注释的或者为 none,那么你需要通过 Apache 或者 Nginx 来修改> php.ini 文件通常是在 /usr/local/php/etc 目录中,当然了这取决于你 LAMP 环境配置。8 A, \( E0 @; I( T' T+ G6 Q/ r
0 x* C V9 j+ l8 `+ T0 m
二、Apache 修改方法8 L! ]3 o8 l7 a/ }" `; {6 i
Apache 需要修改 httpd.conf 或者同目录下的 vhost 目录下 你的域名.conf 文件,如果你的生成环境是 LAMP 一键安装包配置那么多半就是直接修改 你的域名.conf 文件
: x* |2 i4 d: F7 h1 m& L
1 M9 R8 o% s+ m1 ^" }3 {9 Yapache7 |9 f& ]: \3 P5 `& f7 g
├─vhost
$ c$ u, {8 [) J8 D) L- _├─www.thinkphp.cn.conf
2 @3 q' R/ a) U7 `├─......
* {1 X) v0 r! J& E1 \/ n K/ C7 l/ z9 g% p├─httpd.conf
* N1 o3 E" D o& r
2 s& u9 U" c) E- H6 j: S% D+ n7 }2 {
打开 你的域名.conf 文件 搜索 open_basedir,把
% Z, a8 M, t8 j, C6 Y' F6 W* Z. l; o" i5 D$ u+ P6 V8 P
; p3 k0 b( L6 S: O$ _' f4 W: o7 Uphp_admin_value open_basedir "/home/wwwroot/http://www.thinkphp.cn/public/:/tmp/:/var/tmp/:/proc/"
7 A1 P0 _: c, ^9 [+ G* C: Y% l, I. u( b0 _2 o
修改为
+ Y5 Z* B- k- {8 |1 S" k6 L6 g
; v L" k p( m! o. P3 E5 _- _, B, \7 O3 t4 S
php_admin_value open_basedir "/home/wwwroot/http://www.thinkphp.cn/:/tmp/:/var/tmp/:/proc/", M2 V! L" G8 H0 ?
0 w' r; I0 L, b+ x0 H( t& y
# {! P2 u) W, q! [' Z然后重新启动 apache 即可生效
# B1 X3 h! l. M7 m' M+ I$ N> 域名.conf 文件通常是在 /usr/local/apache/conf 目录中,当然了这取决于你 LAMP 环境配置
0 C3 y; Y) k# a/ @( V- m- L9 @6 s
: u, L' `6 J: m3 j& e, L$ U三、Nginx/Tengine 修改方法6 k$ w) C: @$ C+ c0 h
Nginx 需要修改 nginx.conf 或者 conf/vhost 目录下 你的域名.conf 文件,如果你的生成环境是 LNMP/LTMP 一键安装包配置那么多半就是直接修改 你的域名.conf 文件/ n1 X8 d( w* S0 @# t7 X
" o3 V& z3 ]' a+ B/ I( Y
nginx
2 \* F% s( H5 r) m├─conf
$ j( U- V5 B v Y- h) l├─vhost- v$ q# k5 h9 p* Z7 M
├─www.thinkphp.cn.conf$ P/ }6 J1 R( J; ~3 C
├─nginx.conf
4 Q$ ~& c; o: L) O/ w4 s├─......0 `- T0 X. p' P) [, W) Q( m& |
├─nginx.conf
. O& {8 g! }) S$ i5 M
4 ]# |% E# ~0 R) B( T( D' N2 I8 ^8 S N1 V
打开 你的域名.conf 文件 搜索 open_basedir,把
% W- D2 @+ R( e" o8 x: [/ [4 @
' `" u5 `& N9 A1 |! o( q$ A+ t# e, u2 ]; D4 I* l% f
fastcgi_param PHP_VALUE "open_basedir=/home/wwwroot/http://www.thinkphp.cn/public/:/tmp/:/proc/";6 f' Z2 N) n' p# k# S& P
9 q& \. L, v: X1 {1 t' @7 r2 h; m
修改为/ b$ b7 v; Z k1 F! @
/ u/ a& J4 G0 u" b' v! |
* S3 ?3 H, Z) H* A" x' ?9 _ r- jfastcgi_param PHP_VALUE "open_basedir=/home/wwwroot/http://www.thinkphp.cn/:/tmp/:/proc/";
) U) k. ]7 t5 p- c/ H3 Z+ k; p1 x/ q) [5 v0 P
9 N9 H, D8 ]3 F1 p% Y; l' n& R
然后重新启动 Nginx 即可生效
" @* C& e& r. j) l. L% B+ X4 g域名.conf 文件通常是在 /usr/local/nginx/conf/vhost 目录中,当然了这取决于你 LNMP/LTMP 环境配置4 e" n' ~' g7 x
* R2 c$ n5 Q1 a1 i+ j; m$ z: X
注意:上面我们修改的时候用的是绝对地址,但是也可以这样直接去找网站根目录(如果你这样配置,你一定要懂不同项目有不同根目录的原理,否则也是行不通的。):" _- P* m4 R0 ]3 W
fastcgi_param PHP_ADMIN_VALUE "open_basedir=$document_root/:/tmp/:/proc/";4 i8 P+ T7 q' H" S/ }, I9 }
上面这行代码拿thinkphp来讲,如果你根目录是public,那么$document_root就会指到public;3 N5 c6 ?" _+ y1 M7 u$ h
依然会生成错误的配置如下:
8 r5 B# {3 x. D! O! m; }$ q* e5 P5 W" l1 s; y( P
5 r& T1 P2 ^9 Zfastcgi_param PHP_VALUE "open_basedir=/home/wwwroot/http://www.thinkphp.cn/public/:/tmp/:/proc/";' ] v) ~0 N: {" M! P7 ~
! \, V' }* E; Y! U8 q+ {7 l7 L" Q0 c+ g% Q3 L5 @
四、fpm/fastcgi user.ini 修改方法7 c3 y3 ~# ^ Q7 N6 N0 l& y
打开 项目根目录下找到 user.ini 文件,搜索 open_basedir,把! L( [" l0 x7 {6 b
' ^( D% | j* l; n N' W
3 f7 b0 {) I$ _& P9 _5 v
open_basedir=/home/wwwroot/http://www.thinkphp.cn/public/:/tmp/:/proc/
8 J9 c7 ]/ U: H
9 ?8 q% c: V3 \修改为7 o+ y9 E/ K, }, s9 X& X' ?* _
5 @+ d* @& x, V j4 W) K9 c( h: ^
) e! ^$ u: V& N
open_basedir=/home/wwwroot/http://www.thinkphp.cn/:/tmp/:/proc/
+ [/ M3 _. {: Z- i: `' X: A
: r2 h0 ]1 D) j/ c( D7 z
9 Q- q8 ]" _: W- r i2 R! E3 s# b然后重新启动 web 服务器 即可生效!
* h9 e6 J. A7 B) C
* P+ @ \; R2 R8 u; ^总结:' u6 M& k' {/ X O2 \1 s; ?& C
一定要仔细读此篇文章,否则你会选错适合你的方法。
: P; B/ t, @9 V! `4 ~* L k一定要根据自己的服务器环境来选择你的修改方法。* \; \6 Z5 ^* E- D( ~% z F0 i
. `, G2 J, u5 j+ s, }7 m
由于有很多的朋友用的是“宝塔面板”或“LNMP/LAMP“一键安装包的,设置方法可以看评论!9 _5 s: F( X, h) c; W
2 t8 @6 o, o8 C1 A- v
; y' n( }0 S# p ]
8 p& G; H3 o; x, S最近发现有很多人用“宝塔面板”经常出现这个错误,一直来Q上私聊我,我就再给大家分享一下。; ^$ Q; F( I, ]7 [7 r E/ V' U
宝塔面板出现“open_basedir restriction in effect. ”的解决方法
; S' f$ ?! }" Y+ l9 h, ]1、只需要在相应的网站目录,勾选掉防跨站攻击(open_basedir)即可!4 ?1 z$ S) U) m9 p6 [! t
2、必须重启PHP;5 U& E+ h* C+ k2 o7 S" e
; b. ]5 y$ g6 Y4 \& [: a2 R
+ b2 o7 g! U* f6 S- U6 k- p' |. D/ U) x! z/ {7 y# R4 [
* b: f9 Y1 F9 L
LNMP/LAMP 出现“open_basedir restriction in effect. ”的解决方法- y( k: J* T/ O( A# w* L4 n3 Z- ^
我今天也遇到了这样的情况,我用的是LNMP一键安装包+Thinkphp,然后根目录绑定到的是public目录下,再访问首页的时候出现500错误,后来去 php.ini 把 display_errors 设置为 on 后,报的错误和文章讲的一模一样;, c& e( i, Q2 I. Q9 W
! ~ h/ p4 M5 Q- ]$ ]我的解决方法其实和文章也差不多,原理都是一样的:
3 a. C u( I) ], \. Z3 W0 d( ^4 ^5 S1 x+ R9 H. b
我是看到LNMP官方提供的“防跨目录设置”方法,如下:
2 D' D, V0 I5 i$ nLNMP 1.1及之前的版本使用php.ini里面,open_basedir设置
: u& C2 G. c% a L+ z) T I8 X2 e' u
LNMP 1.2及更高版本防跨目录功能使用.user.ini,该文件在网站根目录下,可以修改.user.ini 里面的open_basedir的值来设置限制访问的目录或删除来移除防跨目录的设置。
( Q: {0 ]: c, Y( J) d' z/ Q' U8 J& m! j; |$ i8 _; D& z. @
.user.ini文件无法直接修改,如要修或删除需要先执行:chattr -i /网站目录/.user.ini0 X; B' C- g" y& J% y& R& V F
可以使用winscp文件管理、vim编辑器或nano编辑器进行修改。9 F/ q. v9 o: t# B, P' l9 J
删除的话rm -f /网站目录/.user.ini 就可以。
6 q8 N( M* |/ f* ` T/ J修改完成后再执行:chattr +i /网站目录/.user.ini6 V0 k/ Q M: d1 u: s p3 u' r4 S
.user.ini不需要重启一般5分钟左右生效,也可以重启一下php-fpm立即生效。
7 _6 q( l! {" A; D* R+ _如果要更改网站目录必须要按上述方法修改防跨目录的设置,否则肯定报错!!/ Q" F. ^$ o: u$ | K* U
# Q/ y( M+ ], \* H, V- GLNMP 1.4或更高版本如果不想用防跨目录除需要删除.user.ini的防跨目录的目录还需要将 /usr/local/nginx/conf/fastcgi.conf 里面的fastcgi_param PHP_ADMIN_VALUE "open_basedir=$document_root/:/tmp/:/proc/"; 在该行行前添加 # 或删除改行,需要重启nginx。% p. ^+ t! }* ]2 s- V9 S
: m, ?/ q" v: W6 w) q$ Y- ?* h$ {LNMP 1.4或更高版本也可以直接使用lnmp安装包 tools/ 目录下的 ./remove_open_basedir_restriction.sh 进行移除,需要输入原网站目录完整路径(注意不是新的网站路径),回车即可。+ G3 Z6 M8 ^! Q& m% }1 E
在Thinkphp、codeigniter、Laravel等框架下,网站目录一般是在public下,但是public下的程序要跨目录调用public上级目录下的文件,因为LNMP默认是不允许跨目录访问的,所以都是必须要将防跨目录访问的设置去掉,有时候这些框架类的程序提示500错误也可能是这个问题引起的。. H! E& [* F" Z* o. Z
! \5 f& a+ ]2 v9 e5 l+ Y
+ [4 B3 Y/ K! q' a; F; n+ @0 o7 L, j. b! m* F1 `
LNMPA或LAMP 模式1.2版本或更高版本的防跨目录的设置使用的对应apache虚拟主机配置文件(lnmp管理工具添加的话文件是 /usr/local/apache/conf/vhost/域名.conf )里的php_admin_value open_basedir参数进行设置。如果不需要防跨目录设置可以在 php_admin_value open_basedir 该行前面加 # 进行注释,或自行修改参数后面的目录。
4 u& P. z7 n) n# m; ?9 k. z* Q
! a' D8 |9 s6 d8 z) } `! S5 p- U重启apache生效。 |