Question:
I’m trying to upload a Lumen project in Amazon Elastic Beanstalk.
.env is in .gitignore.
This is OK, because I have several environement ( dev, qa, prod), so I need to configure have separate env variable for each environement
I get this error message:
1 2 |
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 |
I understand that system doesn’t find .env
Thing is I have set variables in Amazon Console :
1 2 3 |
Software Configuration Environment variables: APP_ENV, DB_USERNAME, DB_PASSWORD, DB_DATABASE, DB_HOST, APP_KEY |
eb printenv :
1 2 3 4 5 6 7 8 |
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 |
I also have in .ebextensions/environment.config :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
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 |
But can’t get rid of this error!
Answer:
Lumen 5.0, 5.1
If you’re on Lumen < 5.2, you can update your bootstrap/app.php
file that is loading the the .env file. Just catch the exception and ignore it.
1 2 3 4 5 6 |
try { Dotenv::load(__DIR__.'/../'); } catch (InvalidArgumentException $e) { // } |
Lumen 5.2 – 5.7 don’t have this issue, since they basically do the above already.
Lumen 5.8+
Lumen 5.8 reintroduced the issue, and also made it a little bit harder to get around. For this, you need to create a class that extends \Laravel\Lumen\Bootstrap\LoadEnvironmentVariables
, and override the bootstrap()
method. For example, create a new file app/Bootstrap/MyLoadEnvironmentVariables.php
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
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) { // } } } |
Now, you need to update your
bootstrap/app.php
file to use your new class instead of the base class:
1 2 3 4 |
(new App\Bootstrap\MyLoadEnvironmentVariables( dirname(__DIR__) ))->bootstrap(); |