Launch an EC2 Instance

Easily Deploy Node.js Apps on AWS EC2

Deploying a Node.js app from Windows → WSL → AWS EC2, step-by-step, clearly and cleanly.

Why Choose EC2 Over Apache?

TL;DR:

EC2 vs Apache

Apache:

EC2:

Simple Analogy: EC2 is the house. Apache is the kitchen. You install Apache inside EC2 to “cook” your web content.



Deployment Goal

Deploy your local Node.js app (port 5001) to AWS EC2 using WSL and rsync.

Step-by-Step Deployment Guide

  1. Test app locally:
    npm install
    npm start

    Visit: http://localhost:5001

  2. Set up WSL:
    wsl --install

    Then restart and install rsync:

    sudo apt update
    sudo apt install rsync
  3. Go to your Node.js folder in WSL:
    cd "/mnt/c/Users/Dell Inspiron/Documents/GitHub/node-express-ec2"
  4. Move your SSH key to WSL:
    cp "/mnt/c/Users/Dell Inspiron/.ssh/ssh1.pem" ~/.ssh/
    chmod 400 ~/.ssh/ssh1.pem
  5. Run rsync to upload app to EC2:
    rsync -avz --exclude 'node_modules' --exclude '.git' --exclude '.env' \
    -e "ssh -i ~/.ssh/ssh1.pem" \
    . ubuntu@ec2-YOUR-PUBLIC-IP.compute-1.amazonaws.com:~/app
  6. SSH into EC2:
    ssh -i ~/.ssh/ssh1.pem ubuntu@ec2-YOUR-PUBLIC-IP.compute-1.amazonaws.com
  7. Set up Node.js environment:
    cd ~/app
    sudo apt update
    sudo apt install -y nodejs npm
    npm install
    npm start
  8. Access from browser:

    Update Inbound Rules in EC2 to allow port 5001, then visit:

    http://EC2_PUBLIC_IP:5001

Add Security to your EC2-hosted Node.js

you can absolutely add security to your EC2-hosted Node.js app and turn http into https by adding SSL/TLS encryption — which gives you that little padlock in the browser and serves your app over https://. Here’s how to do it using Let's Encrypt (free SSL certificates) with Nginx as a reverse proxy — the most common and recommended approach for a production-like setup.

need add more doucment below...

One-Click Deployment Script (Optional)

1. Create Script

nano deploy.sh

Paste this:

#!/bin/bash

KEY_PATH="$HOME/.ssh/ssh1.pem"
REMOTE_USER="ubuntu"
REMOTE_HOST="ec2-YOUR-PUBLIC-IP.compute-1.amazonaws.com"
REMOTE_DIR="~/app"

echo "🚀 Starting deployment to $REMOTE_HOST"

rsync -avz --exclude 'node_modules' --exclude '.git' --exclude '.env' \
-e "ssh -i $KEY_PATH" \
. $REMOTE_USER@$REMOTE_HOST:$REMOTE_DIR

echo "✅ Code synced. Logging into EC2..."
ssh -i $KEY_PATH $REMOTE_USER@$REMOTE_HOST << 'EOF'
  cd ~/app
  npm install
  npm start
EOF

2. Make Script Executable

chmod +x deploy.sh

3. Deploy

./deploy.sh