【问题标题】:dotenv and Elastic Beanstalk - Environment file .env not found or not readabledotenv 和 Elastic Beanstalk - 环境文件 .env 未找到或不可读
【发布时间】:2016-03-20 13:31:18
【问题描述】:

我正在尝试在 Amazon Elastic Beanstalk 中上传一个 Lumen 项目。

.env 在 .gitignore 中。

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

我收到此错误消息:

 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

问题是我在 Amazon 控制台中设置了变量:

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

eb 打印环境:

 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

但是无法摆脱这个错误!

【问题讨论】:

  • 你找到答案了吗?
  • 不,我没有找到答案

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


【解决方案1】:

流明 5.0、5.1

如果您使用的是 Lumen 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();

【讨论】:

    猜你喜欢
    • 2020-08-05
    • 2021-11-25
    • 2020-05-29
    • 1970-01-01
    • 2019-02-19
    • 2016-09-08
    • 1970-01-01
    • 2018-06-05
    • 2015-09-18
    相关资源
    最近更新 更多