Deploying a Laravel App to EC2: A Comprehensive Step-by-Step Guide

Faraz Irfan
3 min readMay 11, 2023

--

Photo by Mehmet Ali Peker on Unsplash

Laravel is one of the most popular PHP frameworks for building web applications. It provides developers with a rich set of tools and features that make development fast and easy. AWS EC2 (Elastic Compute Cloud) is a widely used cloud computing service that allows users to create and manage virtual machines in the cloud. In this tutorial, we will show you how to deploy a Laravel app to an EC2 instance on AWS.

Prerequisites

To follow this tutorial, you will need the following:

  • An AWS account
  • An EC2 instance running Amazon Linux 2 or another compatible Linux distribution
  • A basic understanding of the Linux command line
  • A Laravel app that is ready for deployment

Step 1: Create an EC2 instance

The first step is to create an EC2 instance on AWS. Go to the AWS Management Console and select EC2 from the list of services. Click on “Launch Instance” and choose an appropriate AMI (Amazon Machine Image) based on your requirements.

Step 2: Install the necessary software

Once your EC2 instance is up and running, SSH into your instance and update your system packages:

sudo yum update

Next, install the necessary software packages:

sudo yum install -y httpd php php-mysqlnd php-gd php-xml php-mbstring php-json php-zip mariadb-server git

Step 3: Clone your Laravel project

Next, clone your Laravel project into the EC2 instance. Assuming you have Git installed on your EC2 instance, run the following command to clone your Laravel project:

git clone https://github.com/your_username/your_project.git /var/www/html

This will clone your Laravel project into the /var/www/html directory.

Step 4: Set up your Laravel project

Once your Laravel project is cloned, you need to set it up by creating the .env file, installing the dependencies using Composer, and configuring your database.

First, navigate to your Laravel project directory:

cd /var/www/html

Next, copy the .env.example file to .env:

cp .env.example .env

Edit the .env file and set your database credentials:

DB_DATABASE=your_database_name
DB_USERNAME=your_database_user
DB_PASSWORD=your_database_password

Save and close the file.

Next, run the following command to install the dependencies using Composer:

composer install --no-dev --optimize-autoloader

This will install the required dependencies for your Laravel project.

Finally, run the following command to generate a key for your Laravel app:

php artisan key:generate

This will generate a unique key for your Laravel app that is used for encryption.

Step 5: Configure your web server

Now that your Laravel project is set up, you need to configure your web server to serve the Laravel application. There are several web servers you can choose from, such as Apache or Nginx.

Configure Apache

If you’re using Apache, you need to create an .htaccess file in the /var/www/html/public directory. Run the following command:

sudo nano /var/www/html/public/.htaccess

Paste the following configuration:

RewriteEngine On
RewriteRule ^(.*)$ index.php/$1 [L]

Save and close the file.

Next, start Apache and enable it to start on boot:

sudo systemctl start httpd
sudo systemctl enable httpd

Step 6: Configure your database

Now that your web server is configured, you need to create a MySQL database for your Laravel app. Run the following command to start the MySQL prompt:

sudo mysql

Create a new database:

CREATE DATABASE your_database_name;

Create a new user and set a password:

CREATE USER 'your_database_user'@'localhost' IDENTIFIED BY 'your_database_password';

Grant all the privileges to the new user:

GRANT ALL PRIVILEGES ON your_database_name.* TO 'your_database_user'@'localhost';
FLUSH PRIVILEGES;

Exit the MySQL prompt by typing:

exit

Step 7: Test your Laravel app

Your Laravel app should now be up and running on your EC2 instance. To test it, open a web browser and enter your instance’s public IP address or DNS name. You should see your Laravel app homepage.

Conclusion

In this tutorial, you learned how to deploy a Laravel app to an EC2 instance on AWS. You created an EC2 instance, installed the necessary software packages, cloned your Laravel project, set it up, and configured your web server and database. Finally, you tested your Laravel app to ensure it was working properly. By following these steps, you can easily deploy your Laravel app to an EC2 instance on AWS.

--

--

Faraz Irfan
Faraz Irfan

Written by Faraz Irfan

Product-centric software engineer, Founder

No responses yet