mercredi 25 février 2015

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

Note I'm a beginner :)


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.


I have all the variables I need, I just don't know where to put them and set things up so laravel automatically detects the environment and uses the right variables.


My research so far


There is already questions about laravel 5 env variables here on stackoverflow.




  1. What's the correct way to set ENV variables in Laravel 5?




  2. Laravel 5 configuration - environments and overriding




  3. How to have environment specific .env files for dotenv (in Laravel 5)




  4. Laravel 5 Changing Environment to match URL




All these answers have led me in the right direction, but I still don't quite get where to make all the changes they describe.



  • Should I create more than one .env file? If yes, what should I call it and how does a configured config/database.php file look like?

  • Answer to question 1 above mentions the bootstrap/environment.php file needs to be configured. I don't have this file (installed laravel with composer - several composer update 's have been run). Do I need it or...?

  • Answer to question 4 above says I have to use Config::set('database.default', "mysql") or Config::set('database.default', "mysql2" after having configured .env and config/database.php. But where exactly is it I put this line of code?


Besides the above I have also seen Jeffrey Way's Laracast on Environment Configuration, but this didn't include a concrete example, so I'm still not sure where to put what and how to configure it correctly...


Yes, I have also read the official documentation for environment configuration, but this was also an too high level explanation for me to understand.



You may access the current application environment via the environment method on the Application instance:



$environment = $app->environment();


You may also pass arguments to the environment method to check if the environment matches a given value:



if ($app->environment('local'))
{
// The environment is local
}

if ($app->environment('local', 'staging'))
{
// The environment is either local OR staging...
}


To obtain an instance of the application, resolve the Illuminate\Contracts\Foundation\Application contract via the service container. Of course, if you are within a service provider, the application instance is available via the $this->app instance variable.


An application instance may also be accessed via the app helper or the App facade:



$environment = app()->environment();

$environment = App::environment();



  • I assume "The app helper or the app facade" refers to bootstrap/app.php. If this is correct, how do I set things up so laravel knows whether I'm in production or development? Where do I put my database variables from AWS?


Finally AWS has their own option to pass in environment variables (see pic below). Should I use this instead, and if yes, how do I then set up things?


AWS configurable environment variables


In conclusion: I'm confused...HALP!


I know it is much to ask, but I really need a concrete example on how to do this. A screen shot of a proper setup perhaps, or maybe a clear step-by-step instruction explaining what path/example.php I need to change, and what this change looks like.


Thanks for taking your time. It is appreciated a lot.





Aucun commentaire:

Enregistrer un commentaire