dotenv和Elastic Beanstalk - 环境文件.env未找到或无法读取

时间:2015-12-14 22:20:52

标签: laravel amazon-web-services elastic-beanstalk lumen laravel-dotenv

我试图在Amazon Elastic Beanstalk中上传Lumen项目。

.env在.gitignore。

这没关系,因为我有几个environement(dev,qa,prod),所以我需要为每个环境配置单独的env变量

我收到此错误消息:

 Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Dotenv: Environment file .env not found or not readable. Create file with your environment settings at /var/app/current/bootstrap/../.env' in /var/app/current/vendor/vlucas/phpdotenv/src/Dotenv.php:33 Stack trace: #0 /var/app/current/bootstrap/app.php(4): Dotenv::load('/var/app/curren...') #1 /var/app/current/public/index.php(13): require('/var/app/curren...') #2 {main} thrown in /var/app/current/vendor/vlucas/phpdotenv/src/Dotenv.php on line 33

我知道系统找不到.env

我在亚马逊控制台中设置了变量:

Software Configuration
Environment variables: APP_ENV, DB_USERNAME, DB_PASSWORD, DB_DATABASE,  DB_HOST, APP_KEY

eb printenv:

 Environment Variables:
  DB_DATABASE = ebdb
  DB_PASSWORD = xxxxxxxx
  APP_KEY = xAY4hnrXlht5fdvB9PzPAwDqc1R
  DB_HOST = xxxxxxcnzd3rux8ue7.us-east-1.rds.amazonaws.com:3306
  APP_ENV = dev
  DB_USERNAME = myuser

我也有.ebextensions / environment.config:

 container_commands:
 # Copy EB env configuration file over
 01_config_environment:
  command: mv /var/app/ondeck/.env.elasticbeanstalk /var/app/ondeck/.env
 02-install-packages:
command: "composer.phar install -d /var/app/ondeck/www"
 option_settings:
 option_name: DB_HOST
 value: xxxxxxx.cnzd3rux8ue7.us-east-1.rds.amazonaws.com
- option_name: DB_PORT
 value: 3306
- option_name: DB_NAME
 value: ebdb
- option_name: DB_USER
 value: myuser
- option_name: DB_PASS
 value: xxxxxx

但是不能摆脱这个错误!

1 个答案:

答案 0 :(得分:0)

流明 5.0, 5.1

如果您使用的是 Lumen < 5.2,您可以更新正在加载 .env 文件的 bootstrap/app.php 文件。只需捕获异常并忽略它。

try {
    Dotenv::load(__DIR__.'/../');
} catch (InvalidArgumentException $e) {
    //
}

Lumen 5.2 - 5.7 没有这个问题,因为他们基本上已经完成了上述工作。

流明 5.8+

Lumen 5.8 重新引入了这个问题,也让它变得更难解决。为此,您需要创建一个扩展 \Laravel\Lumen\Bootstrap\LoadEnvironmentVariables 的类,并覆盖 bootstrap() 方法。例如,创建一个新文件 app/Bootstrap/MyLoadEnvironmentVariables.php:

<?php

namespace App\Bootstrap;

use Dotenv\Exception\InvalidFileException;
use Laravel\Lumen\Bootstrap\LoadEnvironmentVariables;

class MyLoadEnvironmentVariables extends LoadEnvironmentVariables
{
    public function bootstrap()
    {
        try {
            $this->createDotenv()->safeLoad();
        } catch (InvalidFileException $e) {
            //
        }
    }
}

现在,您需要更新 bootstrap/app.php 文件以使用新类而不是基类:

(new App\Bootstrap\MyLoadEnvironmentVariables(
    dirname(__DIR__)
))->bootstrap();