How to Deploy a Laravel Application on AWS EC2

How to Deploy a Laravel Application on AWS EC2

A comprehensive guide to deploying your Laravel application on an AWS EC2 instance, covering server setup, database configuration, and deployment best practices.

Subash Rijal
Subash Rijal
Software Developer
February 15, 2025
4 min read
Share:

Table of Contents

Introduction

Deploying a Laravel application can be a complex task, especially when moving from a local development environment to a production server. AWS EC2 provides a flexible and scalable solution for hosting your applications. This guide will walk you through the process of deploying a Laravel application on an AWS EC2 instance.

Prerequisites

Before you begin, ensure you have the following:

  • An AWS account.
  • Basic knowledge of Linux commands.
  • Your Laravel application ready for deployment (e.g., pushed to a Git repository).

Step 1: Launch an EC2 Instance

  1. Log in to AWS Management Console: Navigate to the EC2 dashboard.
  2. Launch Instance: Click on “Launch Instance”.
  3. Choose an Amazon Machine Image (AMI): Select an Ubuntu Server AMI (e.g., Ubuntu Server 22.04 LTS).
  4. Choose an Instance Type: Select an appropriate instance type based on your application’s needs (e.g., t2.micro for testing).
  5. Configure Instance Details: Keep the defaults for most settings. Ensure “Auto-assign Public IP” is enabled.
  6. Add Storage: Default storage (8GB) is usually sufficient for a basic Laravel app.
  7. Add Tags: (Optional) Add tags like Name: LaravelAppServer.
  8. Configure Security Group: Create a new security group or select an existing one. Ensure the following ports are open:
    • SSH (Port 22): For connecting to your instance.
    • HTTP (Port 80): For web traffic.
    • HTTPS (Port 443): For secure web traffic (recommended).
  9. Review and Launch: Review your settings and click “Launch”.
  10. Create a new key pair: Download the .pem file. This key is crucial for SSH access.

Step 2: Connect to Your EC2 Instance

Open your terminal and use the following command to connect to your instance. Replace your-key.pem with your key pair file and your-instance-ip with your EC2 instance’s public IP address.

chmod 400 your-key.pem
ssh -i "your-key.pem" ubuntu@your-instance-ip

Step 3: Install Web Server (Nginx), PHP, and Composer

Once connected, update your package list and install necessary software.

sudo apt update
sudo apt upgrade -y

# Install Nginx
sudo apt install nginx -y

# Install PHP and extensions (adjust version if needed, e.g., php8.1-fpm)
sudo apt install php8.1-fpm php8.1-mysql php8.1-mbstring php8.1-xml php8.1-bcmath php8.1-curl php8.1-zip php8.1-gd -y

# Install Composer
curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer

Step 4: Install MySQL Server

sudo apt install mysql-server -y
sudo mysql_secure_installation

Follow the prompts to secure your MySQL installation. Remember the root password.

Step 5: Deploy Your Laravel Application

  1. Clone your repository: Navigate to /var/www/ and clone your Laravel project.
sudo mkdir -p /var/www/your-laravel-app
cd /var/www/your-laravel-app
sudo git clone https://github.com/your-username/your-laravel-app.git .
  1. Set permissions: Ensure Nginx and PHP-FPM have proper permissions.
sudo chown -R www-data:www-data /var/www/your-laravel-app
sudo chmod -R 775 /var/www/your-laravel-app/storage
sudo chmod -R 775 /var/www/your-laravel-app/bootstrap/cache
  1. Install Composer dependencies: From your project root (/var/www/your-laravel-app).
composer install --no-dev --optimize-autoloader
  1. Configure .env file: Copy .env.example to .env and update your database credentials.
sudo cp .env.example .env
sudo nano .env

Update DB_DATABASE, DB_USERNAME, DB_PASSWORD.

  1. Generate Application Key:
php artisan key:generate
  1. Run Migrations and Seeders:
php artisan migrate --seed

Step 6: Configure Nginx

Create a new Nginx configuration file for your Laravel application.

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

Add the following content:

server {
    listen 80;
    server_name your-instance-ip your-domain.com;
    root /var/www/your-laravel-app/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    charset utf-8;

    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 $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}

Enable the site and restart Nginx:

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

Step 7: Configure Database

Connect to MySQL and create a database and user for your Laravel application.

sudo mysql -u root -p
CREATE DATABASE your_database_name CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'your_username'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON your_database_name.* TO 'your_username'@'localhost';
FLUSH PRIVILEGES;
EXIT;

Remember to update your .env file with these new credentials.

Conclusion

You have successfully deployed your Laravel application on an AWS EC2 instance. This guide covered the essential steps from launching an instance to configuring your web server and database. For production environments, consider adding SSL, a domain name, and more robust security measures.

Related Posts

Continue your learning journey with these handpicked articles

Fork Yeah! Asynchronous PHP Like a Pro
Tutorials

Fork Yeah! Asynchronous PHP Like a Pro

Master asynchronous programming in PHP with process forking - a comprehensive guide to building concurrent applications

5 min read
Read More →
How I Built an AI Assistant That Writes Weekly Marketing Emails from Sales Data
Tutorials

How I Built an AI Assistant That Writes Weekly Marketing Emails from Sales Data

Learn how to automate your marketing emails using Python, OpenAI GPT, and Google Sheets API to transform sales data into engaging content.

4 min read
Read More →
How to Deploy a Laravel Application
Tutorials

How to Deploy a Laravel Application

Step-by-step guide to deploying Laravel applications to production

1 min read
Read More →