Host & Deploy a Laravel App to AWS Elastic Beanstalk in 2023

CraftyTechie is reader-supported. When you buy through links on our site, we may earn an affiliate commission.

Step by Step Guide to Hosting & Deploying a Laravel PHP app to AWS Elastic Beanstalk

This article shows how to setup & deploy a Laravel app to Elastic Beanstalk. It includes the environment setup as well as discussing application deployment. Read on to get started.

FuelingPHP features a complete guide for hosting PHP applications in AWS, where it goes through alot of information that will set the context for this article. You can also learn about AWS Elastic Beanstalk, its upsides, downsides, and use cases. So, it is highly recommended to go through it.

Background on AWS Beanstalk. What is it?

Just for a quick intro – Elastic Beanstalk is AWS’s platform-as-a-service that deploys applications, orchestrating the underlying resources and thereby helping you in effortless deployments.

Deploy PHP App to Elastic Beanstalk
AWS Elastic Beanstalk Logo

Elastic Beanstalk supports a number of different platforms, including .NET, PHP, Python, Go, Docker, and Node. One cool thing is that EBS doesn’t have any additional charges but charges for the underlying resources only. So, with some context set, time to do the real work.

Related Articles on Elastic Beanstalk

Check out these articles below if you want to dig deeper into AWS Elastic Beanstalk.

Start with a Laravel App

Notice: This section is to set up a default Laravel app from scratch. You may already have a pre-existing app that you want to move to AWS beanstalk. If so, you can skip past this section or you can choose to begin with a demo Laravel app first to test-drive the process.

Step 1: Clone Our Demo Laravel Project

First things first, we need a project ready to get started. We have bootstrapped a Laravel project available on Github. The project is an example of a typical production-ready application. Feel free to clone or download the app via zip file to get started from the repository.

a github repository
GitHub Repository

Downloading from a repository doesn’t include packages and environment files, as these are not pushed along with source code. So, let’s take the first few steps to set the project up on the development server.  You can skip the next section if you have a development environment ready.

Step 2: Do a Composer Install

After you extract the zip file to your local storage, the next logical step is downloading all the packages/dependencies, which can be done easily through composer. You need to run the following command in the root project directory.

composer install

The composer will download all the dependencies of your project. Once that is done, the next focus would be the .env file which includes the necessary environment variables. For the time being, we copy the .env-example as.

Deploy PHP App to Elastic Beanstalk

Step 3: Generate Your App Key with artisan key: generate

There are a bunch of environment variables, and we will get back to this file after deployment. For now, let’s focus on the APP_KEY. We can generate an app key by running the following command.

php artisan key:generate

Now, if you switch to the .env file, you see the APP_KEY is set to a value. That’s it for the initial project setup. Next, we move to the good stuff, deploying to Elastic Beanstalk.

AWS Elastic Beanstalk Environment

Ok, now it’s time to move to the cloud. We have our Laravel app setup. It is time to begin setting up our AWS environment. This marks the beginning stage for many development teams needing to move their Laravel app to AWS Beanstalk.

Step 1: Setup Elastic Beanstalk from AWS Console

Elastic Beanstalk is awfully simple, and that’s the beauty of it. Let’s sequentially see the walkthrough.

  1. Login to your AWS Console and navigate to Elastic Beanstalk.
ebs dashboard
EBS Dashboard

2. Click Create Application and fill out the necessary information. Click for more info about the platform, branch & version.

Deploy PHP App to Elastic Beanstalk
Create Application EBS

3. Select all the project files and compress them in a zip – this is necessary and will be large.

4. Back to Elastic Beanstalk, click on Upload your code. Choose the zip file and upload it. You may need to upload it to an S3 bucket if it’s larger than 512MB. There’s a good chance that it is.

Note: Don’t worry. You won’t have to zip up your app every time you want to deploy a new version. You can accomplish CI/CD with Beanstalk. We will discuss further in deployment section below. This is just to get your Laravel app started.

upload application to ebs
Upload Application

5. Click on Create application  – If you want to look at configuration options, hold this for a whole unless we have a sneak peek.

Notes on Beanstalk PHP Versions – You Must Keep Up to Date

One of the biggest gotchas and issues when migrating legacy PHP apps to Elastic Beanstalk is the PHP version.

This is because many legacy PHP apps run on older versions of PHP due to the development cost and risk to upgrade. Beanstalk requires you to keep your app up to date with the latest versions. This means you must be using at least PHP 8 at the time of this writing.

Currently Available PHP Versions available on AWS Beanstalk

Currently, EBS has two options for PHP. You must be running at least PHP one of the latest versions of PHP before you migrate to Beanstalk. We have chosen PHP 8.1, but you must ensure your application specification.

  1. PHP 8.1 running on 64-bit Amazon Linux 2.
  2. PHP 8.0 running on 64-bit Amazon Linux 2.

Can you use PHP 5 or 7 in AWS Beanstalk? No. You must use PHP 8+ at the time of this writing.

What are your options if you can’t upgrade from PHP 5 or 7?

It is rare and you should upgrade 9 times out of 10… but there are those unique occasions where upgrading isn;t currently feasible. Maybe you don’t have the budget to do a massive refactor or the risk to the customer experience is too high before you introduce tests to this legacy app.

If this is the case, then you still have options to migrate your app to the cloud. Send a request for an App Migration Evaluation and we can discuss what makes the most sense for your situation.

Click here to submit a PHP App AWS Cloud Migration Evaluation Request

More Beanstalk Configuration Options

Although Elastic Beanstalk orchestrates the underlying resources, it allows you to customize the type of resources you want to use.  Click on the Configure more options.

Deploy PHP App to Elastic Beanstalk
EBS Configurations

There is a set of Configuration Presets, and each has a use case. Let’s see the breakdown.

PresetWhat it isUse Cases
Single Instance (Free Tier Eligible)EC2 resources with minimum resources available for 750 hours a month every month.For internal software tools, queue processing, or a test server. Not recommended for production-grade applications.
Single Instance (using Spot Instances)Buys EC2 instances like stocks. Available at 90% discounted rates than on-demand instances. Stateless and fault-tolerant systems. For instance – Image renderings, distributed databases, and Big Data software.
High AvailabilityIncludes a load balancer and runs multiple EC2 instances.A production-grade application with a high amount of traffic where downtime is not tolerable.
High Availability (using Spot and On-Demand instances)Includes a highly available on-demand and a spot EC2 instance for cost reduction.A production-grade application with low-to-moderate traffic.
EBS Configuration Presets

Besides these presets, EBS gives you the freedom to customize the default. This article will stick to the Single instance (Free Tier eligible) for demonstration purposes.

Feel free to play around with different settings as long as you know the incurring cost. When you’re ready to go with the customizations, move on and create the application. Application setup will take a while – so you can brew yourself a coffee in the meantime 🙂

creating EBS environment
Creating EBS environment

Once the service is up, it will redirect you to the dashboard, where you can see application health and logs.

Step 2: Setup your Laravel Database in Beanstalk with RDS

I am going to make the assumption that you are going to need a database with your Laravel app.

AWS Elastic Beanstalk uses Amazon Relational Database Service under the hood. Amazon RDS is a versatile service in terms of features, and at the moment of writing this article, it supports the following database engines.

  1. Amazon Aurora
  2. MySQL
  3. PostgreSQL
  4. MariaDB
  5. Oracle
  6. SQL Server

Besides, you can choose instance classes. Every instance comes with a unique set of specifications and pricing. Read more.

We will use db.t2.micro with 5 GB storage, and that too in only one availability zone. For production-grade applications, you will require a powerful instance, and that too in multiple availability zones.

So, Here’s the walkthrough.

  1. From the application dashboard, navigate to Configurations.
EBS Configurations
EBS Configurations

2. Scroll down to the bottom and edit the Database category.

Database Configurations
Database Configurations

3. The Database includes a lot of customization options; feel free to play around. We keep the defaults. We also fill out the username and password here.

Deploy PHP App to Elastic Beanstalk
Database Configurations

4. Scroll down and click Apply.

Elastic beanstalk will start updating your environment, which will take a while. 

Configure AWS Elastic Beanstalk for Laravel PHP App

Step 1: Inject Laravel Env Variables into AWS Beanstalk

Ok. We have the Beanstalk app environment created and the DB setup for our Laravel app. What is the next step?

Let’s add our environment variables.

Laravel uses .env files by default for environment setup. You can still use this approach by including a .env file in your deployment, but we will share a different approach using Beanstalk’s Environment Properties. Ideally, we would create the .env file during the CI/CD process which we will discuss in a future article.

Inject the env variables into the runtime so that you can avoid storing sensitive information inside of files.

  1. Navigate to Configuration > Software, and click Edit.
Deploy PHP App to Elastic Beanstalk
EBS Configuration

2. Scroll down to the Environment Properties and add environment variables as follows.

Server Environment
Server Environment

The server will be able to inject these values into a running instance of the application. Next, let’s see how the application uses these environment variables in the source code.

These are the same variables that you will find in your .ENV files that you use locally. You can copy them from the ENV file and add them to the Environment properties. Update the values as expected.

