How to Deploy a Laravel 12 Project on AWS EC2

When I first started working with Laravel, deploying my project to a live server felt like a daunting task. But after experimenting with AWS EC2, I realized it’s not as complicated as it seems. AWS EC2 is a powerful and scalable cloud platform that’s perfect for hosting Laravel applications.

In this guide, I’ll share my experience and walk you through the process of deploying a Laravel 12 project on AWS EC2 in simple, beginner-friendly steps. Whether you’re new to AWS or Laravel, this tutorial will help you get your app live on the cloud with ease.

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

Prerequisites

Before we dive in, make sure you have:

Let’s get started!

Step-by-Step Guide to Deploy Laravel 12 on AWS EC2

Step 1: Set Up an AWS EC2 Instance

First, I log in to my AWS Management Console and navigate to the EC2 service. Here’s how I set up my instance:

  1. Launch an Instance: Click “Launch Instance” and give your instance a name, like “Laravel12App.”
  2. Choose an AMI: Select “Ubuntu Server 22.04 LTS” as it’s reliable and widely used for Laravel.
  3. Select Instance Type: For testing, I pick t2.micro (eligible for AWS Free Tier).
  4. Create a Key Pair: Generate a .pem file for SSH access and store it securely.
  5. Configure Security Group: Allow inbound traffic on ports 22 (SSH), 80 (HTTP), and 443 (HTTPS).
  6. Launch: Review your settings and launch the instance. Note down the public IP address.

Step 2: Connect to Your EC2 Instance

Once the instance is running, I connect to it using SSH:

  1. Open a terminal (or PuTTY on Windows).
  2. Run the command: ssh -i your-key.pem ubuntu@your-ec2-public-ip.
  3. If prompted, type “yes” to accept the connection.

Now, I’m inside my EC2 instance and ready to set up the server.

Step 3: Update the Server and Install Dependencies

To ensure the server is up-to-date and has the necessary software, I run these commands:

sudo apt update && sudo apt upgrade -y

Next, I install PHP 8.1 (required for Laravel 12) and other dependencies:

sudo add-apt-repository ppa:ondrej/php -y
sudo apt update
sudo apt install -y php8.1 php8.1-cli php8.1-fpm php8.1-mysql php8.1-xml php8.1-mbstring php8.1-curl php8.1-zip php8.1-gd nginx git unzip

I also install Composer, Laravel’s dependency manager:

curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

Step 4: Install and Configure MySQL

Most Laravel apps use a database, so I set up MySQL:

sudo apt install -y mysql-server
sudo systemctl start mysql
sudo systemctl enable mysql

Then, I secure MySQL and create a database:

sudo mysql_secure_installation
sudo mysql
CREATE DATABASE laravel_db;
CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY 'your_secure_password';
GRANT ALL PRIVILEGES ON laravel_db.* TO 'laravel_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Step 5: Deploy Your Laravel Project

Now, I move my Laravel project to the server. I prefer using Git for this:

  1. Install Git (if not already installed):
    sudo apt install git -y
    
  2. Clone Your Project:
    cd /var/www
    sudo git clone https://github.com/your_username/your_project.git laravel
    
  3. Set Permissions:
    sudo chown -R www-data:www-data /var/www/laravel
    sudo chmod -R 775 /var/www/laravel/storage /var/www/laravel/bootstrap/cache
    
  4. Install Dependencies:
    cd /var/www/laravel
    composer install --optimize-autoloader --no-dev
    

Step 6: Configure the .env File

I copy the .env.example file and update it with my database credentials:

cp .env.example .env
nano .env

In the .env file, I update:

APP_URL=http://your-ec2-public-ip
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_db
DB_USERNAME=laravel_user
DB_PASSWORD=your_secure_password

Then, I generate an application key:

php artisan key:generate

Step 7: Configure Nginx

I use Nginx as my web server. I create a new configuration file:

sudo nano /etc/nginx/sites-available/laravel

I add the following configuration:

server {
    listen 80;
    server_name your-ec2-public-ip;
    root /var/www/laravel/public;
    index index.php index.html;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\. {
        deny all;
    }
}

I enable the configuration and restart Nginx:

sudo ln -s /etc/nginx/sites-available/laravel /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx

Step 8: Run Migrations and Optimize

To set up the database schema, I run:

php artisan migrate

For better performance, I cache configurations:

php artisan config:cache
php artisan route:cache
php artisan view:cache

Step 9: Test Your Application

I open my browser and enter my EC2 instance’s public IP. If everything is set up correctly, my Laravel 12 app should load. If face issues, check the logs in /var/www/laravel/storage/logs.

Conclusion

Deploying my Laravel 12 project on AWS EC2 was a rewarding experience. By following these steps, I was able to set up a scalable and reliable server for my application. AWS EC2 offers flexibility and power, making it a great choice for hosting Laravel apps. With this guide, I hope you feel confident to deploy your own project and take it live.

Frequently Asked Questions (FAQs)

Q: Do I need an AWS account to deploy Laravel 12 on EC2?
A: Yes, you need an AWS account. The free tier is sufficient for testing and small projects.

Q: Can I use Apache instead of Nginx?
A: Absolutely! Install Apache instead of Nginx and configure it to point to your Laravel project’s public directory.

Q: Why is my Laravel app not loading after deployment?
A: Check your Nginx configuration, file permissions, and .env settings. Also, review the logs in storage/logs for errors.

Q: How can I secure my Laravel app on EC2?
A: Enable HTTPS by adding an SSL certificate (e.g., using Let’s Encrypt), restrict SSH access, and keep your server updated.

Q: Can I scale my Laravel app on EC2?
A: Yes, use AWS Auto Scaling and a Load Balancer to handle increased traffic and ensure high availability.


You might also like :

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS