Setting up a Laravel application on AWS is a powerful way to scale your web applications in a secure and cloud-native environment. This guide walks you through deploying Laravel on an Ubuntu-based EC2 instance, connecting it to an Amazon RDS MySQL database, and using NGINX as the web server.
Prerequisites
Make sure you have the following:
- ✅ AWS EC2 instance (Ubuntu 22.04+ recommended)
- ✅ Amazon RDS MySQL database (accessible to EC2)
- ✅ Security Groups properly configured (EC2 <-> RDS: port 3306)
- ✅ NGINX installed on EC2
- ✅ SSH terminal access to EC2 (e.g., Termius)
- ✅ Laravel knowledge (Composer installed)
Step 1: Update Your Server & Install PHP and Required Extensions
sudo apt update && sudo apt upgrade -y
sudo apt install php php-fpm php-mysql php-xml php-mbstring php-curl php-zip unzip curl git -y
Step 2: Install Composer (PHP Dependency Manager)
cd ~
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
composer --version
Step 3: Create Laravel Project
cd /var/www/
sudo composer create-project laravel/laravel myapp
sudo chown -R ubuntu:ubuntu myapp
Tip: Replace
myapp
with your desired app folder name.
Step 4: Configure Laravel to Use Amazon RDS
Edit the .env
file:
nano /var/www/myapp/.env
Update the DB connection settings:
DB_CONNECTION=mysql
DB_HOST=your-rds-endpoint.amazonaws.com
DB_PORT=3306
DB_DATABASE=yourdbname
DB_USERNAME=yourdbuser
DB_PASSWORD=yourdbpassword
Step 5: Generate Laravel App Key & Run Migration
Ensure the Laravel CLI (artisan
) has permission to write logs:
sudo chown -R ubuntu:ubuntu /var/www/myapp
Then run:
cd /var/www/myapp
php artisan key:generate
php artisan migrate
✅ You should see success messages for both.
Step 6: Stop Apache (If Installed) & Configure NGINX
Apache often comes pre-installed — stop and disable it:
sudo systemctl stop apache2
sudo systemctl disable apache2
Create a new NGINX config:
sudo nano /etc/nginx/sites-available/laravel
Paste the following (replace <your-ip>
and php8.3
if needed):
server {
listen 80;
server_name <your-ec2-public-ip>;
root /var/www/myapp/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:/run/php/php8.3-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
Enable the Laravel site:
sudo ln -s /etc/nginx/sites-available/laravel /etc/nginx/sites-enabled/
sudo rm /etc/nginx/sites-enabled/default
Test and restart NGINX:
sudo nginx -t
sudo systemctl restart nginx
Step 7: Set Folder Permissions for Runtime
sudo chown -R www-data:www-data /var/www/myapp
sudo chmod -R 775 /var/www/myapp/storage
sudo chmod -R 775 /var/www/myapp/bootstrap/cache
Step 8: Visit Your Application
Go to your EC2 public IP in a browser:
http://<your-ec2-public-ip>
You should see the Laravel welcome page.
Top comments (0)