DEV Community

alok-38
alok-38

Posted on

Setting Up a Node.js Backend on AWS EC2: A Practical Step-by-Step Guide

A Practical Guide to Learning Backend Engineering with Node.js on AWS EC2

If you’ve decided to learn backend engineering using Node.js on an AWS EC2 server, you’re on a solid path. Before jumping straight into application code, there are a few foundational steps that will save you hours (or days) of pain later.

Below is a practical, opinionated checklist that focuses on getting a sane, reachable, and resilient backend up and running.


1️⃣ Make Sure the Server Itself Is Sane (Non-Negotiable)

Before writing any app code, lock down the basics. A surprising number of issues come from not knowing what environment you’re actually in.

✅ Check OS & User

uname -a
lsb_release -a   # if available
whoami
Enter fullscreen mode Exit fullscreen mode

You want clear answers to:

  • What Linux distribution am I running?

  • Am I logged in as root or a normal user?

Knowing this affects package managers, permissions, and security decisions later.

✅ Update Packages

sudo dnf update -y
Enter fullscreen mode Exit fullscreen mode

(Use apt or yum depending on your distro.)

This reduces the chance of:

  • Security vulnerabilities

  • Strange dependency bugs

  • Outdated system libraries breaking installs

2️⃣ Basic Security Hygiene (Do This Early)

You don’t need enterprise-grade security yet — just don’t be reckless.

🔐 Confirm Firewall Rules

On the EC2 Security Group (AWS Console), make sure you explicitly allow only what you need:

  • Port 22 → SSH (restrict to your IP if possible)

  • Port 80 → HTTP

  • Port 443 → HTTPS (later, when TLS is set up)

💡 On EC2, Security Groups are your primary firewall. Many instances don’t even need a local firewall initially.

3️⃣ Install Your Runtime (Language First, Always)

Before frameworks, before databases — install your language runtime.

Node.js

curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install -y nodejs
Enter fullscreen mode Exit fullscreen mode

Verify the install:

node -v
npm -v
Enter fullscreen mode Exit fullscreen mode

If these commands work, your runtime is ready.

4️⃣ Create a Minimal Backend App

Your first milestone is simple:

“Can the internet hit my server?”

Nothing else matters until this works.

Example: Node.js + Express

mkdir app && cd app
npm init -y
npm install express
Enter fullscreen mode Exit fullscreen mode

Create index.js:

const express = require("express");
const app = express();

app.get("/", (req, res) => {
  res.send("EC2 backend is alive 🚀");
});

app.listen(3000, () => {
  console.log("Listening on port 3000");
});
Enter fullscreen mode Exit fullscreen mode

Run it:

node index.js
Enter fullscreen mode Exit fullscreen mode

From your local machine, open:

http://<EC2_PUBLIC_IP>:3000
Enter fullscreen mode Exit fullscreen mode

If you see the response → huge milestone 🎉

You’ve proven networking, runtime, and app code all work together.

5️⃣ Make the App Survive Disconnects (Process Manager)

Right now, there’s a big problem:

  • Close your SSH session → app dies ❌

That’s expected, but not acceptable for a real backend.

Install PM2 (Node.js Process Manager)

sudo npm install -g pm2
pm2 start index.js --name backend
pm2 status
pm2 save
Enter fullscreen mode Exit fullscreen mode

Now you get:

  • App survives SSH logout

  • Easy restarts

  • Basic process monitoring

I learnt that professional backend engineers automate this procedure using bash or python scripts like so

#!/usr/bin/env bash

set -e

echo "🚀 Starting EC2 bootstrap..."

# ----------------------------
# System update
# ----------------------------
echo "📦 Updating system packages..."
sudo dnf update -y

# ----------------------------
# Install base dependencies
# ----------------------------
echo "🔧 Installing base tools..."
sudo dnf install -y \
  git \
  nginx \
  htop \
  unzip \
  curl

# ----------------------------
# Install NVM
# ----------------------------
echo "🟢 Installing NVM..."

export NVM_DIR="$HOME/.nvm"

if [ ! -d "$NVM_DIR" ]; then
  curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
else
  echo "NVM already installed"
fi

# Load NVM into this shell
source "$NVM_DIR/nvm.sh"

# ----------------------------
# Install Node.js (LTS)
# ----------------------------
echo "📦 Installing Node.js (LTS)..."

nvm install --lts
nvm use --lts
nvm alias default lts/*

# ----------------------------
# Verification
# ----------------------------
echo "✅ Verifying installation..."
node -v
npm -v

echo "🎉 Bootstrap complete!"
Enter fullscreen mode Exit fullscreen mode

What’s Next?

Once this foundation is solid, you can move on to:

  • Nginx (reverse proxy, ports 80/443)

  • Observability (logs, metrics, health checks)

  • Databases (Postgres, MySQL, MongoDB, etc.)

  • HTTPS (Let’s Encrypt)

  • CI/CD and automated deployments

But don’t rush. A stable, boring backend setup is a huge win.

Top comments (0)