AWS Laravel

Laravel application with MySQL using AWS Elastic Beanstalk – Part 1

In this tutorial, I will walk you through step-by-step of how to deploy a Laravel application with MySQL database using AWS Elastic Beanstalk environment.

Installing Laravel Project

It is extremely simple to deploy a Laravel application to AWS using Elastic Beanstalk. I will be using Composer to install Laravel framework, if you haven’t already, start by installing Composer.

Firstly, run the following command to install Laravel framework and create a working project:

composer create-project --prefer-dist laravel/laravel
Installing Laravel via Composer
Installing Laravel via Composer

You can verify the installation by running it from localhost via the wamp server.

Laravel running localhost successfully.

It is running well. Now we can start to upload the Laravel project into Elastic Beanstalk.

Go to the root folder of your Laravel project, select all the files inside root folder except for vendor. Then, right-click the selected files and zip them up.

Laravel Project Structure
Laravel Project Structure

If you are using MAC book to do above step, do remember to CRT+ Command + Space to show the hidden files. Otherwise, you will miss out the .env file. Also to take note, after zipping the files, you need to run command to eliminate __MACOSX folder.

Deploy your Laravel application to AWS

Login into your AWS Management Console, and select Elastic Beanstalk. Follow the step below to create an environment.

  1. Click Create New Application link.
  2. A window box will pop out, fill up the Application Name and Description fields.
  3. Click create button.
Deploy Laravel Application to AWS using Elastic Beanstalk
Creating an application using Elastic Beanstalk

Create a new environment

You will see a new application was created under Elastic Beanstalk, click on Create one now to create an environment.

Creating Web Server Environment using Elastic Beanstalk

Select Web Server environment and click Select button. Worker environment is for the batch job.

Fill up the Domain field. This will be the access URL for our Laravel web application.

Under Base configuration, Platform section, choose Preconfigure platform and select PHP.

On Application code section, choose to Upload your code, click on the Upload button. Select the zip file you have created on the previous step.

Next, Click Create environment button. In this tutorial, we will just skip the Configure more options which you can configure Load Balancer and Auto Scaling.

Configuring Web Server Environment using AWS Elastic Beanstalk
Configuring Web Server Environment using Elastic Beanstalk

That is all, the Elastic Beanstalk will create the EC2 instance, IP address and other necessary resources for you. The process will take a few minutes.

Elastic Beanstalk creating environment
Elastic Beanstalk creating an environment

Finally, when it is ready, the health status will be changed to OK and turned to a green colour.

Deploy Laravel Application to AWS using Elastic Beanstalk
Successfully created a webserver environment and deployed the Laravel application into AWS using Elastic Beanstalk

You may now access the web application via the highlighted URL, which is the Domain you have created on the previous step.

However, you may encounter a Forbidden error. By default, Elastic Beanstalk serves the root of your project at the root path of your web project.

There are two ways to resolve this, you can either add a /public into your URL and reaccess again. Or else you can follow the step below:

  1. Go back to your Elastic Beanstalk page, on the left-hand side navigation bar, choose Configuration.
  2. In the Software category, click Modify button.
  3. Fill up /public in the Document root field.
  4. Click the Apply button.

Now reopen your site in the browser by the URL. You will be able to see your site.

Deploy Laravel Application to AWS using Elastic Beanstalk
Successfully deployed Laravel application into AWS using Elastic Beanstalk

In the next tutorial, we will walk you through how to integrate your Laravel application with MySQL using AWS Elastic Beanstalk.

Leave a Reply