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


November 25th, 2018, posted in tech_stuff
by Cristian

As you might have noticed if you’ve been following our work, we use Laravel a lot around here. Hey, we even made our own open-source admin panel Backpack for Laravel. And when it comes to hosting our Laravel-powered apps, we 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 with 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 blogpost 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 blogpost will be enough to get your app up and running some more customization might 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 enviroment
  • 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 enviroment



Step 0.2 Configure environment


  • Click on the newly created enviroment
  • 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
    • Git-related files, tests folder are not required
cd //
zip -r .zip
  • Access your AWS Console
  • Go to Elastic Beanstalk
  • Go to your Application's Dasboard
  • 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
    • Enter the desired Application name
    • Select EC2/On-premises as Compute Platform
    • Enter the desired Deployment group name
    • Deployment type is In-place deployment
    • 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)

version: 0.0
os: linux
    - source: /
      destination: /var/www/html/
        - location: scripts/
        - location: scripts/
  • Create a scripts folder in your project's root and include the scripts that you want to run
  •, from the above example, could look something like this
# Set permissions to storage and bootstrap cache
sudo chmod -R 0777 /var/www/html/storage
sudo chmod -R 0777 /var/www/html/bootstrap/cache
cd /var/www/html
# Run composer
sudo /usr/bin/composer.phar install --no-ansi --no-dev --no-suggest --no-interaction --no-progress --prefer-dist --no-scripts -d /var/www/html
# Run artisan commands
php /var/www/html/artisan migrate



Step 2.4 Deploy on Single instance environment


  • Ssh into your EC2 instance
ssh -i ~/.pem [email protected]
  • 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:
cd /home/ec2-user
curl -O
chmod +x ./install
sudo ./install auto
  • 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:
sudo yum install -y nodejs npm --enablerepo=epel
pip install awscli
php -r "copy('', 'composer-setup.php');"
sudo php composer-setup.php -- --install-dir=/home/ec2-user --filename=composer
sudo rm composer-setup.php
yum -y update
yum install -y ruby
cd /home/ec2-user
curl -O
chmod +x ./install
./install auto


  • Click Skip to review
  • Click Create laungh 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
- location: scripts/
- location: scripts/




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





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.

Or maybe you’re trying to do other cool stuff with Laravel. We’ve got you covered. Read our step-by-step guides to deploying a Laravel app on Heroku, on Google Compute Engine on LINUX, on Google App Engine or setting up an e-commerce platform?

About the author


I am an experienced web developer acting as Technical Director at UPDIVISION. I have been part of the team for over six years, which allowed me to gain the overall vision needed for the job. I’m a down-to-earth guy who can think on his feet. I need to, since I have to respond to problems quickly and decisively while maintaining the technological strategy and software architecture I came up with in the first place.

See more articles by Cristian