Step-by-Step Guide to Deploying a Laravel App on AWS EC2

AWS
EmpowerCodes
Oct 30, 2025

Deploying a Laravel application on Amazon Web Services (AWS) gives you a scalable, secure, and high-performance environment for hosting production-grade applications. AWS EC2 provides full control over the server, enabling developers to configure and optimize the stack based on their needs. Whether you are migrating from shared hosting or deploying your first cloud application, this step-by-step guide will walk you through the full deployment process using an EC2 instance.

Prerequisites

Before you begin, ensure the following:

  • A working Laravel application

  • An AWS account

  • Basic understanding of Linux commands

  • Domain name (optional but recommended)

  • Composer and Git knowledge

Step 1: Launch Your EC2 Instance

  1. Log into the AWS Management Console

  2. Navigate to EC2 Dashboard and click Launch Instance

  3. Choose an Amazon Machine Image (AMI)
    Recommended: Ubuntu Server 22.04 LTS

  4. Select Instance Type
    Suggested: t3.micro (Free Tier eligible)

  5. Create a Key Pair to securely access your instance

  6. Configure Security Group
    Allow inbound:

    • SSH (22)

    • HTTP (80)

    • HTTPS (443)

  7. Launch the instance

Once launched, copy the Public IP address of your instance.

Step 2: Connect to the EC2 Server via SSH

Use the key pair downloaded earlier to connect:

chmod 400 yourkey.pem ssh -i yourkey.pem ubuntu@your-ec2-public-ip

You now have remote access to the EC2 server.

Step 3: Install Required Packages and LAMP Stack

Update packages:

sudo apt update && sudo apt upgrade -y

Install Apache:

sudo apt install apache2 -y sudo systemctl enable apache2

Install PHP and extensions required by Laravel:

sudo apt install php php-mbstring php-xml php-bcmath php-common php-json php-zip php-fpm php-mysql php-curl php-gd -y

Install Composer:

sudo apt install composer -y

Install MySQL Client (if needed):

sudo apt install mysql-client -y

Step 4: Clone Your Laravel Application

Install Git:

sudo apt install git -y

Navigate to Apache directory and clone your repo:

cd /var/www/ sudo git clone your-repository-url project-name cd project-name

Step 5: Set Up Environment Variables

Duplicate the .env file and update it:

cp .env.example .env

Generate application key:

php artisan key:generate

Update .env with database and app details.

Step 6: Install Laravel Dependencies

Run Composer install:

composer install

Give storage and bootstrap folder permissions:

sudo chown -R www-data:www-data /var/www/project-name sudo chmod -R 775 storage bootstrap/cache

Step 7: Configure Apache Virtual Host

Create a new config file:

sudo nano /etc/apache2/sites-available/laravel.conf

Add the following:

<VirtualHost *:80> ServerName your-domain.com ServerAdmin admin@your-domain.com DocumentRoot /var/www/project-name/public <Directory /var/www/project-name> AllowOverride All Require all granted </Directory> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>

Enable site and rewrite module:

sudo a2ensite laravel.conf sudo a2enmod rewrite sudo systemctl restart apache2

Step 8: Set Up Database (Optional)

If using Amazon RDS or external DB, configure connection inside .env.

If using local MySQL on EC2, install:

sudo apt install mysql-server -y sudo mysql_secure_installation

Create DB and user:

CREATE DATABASE laravel_db; CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY 'password'; GRANT ALL PRIVILEGES ON laravel_db.* TO 'laravel_user'@'localhost'; FLUSH PRIVILEGES;

Update .env and run migrations:

php artisan migrate

Step 9: Configure Domain and SSL

Point your domain’s A record to EC2 Public IP.

To secure with HTTPS, install Certbot:

sudo apt install certbot python3-certbot-apache -y sudo certbot --apache

Follow on-screen steps to install SSL certificate.

Step 10: Set Up Supervisor for Queues (Optional but Recommended)

sudo apt install supervisor -y sudo nano /etc/supervisor/conf.d/laravel-worker.conf

Add:

[program:laravel-worker] process_name=%(program_name)s_%(process_num)02d command=php /var/www/project-name/artisan queue:work autostart=true autorestart=true numprocs=1 redirect_stderr=true stdout_logfile=/var/www/project-name/storage/logs/worker.log

Enable service:

sudo supervisorctl reread sudo supervisorctl update sudo supervisorctl start laravel-worker:*

Conclusion

Deploying a Laravel application on AWS EC2 gives you full control over configuration, scalability, deployment workflows, and performance. By following this guide, you can set up a production-ready environment to run Laravel efficiently, with support for domain routing, SSL, and automated job queues. Once deployed, you can enhance your architecture using RDS, Load Balancers, CloudWatch, and auto-scaling for a highly available cloud environment.