【yii2从Apache迁移到nginx*问报500错误】

时间:2021-07-28 04:51:43

【yii2从Apache迁移到nginx*问报500错误】

今天迁移yii2项目从Apache到nginx,出现了几个小问题,记录一下

index.php 加上

error_reporting(E_ERROR); ini_set('display_errors', 'yes');

来临时打开错误级别和错误显示,500 是内部错误,一般是程序有问题。

用ip从目录访问不报错,最后看到了域名的.conf配置文件open_basedir=$document_root字段,也就是说把活动限制在了web目录下,

直接从直接从nginx.conf拿出的一段server{} 放在vhost/xxxx.conf里面,没有注意到open_basedir,于是注释掉了这句配置文件

nginx在server{}中加了伪静态配置

location / {  

    if (!-e $request_filename){  

        rewrite ^/(.*) /index.php last;  

    }  

}

页面能打开,刷新一下变成500,再刷新两下又能打开,再刷新又是500状态,如此循环。

nginx拿到错误日志,一下报三条。

2016/04/22 20:56:18 [error] 28923#0: *558 FastCGI sent in stderr: "PHP message: PHP Warning:  realpath(): open_basedir restriction in effect. File(/home/wwwroot/php/zlxoc/trunk/admin) is not within the allowed path(s): (/home/wwwroot/php/zlxoc/trunk/admin/web:/tmp/:/proc/) in /home/wwwroot/php/zlxoc/trunk/vendor/yiisoft/yii2/base/Module.php on line 220

PHP message: PHP Stack trace:

PHP message: PHP   1. {main}() /home/wwwroot/php/zlxoc/trunk/admin/web/index.php:0

PHP message: PHP   2. yii\base\Application->__construct() /home/wwwroot/php/zlxoc/trunk/admin/web/index.php:17

PHP message: PHP   3. yii\base\Application->preInit() /home/wwwroot/php/zlxoc/trunk/vendor/yiisoft/yii2/base/Application.php:202

PHP message: PHP   4. yii\base\Application->setBasePath() /home/wwwroot/php/zlxoc/trunk/vendor/yiisoft/yii2/base/Application.php:223

PHP message: PHP   5. yii\base\Module->setBasePath() /home/wwwroot/php/zlxoc/trunk/vendor/yiisoft/yii2/base/Application.php:358

PHP message: PHP   6. realpath() /home/wwwroot/php/zlxoc/trunk/vendor/yiisoft/yii2/base/Module.php:220

PHP message: PHP Warning:  include(): open_basedir restriction in effect. File(/home/wwwroot/php/zlxoc/trunk/vendor/yiisoft/yii2/base/InvalidParamException.php) is not within the allowed path(s): (/home/wwwroot/php/zlxoc/trunk/admin/web:/tmp/:/proc/) in /home/wwwroot/php/zlxoc/trunk/vendor/yiisoft/yii2/BaseYii.php on line 288

PHP message: PHP Stack trace:

PHP message: PHP   1. {main}() /home/wwwroot/php/zlxoc/trunk/admin/web/index.php:0

PHP message: PHP   2. yii\base\Application->__construct() /home/wwwroot/php/zlxoc/trunk/admin/web/index.php:17

PHP message: PHP   3. yii\base\Application->preInit() /home/wwwroot/php/zlxoc/trunk/vendor/yiisoft/yii2/base/Application.php:202

PHP message: PHP   4. yii\base\Application->setBasePath() /home/wwwroot/php/zlxoc/trunk/vendor/yiisoft/yii2/base/Application.php:223

PHP message: PHP   5. yii\base\Module->setBasePath() /home/wwwroot/php/zlxoc/trunk/vendor/yiisoft/yii2/base/Application.php:358

PHP message: PHP   6. spl_autoloa

2016/04/22 20:56:18 [error] 28923#0: *558 FastCGI sent in stderr: "nk/vendor/yiisoft/yii2/base/Application.php:358

PHP message: PHP   6. spl_autoload_call() /home/wwwroot/php/zlxoc/trunk/vendor/yiisoft/yii2/base/Application.php:224

PHP message: PHP   7. yii\BaseYii::autoload() /home/wwwroot/php/zlxoc/trunk/vendor/yiisoft/yii2/base/Application.php:0

PHP message: PHP Warning:  include(): open_basedir restriction in effect. File(/home/wwwroot/php/zlxoc/trunk/vendor/yiisoft/yii2/base/UnknownClassException.php) is not within the allowed path(s): (/home/wwwroot/php/zlxoc/trunk/admin/web:/tmp/:/proc/) in /home/wwwroot/php/zlxoc/trunk/vendor/yiisoft/yii2/BaseYii.php on line 288

