Question:
I’m trying to deploy my Laravel application to Elastic Beanstalk in development mode. To make the application run in development mode rather than production, I’ve done the following in my /bootstrap/start.php
file:
1 2 3 4 |
$env = $app->detectEnvironment(function() { return $_ENV['ENV_NAME']; }); |
To actually create the environment variable, I’ve created a .config
file in the following path: /.ebextensions/00environmentVariables.config
with these contents:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
option_settings: - namespace: aws:elasticbeanstalk:application:environment option_name: ENV_NAME value: development - option_name: DB_HOST value: [redacted] - option_name: DB_PORT value: [redacted] - option_name: DB_NAME value: [redacted] - option_name: DB_USER value: [redacted] - option_name: DB_PASS value: [redacted] |
When I run eb start
from the command line, it spins up an EC2 instance and attempts to provision it, at which point it tells me it fails. and to check the logs. In the logs, I can see these entries:
PHP Notice: Undefined index: ENV_NAME in
/var/app/ondeck/bootstrap/start.php on line 28Notice: Undefined index: ENV_NAME in /var/app/ondeck/bootstrap/start.php on line 28
So for some reason, the ENV_NAME
environment variable doesn’t exist, even though I’ve specified it in 00environmentVariables.config
. What’s even weirder, is that I can see the environment variable does exist under the software configuration settings of the EB environment:
To summarize:
- I know my .config files are being parsed from looking at the logs
- For some reason my Laravel application still doesn’t think that
ENV_NAME
eixsts ENV_NAME
eixsts both in the.config
file and in my Elastic Beanstalk settings for this environment
EDIT
Alright so I worked out that the environment variables do work correctly when serving the application over the Apache HTTP server, but the environment variables don’t exist when running the PHP CLI.
In the above logs, it’s complaining about ENV_NAME
not existing when running a /usr/bin/composer.phar install
.
So, for some reason, my environment variables don’t exist within the PHP CLI but they do work normally when serving over Apache.
FURTHER EDIT
So I SSH’d into the EC2 instance that’s hosting my Laravel application on Elastic Beanstalk, and I can see the proper environment variables when I use the printenv command`:
1 2 |
ENV_NAME=development |
However, if I do a die(var_dump($_SERVER));
and run the PHP CLI, I don’t see the environment variables that I’ve defined. Same story with $_ENV
and getenv()
.
Why can’t I access my environment variables within the PHP CLI, when I can access them when Apache processes my PHP scripts?
YET ANOTHER EDIT
I made a test.php
file with one line: die(var_dump($_ENV));
.
When I run this using php test.php
I successfully get my custom environment variables coming out, so this seems like a composer only problem, not a PHP CLI problem.
Answer:
I use a YAML script which sets the environment variables for the root user from the existing variables set for ec2-user. Add this to your .ebextensions
folder with the .config
extension.
From there you can run PHP cli and it will see the correct environment variables
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
commands: create_post_dir: command: "mkdir /opt/elasticbeanstalk/hooks/appdeploy/post" ignoreErrors: true files: "/opt/elasticbeanstalk/hooks/appdeploy/post/job_after_deploy.sh": mode: "000755" owner: root group: root content: | #!/usr/bin/env bash source /opt/elasticbeanstalk/support/envvars # Run PHP scripts here. # |
From XuDing’s answer to this question and this answer