How to set environment variables for Laravel 5 on AWS EC2 with MySQL How to set environment variables for Laravel 5 on AWS EC2 with MySQL git git

How to set environment variables for Laravel 5 on AWS EC2 with MySQL


Russ Matney above gave the right answer, so he gets the checkmark. I'll write my own answer here to add in details on how I made things work. I assume you do have your database set up and have all the credentials you need.

1. Go to your elastic beanstalk dashboard

eb dashboard


2. Next go to your software config

eb software config


3. Add your production environment variables as shown below. Remember to set the doc root to /public, and also add :3306 at the end of your database end point to avoid the PDOEXCEPTION error.

See bigger version of picture below

environment variable configuration page


4. Next SSH into your apps eb instance. See details here, or try the following below:

$ ssh -i path/to/your/key/pair/pem/file.pem ec2-user@ec1-11-11-11-111.eu-central-1.compute.amazonaws.com

Note the ec1-11-11-11-111.eu-central-1.compute.amazonaws.com is your apps public DNS. You'll find yours right here:

public dns at EC2 instance dashboard


5. cd to your app: $ cd /var/app/current


6. Give read/write access to your storage folder or the app can't write to the logs folder and that'll result in an error when running the migrations. To give access: $ sudo chmod -R ugo+rw storage


7. Finally! Run your migrations and do other artisan commands if you please! $ php artisan migrate Success should roughly look like this from gitbash:

Migration success on aws eb


You could create a new .env on your ec2 instance and add all the env vars in there. One option would be ssh-ing into the box and creating the file via vi or cat. Or you could write a script to remotely pull the .env in from an external location.

You could also ssh into the box and export APP_ENV=production all your env vars (assuming that's the right command for your OS).

Adding env vars to your environment will depend on the OS that your ec2 instance is running, so the solution will depend on that. ec2 has a concept of 'tags' which might be useful, but the docs show they limit the number of tags to 10, so you may have to do it manually and per ec2 instance :/

See here for one method that uses tags to pull in and set env vars (non-laravel specific).

I just went through this yesterday while getting Laravel running on Elastic Beanstalk, the solution was clean. You can actually set the env vars directly via the aws console (EB app/environment -> Configuration -> Software Configuration -> Environment Properties).

Update:

The key concept to understand is that Laravel just uses phpdotenv to dump vars from the .env file into php's global $_ENV, whereas any already existing env vars are automatically included in $_ENV when php starts the server (docs). So the .env file itself is unnecessary, really just a dev convenience. (unless I've just been spoiled by elastic beanstalk so far).