Deploying a Node.js app from Windows β WSL β AWS EC2, step-by-step, clearly and cleanly.
Reference:Sam Meech-Wardβs Guide
π‘ Simple Analogy: EC2 is the house. Apache is the kitchen. You install Apache inside EC2 to βcookβ your web content.
Deploy your local Node.js app (port 5001) to AWS EC2 using WSL and rsync
.
npm install npm start
Visit: http://localhost:5001
wsl --install
Then restart and install rsync:
sudo apt update sudo apt install rsync
cd "/mnt/c/Users/Dell Inspiron/Documents/GitHub/node-express-ec2"
cp "/mnt/c/Users/Dell Inspiron/.ssh/ssh1.pem" ~/.ssh/ chmod 400 ~/.ssh/ssh1.pem
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
ssh -i ~/.ssh/ssh1.pem ubuntu@ec2-YOUR-PUBLIC-IP.compute-1.amazonaws.com
cd ~/app sudo apt update sudo apt install -y nodejs npm npm install npm start
Update Inbound Rules in EC2 to allow port 5001, then visit:
http://EC2_PUBLIC_IP:5001
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
chmod +x deploy.sh
./deploy.sh