PHP message: PHP Stack trace:

PHP message: PHP   1. {main}() /home/wwwroot/php/zlxoc/trunk/admin/web/index.php:0

PHP message: PHP   2. yii\base\Application->__construct() /home/wwwroot/php/zlxoc/trunk/admin/web/index.php:17

PHP message: PHP   3. yii\base\Application->preInit() /home/wwwroot/php/zlxoc/trunk/vendor/yiisoft/yii2/base/Application.php:202

PHP message: PHP   4. yii\base\Application->setBasePath() /home/wwwroot/php/zlxoc/trunk/vendor/yiisoft/yii2/base/Application.php:223

PHP message: PHP   5. yii\base\Module->setBasePath() /home/wwwroot/php/zlxoc/trunk/vendor/yiisoft/yii2/base/Application.php:358

PHP message: PHP   6. spl_autoload_call() /home/wwwroot/php/zlxoc/trunk/vendor/yiisoft/yii2/base/Application.php:224

PHP message: PHP   7. yii\BaseYii::autoload() /home/wwwroot/php/zlxoc/trunk/vendor/yiisoft/yii2/base/Application.php:0

PHP message: PHP   8. spl_autoload_call() /home/wwwroot/php/zlxoc/trunk/vendor/yiisoft/yii2/base/Application.php:291

PHP message: PHP   9. yii\BaseYii::autoload() /home/wwwroot/php/zlxoc/trunk/vendor/yiisoft/yii2/base/Application.php:0

PHP message: PHP Warning:  include(/home/wwwroot/php/zlxoc/trunk/vendor/yiisoft/yii2/base/UnknownClassException.php): failed to open stream: Operation not permitted in /home/wwwroot/php/zlxoc/trunk/vendor/yiisoft/yii2/BaseYii.php on line 288

PHP message: PHP Stack trace:

PHP messag

2016/04/22 20:56:18 [error] 28923#0: *558 FastCGI sent in stderr: "ii2/base/Application.php:0

PHP message: PHP   8. spl_autoload_call() /home/wwwroot/php/zlxoc/trunk/vendor/yiisoft/yii2/base/Application.php:291

PHP message: PHP   9. yii\BaseYii::autoload() /home/wwwroot/php/zlxoc/trunk/vendor/yiisoft/yii2/base/Application.php:0

PHP message: PHP Fatal error:  Class 'yii\base\UnknownClassException' not found in /home/wwwroot/php/zlxoc/trunk/vendor/yiisoft/yii2/BaseYii.php on line 291

PHP message: PHP Stack trace:

PHP message: PHP   1. {main}() /home/wwwroot/php/zlxoc/trunk/admin/web/index.php:0

PHP message: PHP   2. yii\base\Application->__construct() /home/wwwroot/php/zlxoc/trunk/admin/web/index.php:17

PHP message: PHP   3. yii\base\Application->preInit() /home/wwwroot/php/zlxoc/trunk/vendor/yiisoft/yii2/base/Application.php:202

PHP message: PHP   4. yii\base\Application->setBasePath() /home/wwwroot/php/zlxoc/trunk/vendor/yiisoft/yii2/base/Application.php:223

PHP message: PHP   5. yii\base\Module->setBasePath() /home/wwwroot/php/zlxoc/trunk/vendor/yiisoft/yii2/base/Application.php:358

PHP message: PHP   6. spl_autoload_call() /home/wwwroot/php/zlxoc/trunk/vendor/yiisoft/yii2/base/Application.php:224

PHP message: PHP   7. yii\BaseYii::autoload() /home/wwwroot/php/zlxoc/trunk/vendor/yiisoft/yii2/base/Application.php:0

PHP message: PHP   8. spl_autoload_call() /home/wwwroot/php/zlxoc/trunk/vendor/yiisoft/yii2/base/Application.php:291

PHP message: PHP   9. yii\BaseYii::autoload() /home/wwwroot/php/zlxoc/trunk/vendor/yiisoft/yii2/base/Application.php:0" while reading response header from upstream, client: 192.168.26.97, server: admin.zlxoc.com, request: "GET /weixin/test.html HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "admin.zlxoc.com"

看过日志中间的问题,基本上锁定了一会儿200,一会儿500状态的根源在哪里,还是在open_basedir 上面。

于是重新开启把路径限制在项目根目录下,/etc/init.d/nginx restart ,不再出现此问题