Note: Updating Laravel Config to Read from Server Env Variables

You may need to update your various config files to read from your server environment as Laravel defaults to read from a .ENV file.

This is a pretty simple update.

  1. Open up your various config files (app.php)
  2. Update each of the env functions to the following format to default to $_ENV when available.
    env(“ENV_OPTION”, isset($_ENV[‘ENV_OPTION’]) ? $_ENV[‘ENV_OPTION’] : ‘Hello World’);
  3. Repeat throughout your configs.

Note: Use AWS Param Store for More Secure Options than Beanstalk Environment Properties

You can use Beanstalk’s environment properties for environment configuration options, but it isn’t the recommended approach for your secrets. You should use AWS Param Store PHP library for your sensitive variables like passwords and keys.

Articles Related to Managing Secrets in AWS

Param Store integration is outside of the scope of this article but you can learn more about it:

Step 2: DB Env Variables

Ok, we’re coming up on the final steps of our configuration requirements. We need to update our DB-related Laravel env variables to match the RDS config from above. We should be able to get this done fairly quickly.

  1. Navigate to Configurations and then Database. Click on the endpoint to reach RDS.
Database Endpoint
Database Endpoint

2. That’s the RDS database list, and here we have our database.

Deploy PHP App to Elastic Beanstalk

3. Click on the database to see its info.

RDS Config
RDS Config

4. To get the name of the database, navigate to Configurations.

Deploy PHP App to Elastic Beanstalk
Database Name

And that’s how we get the info. Now, you can update the environment variable configs for the related DB content.

.env file
.env file

Step 3: Run Your Laravel Migrations in Beanstalk

The final step you want to do is run your DB migrations in Beanstalk. You should create a .ebextensions subdirectory at the root of the project. In this folder, you can create automated commands that will run on every new deployment. You can reference the following StackOverflow answer to set up this process.

Stackoverflow Laravel Artisan Migrate on Beanstalk (Reference second answer)

Update the Document Root to the Public Directory

  Deploy PHP App to Elastic Beanstalk -
EBS Application Dashboard

We can see an issue immediately, and that’s the health. EBS monitors the resources and uses 40+ metrics to determine if the application resources are able to perform as intended. Any issue will cause the application’s health to deteriorate. Learn more about health checks.

To resolve this issue, we will take the following steps.

  1. Navigate to Configuration in the left panel.

2. Edit the Software configuration.

EBS Software Configurations
EBS Software Configurations

3. In document root, type “/public,” the folder that is the starting point of your app.

Deploy PHP App to Elastic Beanstalk
Setting Up Document Root

4. Scroll to the bottom and click Apply.

Let’s revisit the dashboard and check what’s the health status.

Application Healthy Status
Application Healthy Status

Voila! The application is healthy

Up and Running | PHP App in AWS

To see if the server is working, click on the link that the figure highlights.

Deploy PHP App to Elastic Beanstalk
Application Link

Here’s how it looks on our end.

Laravel Application
Laravel Application

CI/CD | Upload Project from Github to Elastic Beanstalk

So far, we reupload the zip file whenever there is a change to the source code. There’s nothing wrong, but it is certainly not efficient, especially when you’re working on a real application. We need to create a CI/CD pipeline from Github to ensure we are continuously integrating and deploying.

Not sure what CI/CD is? CI/CD makes developers’ life a lot easier, and it has been a standard in the industry. Want to learn more about it? Stay tuned at FuelingPHP, as we will upload a complete guide on this topic.

And Voila! That’s pretty much it. Have you been able to deploy the PHP app to Elastic Beanstalk? If not, review the article and see what has been missed. 

That’s it. Let’s jump to a conclusion.

AWS Elastic Beanstalk CI/CD Articles

How to Deploy PHP App to Elastic Beanstalk

This article is about deploying a PHP application to AWS Elastic Beanstalk. The article includes a PHP Laravel application and presents a walkthrough of how to set up the application for production, deploy it to EBS, and set up a database. Every walkthrough is broken down into steps to make it easier to understand.

Hopefully, you’ve been able to migrate your PHP app to AWS Elastic Beanstalk. If so, cheers! Stay tuned at FuelingPHP for more interesting walkthroughs and guides.

Building Web Applications in AWS

This article is part of our series to make AWS easy. We love AWS, but let’s be honest. It isn’t effortless. It’s way too complicated. We’ve created this learning path to help level you up and onboard your PHP app quickly.

Did you find this article helpful?

If you need additional support, I offer a few a few ways to help.

I hope you're enjoying this article.

Here are a few ways that I can provide additional technical support.