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

deploy Laravel on AWS EC2, Laravel deployment AWS, Laravel AWS EC2 tutorial

How I Deployed Laravel on AWS EC2 (And What I Wish I Knew Before)

So, full honesty here: the first time I tried deploying a Laravel project to AWS EC2, I almost gave up halfway.

It’s not that AWS is hard — it’s just… opinionated. There are a lot of options, settings, dropdowns, and terms that seem like they were designed to scare developers away.

But hey, I figured it out. And now that I’ve been through it (twice, because I messed up the first one), here’s a guide — not just with the steps, but with real-life gotchas I wish someone told me before.


Step 1: Choosing the Right EC2 Setup

Before anything, you need an AWS account. No brainer, right? Once you’re in, head to EC2 and spin up a new instance.

Here’s what I learned quickly:

  • Use Ubuntu 20.04 or 22.04 – Laravel works fine there, and there’s tons of community support.
  • Instance typet2.micro is okay for testing. For anything real, I’d bump it up to t3.small or more.
  • Security group setup is crucial – I forgot to open port 80 once, and spent 40 minutes wondering why the site wasn’t loading 😅

✅ Open ports 22 (SSH), 80 (HTTP), and maybe 443 (HTTPS if you’re going SSL later).


Step 2: SSH Into Your EC2 Box

Once the instance is running, download the .pem key and SSH in:

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

Don’t lose that .pem file. AWS won’t let you download it again. Learned that the hard way.


Step 3: Installing the Basics

This part’s pretty standard. Update everything first:

sudo apt update && sudo apt upgrade -y

Then install:

  • Nginx
  • PHP + required extensions
  • MySQL or MariaDB
  • Composer
sudo apt install nginx php php-mysql php-cli php-curl php-zip php-mbstring php-xml unzip mysql-server -y

I won’t lie — I always have to Google which PHP extensions Laravel actually needs. Don’t be like me, just install that full list above.


Step 4: MySQL Setup (Skip If You Use RDS)

If you’re running MySQL on the same server:

sudo mysql_secure_installation

Then log in:

sudo mysql -u root -p

Create your DB:

CREATE DATABASE laravel_db;
CREATE USER 'laravel'@'localhost' IDENTIFIED BY 'yourpassword';
GRANT ALL PRIVILEGES ON laravel_db.* TO 'laravel'@'localhost';
FLUSH PRIVILEGES;

Keep those credentials safe. You’ll need them in your .env.


Step 5: Uploading Your Laravel App

Here I had two options:

  1. Clone from GitHub
  2. Use scp or SFTP to push the code

For simplicity:

sudo apt install git
cd /var/www
sudo git clone https://github.com/your-name/your-laravel-project.git

Then go into the project:

cd your-laravel-project
composer install

Set permissions:

sudo chown -R www-data:www-data .
chmod -R 775 storage bootstrap/cache

Copy .env, generate app key:

cp .env.example .env
php artisan key:generate

Then open .env and put in your DB credentials.


Step 6: Configuring Nginx

This is where I got stuck the longest. Here’s what finally worked:

server {
    listen 80;
    server_name your-ec2-ip;

    root /var/www/your-laravel-project/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 ~ /\.ht {
        deny all;
    }
}

Save it to /etc/nginx/sites-available/laravel, then:

sudo ln -s /etc/nginx/sites-available/laravel /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx

Pro tip: Run php -v to confirm which PHP version you’re using — and make sure Nginx points to the correct php-fpm.sock.


Step 7: Migrate & Test

Back to Laravel:

php artisan migrate

If it throws errors, double check .env and DB permissions.

Now go to your EC2 public IP in the browser and boom — Laravel!


Bonus: HTTPS with Let’s Encrypt

If you’re running a real site, you’ll want SSL.

Install Certbot:

sudo apt install certbot python3-certbot-nginx -y

Then run:

sudo certbot --nginx

Follow the prompts, and you’ll get HTTPS set up within minutes.


Final Thoughts

Deploying Laravel on AWS EC2 isn’t hard, but it is full of little gotchas. Permissions, Nginx configs, forgetting to open ports — I’ve hit all of them.

But once it’s set up, you’ve got a blazing fast, super-flexible Laravel app running in the cloud. And that’s pretty awesome.

Would I use EC2 for everything? Probably not. For big projects or when you need full control, yes. For simpler stuff, Laravel Forge or Laravel Cloud might save you a few headaches.

But if you like getting your hands dirty (and learning a ton in the process), EC2’s still a solid choice.


Pro Tip: Set up a swap file if you’re using a t2.micro instance — Composer install will cry without enough memory.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top