How to Deploy a Laravel Application on AWS

Websolutionstuff | Dec-17-2024 | Categories : Laravel

In this guide, I'll show you how to deploy your Laravel application on AWS (Amazon Web Services) in a simple and easy way. Whether you’re a beginner or just new to deploying Laravel apps on the cloud, you’ll find this process straightforward.

AWS is a powerful platform that offers many cloud services, and hosting your Laravel app on it can provide great scalability, security, and reliability.

By the end of this guide, you’ll know how to:

  1. Set up an EC2 instance (virtual server) on AWS.
  2. Install the necessary software like Apache, PHP, and MySQL.
  3. Upload your Laravel application to the server.
  4. Point your domain (like yourdomain.com) to AWS so your site is live

How to Deploy a Laravel Application on AWS

 

To deploy your Laravel application on AWS and associate it with your domain (like laravel-aws.com), you need to follow a series of steps. Here's a beginner-friendly, in-depth guide on how to host your Laravel application on AWS:

Step 1: Sign Up for AWS

If you haven't already signed up for AWS, you need to create an AWS account:

  1. Visit AWS and sign up.
  2. Follow the instructions to verify your email and phone number.
  3. After signing up, you’ll have access to the AWS Management Console to manage all your AWS services.

 

Step 2: Set Up an EC2 Instance (Server)

An EC2 instance is essentially a virtual server in AWS that you can use to host your Laravel application.

1. Launch an EC2 Instance:

  • Go to the AWS Management Console.
  • In the search bar, type EC2 and click on EC2 to open the EC2 dashboard.
  • Click on Launch Instance to start the setup for your new server.
  • Choose an Amazon Machine Image (AMI). For example, you can use Ubuntu as it is widely used for hosting Laravel apps.
  • Select an Instance Type. For small to medium applications, choose something like t2.micro (eligible for the free tier).
  • Configure the Instance settings. Leave most options as default, but ensure that the instance has a public IP.
  • Under Configure Security Group, create a new security group that allows:
    • SSH (port 22) for connecting to the instance via the terminal.
    • HTTP (port 80) for web access.
    • HTTPS (port 443) for secure web access (optional).
  • Click Review and Launch and follow the prompts to launch the instance.
  • Download the PEM Key Pair to access your EC2 instance via SSH. This key is used to securely connect to your server.

2. Connect to Your EC2 Instance:

  • Once the instance is launched, go to the Instances section in EC2 and find your new instance.

  • Copy the Public IP of the instance.

  • Open your terminal (or use an SSH client like PuTTY if on Windows), and run the following command to connect to the instance (replace your-key.pem and your-ec2-ip with your actual PEM file and EC2 public IP):

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

 

Step 3: Set Up the Server (Install LAMP Stack)

Now that you're connected to the EC2 instance, you need to set up the LAMP stack (Linux, Apache, MySQL, PHP) to run your Laravel application.

1. Update the Instance:

sudo apt update
sudo apt upgrade

2. Install Apache

sudo apt install apache2

Make sure Apache is running by visiting the public IP of your instance in a browser. You should see the Apache2 default page.

3. Install PHP and Extensions:

Laravel requires PHP and certain PHP extensions. Install the required PHP version and extensions:

sudo apt install php php-cli php-fpm php-mysql php-xml php-mbstring php-curl php-zip unzip

4. Install MySQL:

If you want to use MySQL for your Laravel app, install MySQL as well.

sudo apt install mysql-server
sudo mysql_secure_installation

Follow the prompts to set up the MySQL root password.

5. Install Composer (PHP Dependency Manager for Laravel):

Composer is essential for managing Laravel dependencies. Install it with:

sudo apt install curl php-cli php-mbstring git unzip
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

 

Step 4: Upload Your Laravel Application to EC2

There are multiple ways to upload your Laravel application to your EC2 instance:

1. Via Git (if using Git):

If your Laravel application is in a Git repository, you can clone it directly onto the server.

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

2. Via SFTP (File Transfer):

If you have your Laravel app on your local machine, you can use SFTP (Secure File Transfer Protocol) to upload it to the EC2 instance. For example, use a program like FileZilla or the command line to upload your files to /var/www/html.

3. Move Laravel Files to Web Directory:

If you cloned or uploaded the files, make sure they are in the web directory (/var/www/html/ or a sub-directory).

 

Step 5: Configure Apache for Laravel

Now, configure Apache to serve your Laravel application:

Create an Apache Virtual Host:

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

Add the following configuration (adjust paths based on your setup):

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/your-laravel-app/public
    ServerName laravel-aws.com
    <Directory /var/www/html/your-laravel-app/public>
        AllowOverride All
        Require all granted
    </Directory>
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Enable the Virtual Host and Rewrite Module:

sudo a2ensite laravel.conf
sudo a2enmod rewrite

Restart Apache:

sudo systemctl restart apache2

 

Step 6: Set Up the Database and Environment Variables

Create MySQL Database:

mysql -u root -p
CREATE DATABASE your_database_name;
EXIT;

Update Laravel env File: In your Laravel app's root directory, find the env file and update the database configuration to match your MySQL settings:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=root
DB_PASSWORD=your_password

Run Laravel Migrations:

cd /var/www/html/your-laravel-app
php artisan migrate

 

Step 7: Point Your Domain to AWS EC2

Now that your application is running on your EC2 instance, you need to point your domain (like laravel-aws.com) to the server.

1. Find Your EC2 Instance’s Public IP:

  • In the EC2 dashboard, find your instance and copy its public IP address.

2. Update DNS Settings for Your Domain:

  • Go to the domain registrar (where you bought laravel-aws.com) and log in to the account.
  • Find the DNS settings for your domain.
  • Add an A Record that points to the public IP address of your EC2 instance.
    • Name: @ (or leave it blank, depending on the registrar)
    • Type: A
    • Value: The public IP address of your EC2 instance
    • TTL: Leave as default or set it to 3600 seconds.
  • Save the changes.

It may take up to 48 hours for the DNS changes to propagate, but you should eventually be able to access your Laravel app by visiting laravel-aws.com.

 

Step 8: Secure Your Application with HTTPS

To secure your site with HTTPS, you can use Let’s Encrypt to get a free SSL certificate:

Install Certbot:

sudo apt install certbot python3-certbot-apache

Obtain the SSL Certificate:

sudo certbot --apache -d laravel-aws.com

Follow the prompts to set up the certificate.

Renew the Certificate Automatically:

sudo certbot renew --dry-run

 


You might also like:

Recommended Post
Featured Post
How to Install PHP DOM Extension in Ubuntu 23.04
How to Install PHP DOM Extensi...

In this tutorial, I will guide you through the process of installing the PHP DOM Extension in Ubuntu 23.04. The PHP DOM...

Read More

Jan-29-2024

Laravel 9 Many To Many Polymorphic Relationship
Laravel 9 Many To Many Polymor...

In this article, we will see laravel 9 many to many polymorphic relationship. many to many polymorphic relationship more...

Read More

Apr-06-2022

Laravel 8 Toastr Notifications Example
Laravel 8 Toastr Notifications...

Today, I will show you Laravel 8 Toastr Notifications Example. There are many types of notifications availa...

Read More

Oct-19-2020

How To Get Last 7 Days Record In Laravel 8
How To Get Last 7 Days Record...

In this example, we will see how to get the last 7 days record in laravel 8. You can simply get the last 7 days record u...

Read More

Jan-31-2022