MENU

lnmp1.4beta环境open_basedir限制问题

March 31, 2017 • PHP

使用Yaf代码生成器生成了项目目录后index.php文件是和application目录同级别的,于是想改成下面这样

+ public
  |- index.php //入口文件
  |- .htaccess //重写规则    
  |+ css
  |+ img
  |+ js
+ conf
  |- application.ini //配置文件   
+ application
  |+ controllers
     |- Index.php //默认控制器
  |+ views    
     |+ index   //控制器
        |- index.phtml //默认视图
  |+ modules //其他模块
  |+ library //本地类库
  |+ models  //model目录
  |+ plugins //插件目录

即把index.php放入public目录中。而且修改了index.php中的APPLICATION_PATH常量路径

define('APPLICATION_PATH', realpath(__DIR__ . '/../'));

这时由于lnmp集成环境的open_basedir限制导致php无法读取上一级目录,会报类似如下错误

Warning: realpath(): open_basedir restriction in effect. File(/srv/code) is not within the allowed path(s): (/srv/code/yaf:/tmp/:/proc/) in /srv/code/yaf/public/index.php on line 7

由于lnmp1.4beta环境/usr/local/nginx/conf/fastcgi.conf中添加了fastcgi_param PHP_ADMIN_VALUE "open_basedir=$document_root/:/tmp/:/proc/";这一行,所以如果需要我们自己手动控制限制目录就得先注释掉这行。

index.php同级写可以新建一个.user.ini的文件来进行open_basedir的控制

cd public
cat 'open_basedir=/srv/code/yaf:/tmp/:/proc/' > .user.ini
chmod 644 .user.ini
/etc/init.d/php-fpm restart

然后就可以通过.user.ini文件来设置了

注意:.user.ini文件得和入口文件index.php同级,我之前放到了项目根目录下,导致怎么配也无法生效

Last Modified: November 10, 2019