最近将本地的Tp5项目部署到lnmp环境访问报错open_basedir restriction in effect
查看报错日志显示:
![](https://i-blog.csdnimg.cn/blog_migrate/12ee59853f634f2c2e1a4fd4c15ef1dd.png)
网上查阅资料后是因为, Linux 主机设置了 open_basedir(可将用户访问文件的活动范围限制在指定的区域,通常是入口文件根目录的路径) 选项,因为***.conf中的root 配置入口文件在项目的public目录下所以导致 ThinkPHP5 项目目录(application目录)访问白屏或者报错。
解决办法:
由于lnmp环境考虑到虚拟站点各自的访问目录权限问题,于是在/usr/local/nginx/conf/fastcgi.conf
里配置了:
- fastcgi_param PHP_ADMIN_VALUE "open_basedir=$document_root/:/tmp/:/proc/";
那么每个站点的访问权限就限制在各自的document_root里了,
于是,我们在vhost/xxx.conf修改的root(root /home/wwwroot/xxx/public;)就把权限限制在public目录之后,
导致软件架构根目录下的application等都无法访问了(就会报错无权限),那么此时我们要解决的问题是:
2.既要保持root /home/www/xxx/public;,又要让虚拟站点有/home/www/xxx目录的访问权限,
那么可以修改/usr/local/nginx/conf/fastcgi.conf配置文件:
- fastcgi_param PHP_ADMIN_VALUE "open_basedir=$document_root/:/tmp/:/proc/";
改为:
- fastcgi_param PHP_ADMIN_VALUE "open_basedir=/home/wwwroot/xxx/:/tmp/:/proc/";
但问题来了,/usr/local/nginx/conf/fastcgi.conf是公共配置文件,采用这种写死的方式会导致其他站点不可用,
为了兼顾其他虚拟站点,if_not_empty派上用场了,
修改方法为:
不改变/usr/local/nginx/conf/fastcgi.conf配置文件里的原配置,而在
- fastcgi_param PHP_ADMIN_VALUE "open_basedir=$document_root/:/tmp/:/proc/";
之后添加
- fastcgi_param PHP_ADMIN_VALUE $basedir if_not_empty;#注意nginx要在1.1.11版本之后
$basedir变量就可以在/usr/local/nginx/conf/vhost/xxx.conf配置文件里的include enable-php.conf前赋值:
- set $basedir "open_basedir=/home/www/***/:/tmp/:/proc/";
优点:这样既满足了thinkphp5的部署要求,又不影响其他一般站点的使用。
缺点:如果$basedir没有赋值(至少一个专属配置有赋值),nginx -t无法通过。(会报错)
注意rewrite规则:
最后献上thinkphp的rewrite规则:
这种写法Tp3.2是可以的,但在这里是不可以的,否则经测试pathinfo为空
- location / {
- try_files $uri $uri/ /index.php?$query_string;
- }
应该改为:
- location / {
- if (!-e $request_filename){
- rewrite ^/(.*)$ /index.php?s=/$1 last;
- }
- }