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:
yourdomain.com
) to AWS so your site is live
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:
If you haven't already signed up for AWS, you need to create an AWS account:
An EC2 instance is essentially a virtual server in AWS that you can use to host your Laravel application.
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
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.
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
There are multiple ways to upload your Laravel application to your EC2 instance:
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
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.
If you cloned or uploaded the files, make sure they are in the web directory (/var/www/html/ or a sub-directory).
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
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
Now that your application is running on your EC2 instance, you need to point your domain (like laravel-aws.com) to the server.
@
(or leave it blank, depending on the registrar)A
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.
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:
In this tutorial, I will guide you through the process of installing the PHP DOM Extension in Ubuntu 23.04. The PHP DOM...
Jan-29-2024
In this article, we will see laravel 9 many to many polymorphic relationship. many to many polymorphic relationship more...
Apr-06-2022
Today, I will show you Laravel 8 Toastr Notifications Example. There are many types of notifications availa...
Oct-19-2020
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...
Jan-31-2022