Milind Daraniya

Deploying Laravel Applications on AWS: Step-by-Step Guide

Published February 12th, 2023 31 min read

Deploying a Laravel application on Amazon Web Services (AWS) can be a powerful way to host your web application in a scalable and reliable cloud environment. AWS offers a range of services that complement Laravel applications, providing high availability and seamless scalability. In this step-by-step guide, we'll walk you through the process of deploying a Laravel application on AWS.

Step 1: Set Up AWS Account

If you don't have an AWS account, sign up for one at https://aws.amazon.com. Once you have an account, log in to the AWS Management Console.

Step 2: Launch an EC2 Instance

  1. Navigate to the EC2 Dashboard in the AWS Management Console.
  2. Click on the "Launch Instance" button.
  3. Choose an Amazon Machine Image (AMI) with your preferred operating system. Select a suitable instance type based on your application's requirements.
  4. Configure the instance details, including network settings and security groups. Make sure to open the necessary ports for your Laravel application (e.g., HTTP port 80).
  5. Add storage if needed and configure any additional settings.
  6. Review your configuration and launch the instance.

Step 3: Connect to the Instance

  1. After the instance is launched, you'll receive a private key (PEM file). Save this file securely.
  2. Use SSH to connect to your instance using the private key:
ssh -i /path/to/your/private-key.pem ec2-user@your-instance-public-ip

Step 4: Install Dependencies

Once connected to the instance, install the necessary dependencies for your Laravel application:

sudo yum update -y
sudo yum install git -y
sudo amazon-linux-extras install epel -y
sudo yum install httpd php php-mysqlnd php-mbstring php-xml -y
sudo systemctl start httpd
sudo systemctl enable httpd

Step 5: Install Composer

Install Composer, a PHP package manager required for Laravel:

sudo php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
sudo php -r "unlink('composer-setup.php');"

Step 6: Deploy Laravel Application

Clone your Laravel application from your version control system (e.g., GitHub) to the server:

git clone https://github.com/your-github-username/your-laravel-repo.git /var/www/html

Install Laravel dependencies and generate the application key:

cd /var/www/html
composer install --no-dev
cp .env.example .env
php artisan key:generate

Step 7: Configure Apache

  1. Configure Apache to serve your Laravel application:
sudo nano /etc/httpd/conf.d/laravel.conf

Add the following configuration (replace your-domain.com with your domain or server's public IP):

<VirtualHost *:80>
    ServerName your-domain.com
    DocumentRoot /var/www/html/public

    <Directory /var/www/html>
        AllowOverride All
    </Directory>
</VirtualHost>

Restart Apache to apply the changes:

sudo systemctl restart httpd

Step 8: Configure Security Groups

Ensure that your AWS security groups allow incoming traffic on ports 80 (HTTP) and 443 (HTTPS) to access your Laravel application.

Step 9: Set Up Database

If your Laravel application requires a database, set up the database on AWS RDS or any compatible database service.

Step 10: Point Domain

If you have a domain, point it to your server's public IP or create a DNS record pointing to your AWS Elastic IP.

Step 11: SSL Certificate (Optional)

For HTTPS support, obtain an SSL certificate using AWS Certificate Manager (ACM) or any other SSL provider. Configure Apache to use the SSL certificate.

Step 12: Final Checks

Test your Laravel application by accessing your domain or server's public IP in a web browser. Ensure everything is functioning as expected.