Setup Laravel 5 in Amazon Elastic Beanstalk
Problem
This is my first time dealing with AWS. Initially, I setup my Laravel project in a normal EC2 instance, and manually install mysql, web server, etc.
Then a friend of mine told me that this kind of infrastructure is not scalable. Eventually I go for Elastic Beanstalk.
Create an AWS account
You need a valid credit card to perform this action. Just follow the instruction will do.
Create a new app in Elastic Beanstalk
Then choose Web server environment.
Platform select PHP
.
You can temporary choose Sample application first.
Or you can use command line to create (see here)
1 | cd /path/to/project/ |
Now you can click on the link above to see your sample page
Install eb
in your local
Once you have installed. Run the following command
1 | $ eb init |
You will be prompted to key in AWS credential. Refer the instruction here
Enable ssh login
Go to Configuration on your left side, then select Instances
If you don’t have a key, create it. Download it and put into your ~/.ssh/
directory.
NOTE: Bare in mind that whenever you ssh in and change something, the changes only temporary, once it scale up and down the changes will be lost
Add environment variables
If you notice, there is a file named .env.sample in all laravel project, and you need to rename/copy it as .env (without sample), this is because the configuration cannot be push to git repo, due to security reason.
Now, in elasticbeanstalk, if you ssh in and rename it to .env, then when it scale up and down, the .env will be lost, and the site may be down. Thus, the you have to add the config settings to environment variables
Configure RDS for your project
Go to Services (in the top left), and choose RDS
, then follow the instruction.
Now go to your instances
Get the link and put it in your environment variable.
1 | DB_HOST=zzzzz.xxxxxxxxxxxx.ap-southeast-1.rds.amazonaws.com |
Deploy
Now, the deployment is pretty simple (assume you’re using git here)
In your local, cd
to the project root, then run a command
1 | $ eb deploy <environment> |
It will deploy the latest commit, if you have any changes which not committed yet, it won’t be deployed.