How to Deploy a Laravel Project on AWS EC2: A Step-by-Step Guide

Deploying a Laravel project on AWS EC2 provides scalability, reliability, and security for your application. In this guide, we’ll walk you through the process of deploying a Laravel project on an AWS EC2 instance. Let’s dive in!
Prerequisites
Before starting, ensure you have the following:
- An active AWS account.
- A Laravel project ready for deployment.
- Basic knowledge of AWS EC2, SSH, and Laravel.
Step 1: Launch an AWS EC2 Instance
- Log in to the AWS Console: Navigate to the EC2 service.
- Create a New Instance:
- Click on “Launch Instance.”
- Choose an Amazon Machine Image (AMI), such as Ubuntu 22.04.
- Select an instance type (e.g., t2.micro for free tier).
- Configure the instance details and add storage.
- Security Group Settings: Allow the following ports:
- SSH (Port 22)
- HTTP (Port 80)
- HTTPS (Port 443)
- Launch the Instance: Generate and download a new key pair (.pem file). You’ll need it for SSH access.
Step 2: Connect to the Instance via SSH
- Open a terminal on your local machine.
- Navigate to the directory containing your
.pem
file. - Run the following command to connect:
ssh -i "path/to/your-key.pem" ubuntu@your-ec2-public-ip
Step 3: Update and Install Required Packages
Run these commands on your EC2 instance:
sudo apt update && sudo apt upgrade -y
sudo apt install apache2 php php-cli php-mbstring php-xml php-bcmath unzip curl git -y
Step 4: Install Composer
Composer is essential for Laravel projects. Install it using:
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
Step 5: Set Up the Laravel Project
- Upload Your Laravel Project:
- Use an FTP client like FileZilla or SCP to upload your Laravel project to
/var/www/html
on your EC2 instance.
- Alternatively, clone the project using Git:
cd /var/www/html
git clone your-repository-url
Set Permissions:
sudo chown -R www-data:www-data /var/www/html
sudo chmod -R 775 /var/www/html/storage /var/www/html/bootstrap/cache
Install Dependencies:
cd /var/www/html
composer install
Environment Setup:
- Rename
.env.example
to.env
:
cp .env.example .env
Generate the application key:
php artisan key:generate
Update database credentials in the
env
file.
Step 6: Configure Apache
Enable the Rewrite Module:
sudo a2enmod rewrite
Create a Virtual Host Configuration:
sudo nano /etc/apache2/sites-available/laravel.conf
Add the following content:
<VirtualHost *:80>
ServerAdmin admin@example.com
DocumentRoot /var/www/html/public
ServerName your-domain.com
<Directory /var/www/html>
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Enable the Site Configuration:
sudo a2ensite laravel.conf
sudo systemctl reload apache2
Step 7: Set Up the Database
Install MySQL:
sudo apt install mysql-server -y
Secure MySQL:
sudo mysql_secure_installation
Create a Database and User:
sudo mysql
CREATE DATABASE laravel_db;
CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY 'strong_password';
GRANT ALL PRIVILEGES ON laravel_db.* TO 'laravel_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Update the .env
file with the database details.
Step 8: Test the Deployment
- Access your application via the public IP or domain name of your EC2 instance.
- If everything is configured correctly, you should see your Laravel application.
Optional: Set Up HTTPS
To secure your application, use Let’s Encrypt to configure SSL:
sudo apt install certbot python3-certbot-apache -y
sudo certbot --apache
Follow the prompts to secure your domain.
Deploying a Laravel project on AWS EC2 might seem daunting, but following these steps makes the process straightforward. AWS EC2 provides a robust platform for hosting scalable Laravel applications. Keep your system updated, monitor performance, and enjoy the flexibility of the cloud.
For more tutorials, stay tuned!