16

I have successfully deployed my laravel 5 app to AWS EC2. I have also created a MySQL database with AWS RDS and associated it with my app instance.

Now I want to set my env variables so it uses homesteads default values when on my local machine in development, and my AWS database when deployed and in production.

From here I've made a major edit to my original question to reflect what I've learned since asking it

The classic .env in a laravel project for local development looks roughly like this:

APP_ENV=local
APP_DEBUG=true
APP_KEY=BF3nmfzXJ2T6XU8EVkyHtULCtwnakK5k (Note, not a real key)

DB_HOST=localhost
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret

CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null

For production, I've finally understood that I simply create a new .env file with my production variables. When using AWS, my .env file would roughly look like this:

APP_ENV=production
APP_DEBUG=false
APP_KEY=BF3nmfzXJ2T6XU8EVkyHtULCtwnakK5k (Note, not a real key)

DB_HOST=aaxxxxxxxxxxxxx.cyxxxxxxxxxx.eu-central-1.rds.amazonaws.com:3306
DB_DATABASE=MyAppsDatabaseName
DB_USERNAME=MyAWSRDSUserName
DB_PASSWORD=NotARealPassword

CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null

My question/problem now

I use AWS eb cli to deploy my app from git. But how do I deploy my production .env file without having to push it to git first?

MartinJH
  • 2,590
  • 5
  • 36
  • 49
  • Have you included your .env file in source control ? DONT – ultimate Feb 25 '15 at 14:44
  • No, I haven't :) It is ignored by default in .gitignore. But I assume it is uploaded to aws when I do `eb deploy`. Otherwise laravel can't read it... right? – MartinJH Feb 25 '15 at 14:51
  • 1
    Not in practice, but I think I found the method to do it. It is described by Jeffrey Way on his [Laracast 5 episode 17 "Midterm Review"](https://laracasts.com/series/laravel-5-fundamentals/episodes/17). He starts reviewing .env at 8.31, and at 9.14 he says: "_So once you would deploy this, you would creat another .env file on your production end. So that is the important thing to understand._" Question for me now is, how do I deploy a file to AWS without pushing it to git first... – MartinJH May 20 '15 at 18:23
  • Exactly what we are pondering... – Phil Lawlor May 20 '15 at 18:41

2 Answers2

22

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

Community
  • 1
  • 1
MartinJH
  • 2,590
  • 5
  • 36
  • 49
  • 1
    Thanks for this detail, it helped me get running as well. One note I'll add: my last headache before smooth deploys was updating the RDS instance's security group to allow connections from the ec2 app itself. Found a useful (tho slightly outdated) guide here: http://blog.goforyt.com/laravel-aws-elastic-beanstalk-dev-guide/ – Russ Matney Jun 02 '15 at 14:49
  • @MartinJH any way to get in touch? I am finding this REALLY hard and can use a hand... – ackerchez Dec 13 '16 at 10:31
  • @ackerchez Feel free to write your questions here, or send me an email at martin_hammer AT live.dk :) – MartinJH Dec 13 '16 at 11:06
  • I added all the env variables to the AWS console however I can not access them in the Laravel application. ssh-ing into the instance and running $_SERVER or $_ENV doesn't include any of the variables that I added in the AWS console. – A. Atal Dec 15 '22 at 22:19
17

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).

Community
  • 1
  • 1
Russ Matney
  • 1,165
  • 13
  • 16
  • 1
    I have seen your answer. Will report back with findings and accept answer if valid AFTER I get that damn [`php artisan migrate` to work on my eb instance...](http://stackoverflow.com/questions/30539031/php-fatal-error-on-php-artisan-migrate-on-remote-aws-eb-instance-laravel-log) – MartinJH May 29 '15 at 21:18