When I first started working with Laravel, deploying my project to a live server felt like a daunting task. But after experimenting with AWS EC2, I realized it’s not as complicated as it seems. AWS EC2 is a powerful and scalable cloud platform that’s perfect for hosting Laravel applications.
In this guide, I’ll share my experience and walk you through the process of deploying a Laravel 12 project on AWS EC2 in simple, beginner-friendly steps. Whether you’re new to AWS or Laravel, this tutorial will help you get your app live on the cloud with ease.
Before we dive in, make sure you have:
Let’s get started!
First, I log in to my AWS Management Console and navigate to the EC2 service. Here’s how I set up my instance:
t2.micro
(eligible for AWS Free Tier)..pem
file for SSH access and store it securely.Once the instance is running, I connect to it using SSH:
ssh -i your-key.pem ubuntu@your-ec2-public-ip
.Now, I’m inside my EC2 instance and ready to set up the server.
To ensure the server is up-to-date and has the necessary software, I run these commands:
sudo apt update && sudo apt upgrade -y
Next, I install PHP 8.1 (required for Laravel 12) and other dependencies:
sudo add-apt-repository ppa:ondrej/php -y
sudo apt update
sudo apt install -y php8.1 php8.1-cli php8.1-fpm php8.1-mysql php8.1-xml php8.1-mbstring php8.1-curl php8.1-zip php8.1-gd nginx git unzip
I also install Composer, Laravel’s dependency manager:
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
Most Laravel apps use a database, so I set up MySQL:
sudo apt install -y mysql-server
sudo systemctl start mysql
sudo systemctl enable mysql
Then, I secure MySQL and create a database:
sudo mysql_secure_installation
sudo mysql
CREATE DATABASE laravel_db;
CREATE USER 'laravel_user'@'localhost' IDENTIFIED BY 'your_secure_password';
GRANT ALL PRIVILEGES ON laravel_db.* TO 'laravel_user'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Now, I move my Laravel project to the server. I prefer using Git for this:
sudo apt install git -y
cd /var/www
sudo git clone https://github.com/your_username/your_project.git laravel
sudo chown -R www-data:www-data /var/www/laravel
sudo chmod -R 775 /var/www/laravel/storage /var/www/laravel/bootstrap/cache
cd /var/www/laravel
composer install --optimize-autoloader --no-dev
I copy the .env.example
file and update it with my database credentials:
cp .env.example .env
nano .env
In the .env
file, I update:
APP_URL=http://your-ec2-public-ip
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_db
DB_USERNAME=laravel_user
DB_PASSWORD=your_secure_password
Then, I generate an application key:
php artisan key:generate
I use Nginx as my web server. I create a new configuration file:
sudo nano /etc/nginx/sites-available/laravel
I add the following configuration:
server {
listen 80;
server_name your-ec2-public-ip;
root /var/www/laravel/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\. {
deny all;
}
}
I enable the configuration and restart Nginx:
sudo ln -s /etc/nginx/sites-available/laravel /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx
To set up the database schema, I run:
php artisan migrate
For better performance, I cache configurations:
php artisan config:cache
php artisan route:cache
php artisan view:cache
I open my browser and enter my EC2 instance’s public IP. If everything is set up correctly, my Laravel 12 app should load. If face issues, check the logs in /var/www/laravel/storage/logs
.
Deploying my Laravel 12 project on AWS EC2 was a rewarding experience. By following these steps, I was able to set up a scalable and reliable server for my application. AWS EC2 offers flexibility and power, making it a great choice for hosting Laravel apps. With this guide, I hope you feel confident to deploy your own project and take it live.
Q: Do I need an AWS account to deploy Laravel 12 on EC2?
A: Yes, you need an AWS account. The free tier is sufficient for testing and small projects.
Q: Can I use Apache instead of Nginx?
A: Absolutely! Install Apache instead of Nginx and configure it to point to your Laravel project’s public
directory.
Q: Why is my Laravel app not loading after deployment?
A: Check your Nginx configuration, file permissions, and .env
settings. Also, review the logs in storage/logs
for errors.
Q: How can I secure my Laravel app on EC2?
A: Enable HTTPS by adding an SSL certificate (e.g., using Let’s Encrypt), restrict SSH access, and keep your server updated.
Q: Can I scale my Laravel app on EC2?
A: Yes, use AWS Auto Scaling and a Load Balancer to handle increased traffic and ensure high availability.
You might also like :