Deploying a Node Js Application on AWS EC2

Deploying a Node Js Application on AWS EC2

Are you a developer looking to host your Node.js app on AWS? In this guide, I’ll walk you through deploying and exposing a Node.js application on AWS EC2—perfect for beginners!

Before we start, make sure you have:

AWS Free Tier account (or an existing AWS account)

Basic knowledge of Linux commands (we’ll use Ubuntu)

A Node.js app (or use a simple express demo)


🚀Step 1: Launch an EC2 Instance

1. Go to AWS EC2 Console

  • Log in to AWS → Search for EC2 → Click "Launch Instance"

2. Configure Your Instance

  • Name: nodejs-server
  • OS: Ubuntu 22.04 LTS (Free Tier eligible)
  • Instance Type: t2.micro (Free Tier)
  • Key Pair: Create/download a .pem file (SSH access)

3. Allow HTTP Traffic (IMPORTANT!)

  • Under "Network Settings", check: Allow SSH (port 22) , Allow HTTP (port 80) &(Optional) Allow HTTPS (port 443)

4. Launch Instance

  • Click "Launch Instance" → Wait ~1 min


🔑Step 2: SSH into Your EC2 Instance

1. Find Your Instance’s Public IP

  • Go to EC2 Dashboard → Copy the IPv4 Public IP

2. Connect via SSH

chmod 400 your-key.pem  # Secure your key
ssh -i "your-key.pem" ubuntu@YOUR_PUBLIC_IP        

You’re now inside your EC2 server!


🛠️ Step 3: Install Node.js & NPM

1. Update Ubuntu Packages

sudo apt update        

2. Install Node.js (Latest LTS Version)

sudo apt install nodejs
        

3. Verify Installation

node -v        

OutPut :

Output
v12.22.9        

4. Install npm

sudo apt install npm        


📦 Step 4: Deploy Your Node.js App

1. Clone Your App (or Create a Demo One)

git clone YOUR_GITHUB_REPO_URL
cd your-app
npm install
npm run start        

Open http://YOUR_EC2_IP:3000 in a browser → Should see "Hello AWS!"

NOTE - We will have to edit the inbound rules in the security group of our EC2, in order to allow traffic from our particular port


⚡ Step 5: Keep Node.js Running Forever (PM2)

1. Install PM2 (Process Manager)

sudo npm install -g pm2        

2. Start Your App with PM2

pm2 start server.js --name "node-app"        

3. Auto-Start on Reboot

pm2 startup
pm2 save        

Now your app will survive server crashes/reboots!

🌍 Step 6: Expose Your App to the Internet (NGINX Reverse Proxy)

1. Install NGINX

sudo apt install nginx -y        

2. Configure NGINX

sudo nano /etc/nginx/sites-available/default        

Replace the file with:

server {
    listen 80;
    server_name YOUR_PUBLIC_IP;

    location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}        

Save (CTRL+X → Y → Enter)

3. Restart NGINX

sudo systemctl restart nginx        

🎉 Now visit http://YOUR_PUBLIC_IP (no port needed!)

🔒 (Optional) Secure with HTTPS (Free SSL via Let’s Encrypt)

sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d YOUR_DOMAIN.com  # (or use IP)        

Your app now has HTTPS!

!


📢 Final Thoughts

You just: ✔ Deployed a Node.js app on AWS EC2 ✔ Made it publicly accessible ✔ Added auto-restart (PM2) + HTTPS

Next Steps?

  • Try Dockerizing your app
  • Set up CI/CD with GitHub Actions

Need help? DM me for freelance DevOps support! 🚀

👉 Like & Repost if this helped you! 👉 Follow for more AWS/DevOps guides.

#AWS #DevOps #NodeJS #CloudComputing #WebDevelopment

To view or add a comment, sign in

More articles by Shital Meshram

Insights from the community

Others also viewed

Explore topics