.env / user_vars环境变量返回NULL

时间:2016-07-08 13:42:44

标签: php nginx openshift laravel-5.2

我正在尝试使用Opennshift设置Laravel 5.2 https://github.com/boekkooi/openshift-cartridge-php https://github.com/boekkooi/openshift-cartridge-nginx

我可以使用action_hooks设置像APP_DEBUG这样的用户变量,但是这些环境变量在Laravel文件中返回NULL。请提出解决此问题的方法!我错过的是没有读取.env / user_vars /目录中的变量。

编辑1: 以下是action_hooks / deploy

的代码片段
echo "Setting environment variables..." 1>&2
export APP_ENV="${APPLICATION_ENV:-production}"
if [[ "${APPLICATION_ENV:-}" != "development" ]]; then
  export APP_DEBUG=false
else
  export APP_DEBUG=true
fi

echo "App Debug Mode '$APP_DEBUG' '$APP_ENV'..." 1>&2

if [ "$(type -t set_env_var)" == "function" ]; then
  set_env_var 'APP_ENV' $APP_ENV $OPENSHIFT_HOMEDIR/.env/user_vars
  set_env_var 'APP_DEBUG' $APP_DEBUG $OPENSHIFT_HOMEDIR/.env/user_vars
fi

if [ ! -z "$OPENSHIFT_APP_DNS" ]; then
  export APP_URL="$OPENSHIFT_APP_DNS"
  echo "App URL '$APP_URL' '$OPENSHIFT_APP_DNS'..." 1>&2
  if [ "$(type -t set_env_var)" == "function" ]; then
    set_env_var 'APP_URL' $APP_URL $OPENSHIFT_HOMEDIR/.env/user_vars
  fi
fi

if [ ! -z "$OPENSHIFT_SECRET_TOKEN" ]; then
  export APP_KEY=davjhdfgdhayufgahgajgfdshadjaj
  echo "App KEY '$APP_KEY' '$OPENSHIFT_SECRET_TOKEN'..." 1>&2
  if [ "$(type -t set_env_var)" == "function" ]; then
    set_env_var 'APP_KEY' $APP_KEY $OPENSHIFT_HOMEDIR/.env/user_vars
  fi
fi

echo "App Debug Mode '$APP_DEBUG' '$APP_ENV'..." 1>&2
echo "App KEY '$APP_KEY' '$OPENSHIFT_SECRET_TOKEN'..." 1>&2

Followed by the rest of code...

部署时的输出给了我

remote: Setting environment variables...
remote: App Debug Mode 'true' 'development'...
remote: App URL 'laravel-narsariamanu.rhcloud.com' 'laravel-narsariamanu.rhcloud.com'...
remote: App KEY 'davjhdfgdhayufgahgajgfdshadjaj' 'Ifm2BJ_o9pvLMyEykfwApI5FRLsZ5BZyGI54m2Lqu46XLLtx4aOWjFKYGfTOL3hhkRvm6dSLFmSHBP5xTKgAdtChXYXr6HEgq92qco4dC4fE-CkQR-82RV7UYE0N7_nq'...
remote: App Debug Mode 'true' 'development'...
remote: App KEY 'davjhdfgdhayufgahgajgfdshadjaj' 'Ifm2BJ_o9pvLMyEykfwApI5FRLsZ5BZyGI54m2Lqu46XLLtx4aOWjFKYGfTOL3hhkRvm6dSLFmSHBP5xTKgAdtChXYXr6HEgq92qco4dC4fE-CkQR-82RV7UYE0N7_nq'...
remote: 
remote: Database driver set to 'mysql'...
remote: Database set to '127.5.76.2' 'mysql' 'laravel' 'admin4QlGeTU' '7eTw_XGG4dSx'...
remote: OPENSHIFT Database set to '127.5.76.2' 'mysql' 'laravel' 'admin4QlGeTU' '7eTw_XGG4dSx'...
remote: 
remote: 
remote: Migrating storage and log directories...
remote: 
remote: 
remote: Running 'php artisan migrate --force'...
remote: 
remote: Nothing to migrate.
remote: Checking configuration
remote: - php-fpm.ini: No change
remote: - php-fpm.conf: No change
remote: - php.ini: No change
remote: PHP-FPM already running
remote: Nginx instance is started
remote: -------------------------
remote: Git Post-Receive Result: success
remote: Activation status: success
remote: Deployment completed with status: success

在Laravel的config / app.php中:

/*
|--------------------------------------------------------------------------
| Encryption Key
|--------------------------------------------------------------------------
|
| This key is used by the Illuminate encrypter service and should be set
| to a random, 32 character string, otherwise these encrypted strings
| will not be safe. Please do this before deploying an application!
|
*/

'key' => env('APP_KEY'),

'cipher' => 'AES-256-CBC',

这个env(' APP_KEY')返回NULL,但是在部署它时已经设置了。

请帮我解决这个问题。

3 个答案:

答案 0 :(得分:1)

可能有点晚了但是答案就是这样。 https://github.com/laravel/framework/issues/8191

作为总结,建议在配置文件中设置值(我将它们引用到我的env文件中)。然后你可以从配置文件调用。 \Config::get('app.env')而不是env('APP_ENV'),在我设置的配置文件夹中的app.php文件中: 'env' => env('APP_ENV', 'production'),

php artisan config:cache没有解决我的问题。

我希望它可以帮到你们。 欢呼声。

答案 1 :(得分:0)

在你的php.ini文件中检查你的variables_order。如果它读取" GPCS"尝试将E添加到开头并重新启动服务器,然后查看会发生什么。

如果.env中的任何行有空格而且未包含在"然后它就会失败。

有些事情要尝试

  • 检查.env文件的权限。尝试将其设置为777作为 测试,看看会发生什么。 (如果有效,请不要将其保留在777)

  • 运行php artisan config:cache和php artisan config:clear command。

  • 重新启动您的应用服务器。

答案 2 :(得分:0)

有时当您更改了.env文件时,它没有从中获取正确的值,问题是由于某些配置缓存。尝试运行以下命令,希望能够正常工作

php artisan config:cache   //flush all cached env variable
php artisan config:clear   //repopulate all the env variable
php artisan cache:clear    //flush all the cached content