How to Deploy a Laravel Project on AWS: A Step-by-Step Guide

Deploying a Laravel project on AWS: A Step-by-Step Guide might sound intimidating, but it’s much easier than you think! AWS provides scalable, reliable, and highly customizable hosting solutions, making it a fantastic choice for hosting your Laravel application. Whether you’re new to AWS or have some experience, this guide will walk you through the entire process in a simple and friendly way.
Let’s dive in and get your Laravel project live on AWS! 🚀
Why Deploy Laravel on AWS?
AWS is a powerhouse in cloud hosting. It provides:
- Scalability: Automatically adjust resources based on traffic.
- Security: Industry-leading measures to keep your app secure.
- Flexibility: Choose the configurations that best suit your project.
Step-by-Step Guide to Deploy Laravel on AWS
Step 1: Set Up an AWS Account
- Head over to AWS and create an account if you don’t already have one.
- Once logged in, navigate to the AWS Management Console.
Pro Tip: AWS offers a free tier for beginners, which is perfect for testing your deployment without incurring costs.
Step 2: Launch an EC2 Instance
- In the AWS Management Console, search for EC2 and click on “Instances.”
- Click Launch Instances and follow these steps:
- Choose an Amazon Machine Image (AMI): Select an Ubuntu or Amazon Linux AMI.
- Select an instance type: A t2.micro is ideal for small projects.
- Configure your security group: Allow SSH (port 22), HTTP (port 80), and HTTPS (port 443).
Pro Tip: Download your private key file (e.g., .pem
) during this process. You’ll need it to access your instance.
Step 3: Connect to Your EC2 Instance
Use SSH to connect to your EC2 instance.
ssh -i your-key-file.pem ubuntu@your-ec2-instance-ip
Once connected, update your server:
sudo apt update && sudo apt upgrade -y
Step 4: Install Necessary Packages
To host a Laravel project, you need a LAMP/LEMP stack. Here’s how to set it up:
Install Apache (or Nginx) and PHP
- Install Apache:
sudo apt install apache2 -y
2. Install PHP and required extensions:
sudo apt install php libapache2-mod-php php-mysql php-cli php-curl php-mbstring php-xml php-bcmath php-zip -y
Install MySQL
- Install MySQL Server:
sudo apt install mysql-server -y
2. Secure MySQL:
sudo mysql_secure_installation
Step 5: Upload Your Laravel Project
- Install Git if it’s not already installed:
sudo apt install git -y
2. Clone your Laravel project from your repository:
git clone your-repo-url /var/www/your-project
3. Navigate to your project directory:
cd /var/www/your-project
4. Install Composer dependencies:
composer install
5. Set file permissions:
sudo chown -R www-data:www-data /var/www/your-project
sudo chmod -R 775 /var/www/your-project/storage
sudo chmod -R 775 /var/www/your-project/bootstrap/cache
6. Set up the .env
file:
cp .env.example .env
php artisan key:generate
Update the .env
file with your database and app configurations.
Step 6: Configure Apache for Laravel
- Create a new Apache configuration file:
sudo nano /etc/apache2/sites-available/your-project.conf
2. Add the following configuration:
<VirtualHost *:80>
ServerName your-domain.com
DocumentRoot /var/www/your-project/public
<Directory /var/www/your-project>
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
3. Enable the site and rewrite module:
sudo a2ensite your-project
sudo a2enmod rewrite
sudo systemctl restart apache2
Step 7: Set Up Your Database
- Log in to MySQL:
sudo mysql -u root -p
2. Create a new database and user:
CREATE DATABASE your_database;
CREATE USER 'your_user'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON your_database.* TO 'your_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
3. Update your .env
file with the database credentials.
Step 8: Test Your Laravel Application
- Run migrations:
php artisan migrate
2. Visit your domain or public IP address in the browser. Your Laravel app should now be live! 🎉
Final Thoughts
Deploying a Laravel project on AWS is a valuable skill that can boost your professional portfolio. With AWS’s robust infrastructure and Laravel’s flexibility, you can create a powerful application ready to scale.
If you’re looking for assistance or ready-to-use tools, check out CodeHunger for expert guidance and solutions. Happy deploying! 😊