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
Firstly, run the following command to install Laravel framework and create a working project:
composer create-project --prefer-dist laravel/laravel
You can verify the installation by running it from localhost via the wamp server.
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.
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.
- Click Create New Application link.
- A window box will pop out, fill up the Application Name and Description fields.
- Click create button.
Create a new environment
You will see a new application was created under Elastic Beanstalk, click on Create one now to create an environment.
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.
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.
Finally, when it is ready, the health status will be changed to OK and turned to a green colour.
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:
- Go back to your Elastic Beanstalk page, on the left-hand side navigation bar, choose Configuration.
- In the Software category, click Modify button.
- Fill up /public in the Document root field.
- Click the Apply button.
Now reopen your site in the browser by the URL. You will be able to see your site.
In the next tutorial, we will walk you through how to integrate your Laravel application with MySQL using AWS Elastic Beanstalk.