DEV Community

Cover image for How to Deploy Laravel on Amazon Web Services: A Detailed Step-by-step Tutorial
UPDIVISION
UPDIVISION

Posted on • Updated on • Originally published at updivision.com

How to Deploy Laravel on Amazon Web Services: A Detailed Step-by-step Tutorial

Our developers have been through a lot. These are their stories. This one is told by Cristi, one of our senior app-crafters.

If you're a web developer, chances are you've heard about Laravel. I, for one, use it a lot. Laravel is an open source, model-view-controller framework for PHP - what's not to like?

And when it comes to hosting Laravel-powered apps, I often use Amazon Web Services (AWS), specifically Elastic Beanstalk. It's a solid hosting solution with just about all the infrastructure you need (load balancers, auto scaling, relational database, custom VPC).

Laravel on AWS is a combo that performs very well, which makes it very popular among web developers. This doesn't make it easy to deploy. It doesn't help that there's limited info out there. There's the rare blog post that addresses only parts of the entire process. Yes, Amazon put out some documentation, but it leaves you scratching your head more times than it clears things up.

I've hit my head against the wall plenty of times trying to deploy our Laravel-built projects on AWS. I wrote this article so you don't have to.

Before we begin, I wanted to say that I tried to make this guide as friendly as possible for people with little to no experience with configuring servers. I included a bunch of reference links in case you want to read more about what's being explained.

While this post will be enough to get your app up and running some more customization can be done though reading the documentation.

0. Prerequisites

You should already have an account with AWS. If you don't, create one and then keep reading.

Step 0.1 Create environment

  • Access your AWS Console
  • Go to Elastic Beanstalk
  • Click Create Application and complete the form
  • For you new Application actions Actions > Create environment
  • In the new window select Web server environment and click Select
  • Complete the form: → Choose PHP as your Preconfigured platform → For Application code select Sample application → Click Create environment
  • If you want to modify more settings click Configure more options
  • Make the desired changes (preferably set/create the key pair used for ssh)
  • Click Create environment

Step 0.2 Configure environment

  • Click on the newly created environment
  • In the left side menu click Configuration
  • Click Modify from Software
  • In Container Options: → Set Document root to /public → Make other necessary configurations
  • In Environment properties put all the configuration information you have in your project's .env file
  • Click Apply

1. Upload archive via web browser

  • On your local machine go to your project's root folder
  • Archive the project into a .zip file
  • Make sure the vendor folder is included
  • For Git-related files, tests folder are not required

scr1

  • Access your AWS Console
  • Go to Elastic Beanstalk
  • Go to your Application's Dashboard
  • Click Upload and Deploy
  • For Upload application choose the archive you've just created
  • For Version label enter the new version number
  • Click Deploy

2. Using Code Deploy

Step 2.1 Configure Code Deploy Application in AWS

  • Access your AWS Console
  • Go to CodeDeploy
  • Click Create application
  • Complete the form: a.) Enter the desired Application name b.) Select EC2/On-premises as Compute Platform c.) Enter the desired Deployment group name d.) Deployment type is In-place deployment e.) Environment configuration: If your web application uses load balancing > select the load balancer’s Auto scaling group. If your web application doesn’t uses load balancing > select the Amazon EC2 instance
  • For Deployment configuration select CodeDeployDefault.OneAtATime
  • For Service role select a service role that grants AWS CodeDeploy access to the instances (for example arn:aws:iam::34325423432:role/elasticbeanstalk-ec2-role)
  • Click Save
  • Now you have your Deployment Group (a collection of EC2 instances that CodeDeploy will execute on for each deployment)

Step 2.2 Configure Code Deploy in Bitbucket

  • Install CodeDeploy add-on in Bitbucket
  • Click Settings in your Bitbucket project repository
  • Click CodeDeploy Settings
  • If you don’t have an AWS Role: →Follow the instruction to create a new AWS Role →Select the Role ARN and Region from the dropdowns →Click Save & Continue
  • If you have an AWS Role: →Select the AWS Application (that you created above in Step 2.1) and S3 Bucket to use →Click Save

Step 2.3 YML file configuration

You need to tell CodeDeploy how to deploy to your instances by using an appspec.yml. Then, add appspec.yml to your project’s root (see an example below):

scr2

  • Create a scripts folder in your project’s root and include the scripts that you want to run
  • AfterInstall.sh, from the above example, could look something like this:

scr3

Step 2.4 Deploy on Single instance environment

  • Ssh into your EC2 instance:
    ssh -i ~/.pem ec2-user@ec2-xx-xx-xx-xxx.compute-1.amazonaws.com

  • Check if the CodeDeploy agent is installed end running:
    sudo service codedeploy-agent status

  • If you see a message like error: No AWS CodeDeploy agent running, start the service:
    sudo service codedeploy-agent start

  • If you see an error message like codedeploy-agent: command not found you must Install CodeDeploy agent. To do so run the code below:

scr4

  • If the CodeDeploy agent is successfully installed go to Bitbucket and make your first deploy.

Step 2.5 Deploy on Load Balanced environment

  • Access your AWS Console
  • Go to EC2
  • Click Auto Scaling Groups in the left menu
  • Select the group that corresponds to your load balancer
  • Take note of the Launch Configuration name that corresponds to the Auto Scaling Group
  • Click Launch Configurations in the left menu
  • Select the configuration that you need
  • Click Actions > Copy launch configuration
  • On the new page click Edit details
  • Click Advanced Details and in the User data text area after Content-Disposition: attachment; filename=”user-data.txt” add:

scr5

  • Click Skip to review
  • Click Create launch configuration
  • Select a key pair
  • Check I acknowledge that I have access to the selected private key file, and that without this file, I won’t be able to log into my instance and click Create launch configuration
  • Click Auto Scaling Groups in the left menu and then select your balancer’s group
  • Click Actions > Edit
  • In the Details tab for Launch Configuration select the newly created configuration and click Save
  • Now you need to update the appspec.yml file that is in your project root with the following code

scr6

  • Add RegisterWithELB.sh and DeregisterFromELB.sh in your project’s scripts folder
  • Push modifications to Bitbucket

Step 2.6 Deploy from Bitbucket

  • Go to your project’s repository on Bitbucket
  • Click Branches
  • Click the branch that you want to deploy to AWS
  • Click Deploy to AWS
  • Choose desired Deployment Group
  • Click Start Deployment
  • Now you can check on the status of the deployment in the CodeDeploy console

Conclusion

With a bit of luck, at this point you should have a Laravel app running on Elastic Beanstalk. Feel free to start tweaking the setup and config files to meet your needs. What’s next? If you’re thinking of monitoring your environment or integrating with other AWS services, have a look at the Elastic Beanstalk documentation.

I hope you’ve enjoyed this article about deploying Laravel to AWS using Elastic Beanstalk. Do you have any hints or tips of your own? Or maybe some questions? Feel free to ask in the comments.

Happy coding!

Top comments (0)