How to host a Laravel application on AWS Elastic Beanstalk

In this tutorial, I’m going to walk through configuring a Laravel environment on AWS Beanstalk. AWS Beanstalk is a PaaS service, which means we don’t have to install PHP, configure Apache as it is already installed and managed by AWS.

This is great! It lets us concentrate on releasing new features.

Requirements:

  1.  An environment created in the AWS Elastic Beanstalk
  2. Source code hosted in GIT
  3. Experience using Elastic Beanstalk

Step 1: Initialize EB CLI in the Laravel Directory

To be efficient we will need to manage our AWS Elastic Beanstalk environment through code. This is why we will add Elastic Beanstalk CLI to our working directory.  When we initialize EB CLI inside a GIT directory, Elastic Beanstalk will deploy the changes from the GIT repository.

Inside the Laravel directory, run the following command below

eb init

This command would make your current working directory as a Beanstalk project. The next stages would prompt you to select the region which you have created the environment.

Key things to know about Beanstalk!

  • You should avoid SSH into the server to make any changes!
  • It handles the scaling automatically (If you have configured with EFS it becomes much easier)
  • Beanstalk Hooks – very important to know (https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/custom-platform-hooks.html)

Step 2: Adding AWS Elastic Beanstalk support to our Laravel project

To configure our AWS Elastic Beanstalk environment via code, we have to create a directory called “.ebextensions” inside our Laravel project. Files inside “.ebextensions” are considered as the configuration settings of our Elastic Beanstalk environment. We could change the settings our environment in this directory.

  1. Create a directory “.ebextensions” inside the Laravel project.
  2. Create a file called “01_setup.config”  in it.

The naming of files inside the .ebextension is important. Beanstalk runs the files in an alphabetical order. It is a best practice to prefix numbers to the name of the files.

Things we would need to change in our Elastic Beanstalk environment.

  1. document_root. Laravel handles requests from the public directory. We cannot modify the virtual host configuration of the server, as a result, we have to change the settings through the composer.config file.
  2. Hooks: A fresh installation of Laravel does not have the “.env” file. To automate this process we have to use a post-deployment hook and write a shell script.

01_setup.config

“.ebextensions/composer.config”

option_settings:
  aws:elasticbeanstalk:container:php:phpini:
    document_root: /public
files:
  "/opt/elasticbeanstalk/hooks/appdeploy/post/02_configure_environment_file.sh":
      mode: "000755"
      owner: root
      group: root
      content: |
        #!/bin/bash
        if [ ! -f /var/www/html/.env ] ; then
            cp /var/www/html/.env.example /var/www/html/.env
        else
           OUTPUT="$(egrep "^APP_KEY=(.+)$" /var/www/html/.env)"
           if [ -z "$OUTPUT" ]; then
                php  /var/www/html/artisan key:generate
            fi
        fi

The script will copy the “.env” file if it cannot find one. If the “.env” file exists and it does not contain the “APP_KEY” it will generate one.