Deploy Node.js on EC2

Beginner
11 min read
Updated: Nov 2026
5 examples
Node.js / PM2 / Ubuntu

Definition and Usage

This guide hosts a tiny Node.js website on Amazon EC2 using only the built-in http module—no frameworks—then keeps it online with PM2.

What is a vanilla Node.js site on EC2?

Node can answer HTTP with http.createServer. On Ubuntu EC2 you upload a few files, open port 80 in the security group, and run the process under PM2 so it survives SSH disconnects.

When should you use this approach?

Use it to learn EC2 networking, SFTP, and process management with the smallest possible app. When routes and middleware grow, move to Express.js on EC2 or a full stack such as Next.js on EC2.

What you will build

Two files (index.js + hello-world.html), uploaded with FileZilla, started with pm2 start index.js, and opened via the public DNS or Elastic IP.

Tools in this walkthrough

PuTTY (or SSH), Node.js and npm on Ubuntu, FileZilla for SFTP, and PM2 as the process manager.

No npm project required.

This sample uses only Node built-ins (http, fs, path). You still install PM2 with npm.

Avoid chmod 777 in production.

Wide-open permissions are only for a quick FileZilla demo. Prefer chown for your deploy user afterward.

Port 80 is privileged.

Listening on 80 usually needs elevated privileges. Production often uses a high port behind Nginx or an ALB on 80/443.

Core Concepts

Terms you will use while deploying:

ConceptMeaning
http.createServerNode API that handles incoming HTTP requests
req.urlPath used for simple routing (/, /hello-world)
fs.readFileReads hello-world.html from disk for that route
PM2Keeps index.js running after SSH logout
Security groupMust allow inbound TCP 80 (and 22 for SSH)
Elastic IPStable public IPv4 across stop/start
SFTP / FileZillaSecure upload of project files to the instance

Prerequisites

Have these ready before you deploy:

Examples

Build a minimal HTTP server, place it on EC2, start it with PM2, and open it in a browser.

Example 1 — Minimal Node.js Sample

Create index.js and hello-world.html locally.

index.js
const http = require('http');
const fs = require('fs');
const path = require('path');

const server = http.createServer((req, res) => {
  if (req.url === '/') {
    res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' });
    res.end('Hello, this is the root!');
  } else if (req.url === '/hello-world') {
    const filePath = path.join(__dirname, 'hello-world.html');
    fs.readFile(filePath, 'utf8', (err, data) => {
      if (err) {
        res.writeHead(500, { 'Content-Type': 'text/plain; charset=utf-8' });
        res.end('Internal Server Error');
      } else {
        res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
        res.end(data);
      }
    });
  } else {
    res.writeHead(404, { 'Content-Type': 'text/plain; charset=utf-8' });
    res.end('404 Not Found');
  }
});

const PORT = 80;

server.listen(PORT, () => {
  console.log(`Server running at http://localhost:${PORT}/`);
});
hello-world.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Hello World</title>
</head>
<body>
  <h1>Hello, World!</h1>
  <p>This page is served by Node.js.</p>
</body>
</html>

How It Works

  1. createServer runs a callback for every HTTP request.
  2. / returns plain text; /hello-world reads and sends the HTML file.
  3. Unknown paths get a simple 404 response.
  4. listen(80) binds the default HTTP port for easy browser tests.

Example 2 — Connect and Prepare the Project Folder

Confirm the instance, SSH in, verify Node, and create a deploy directory.

  1. In the EC2 console, confirm the instance is running and note the public DNS or Elastic IP.
  2. Connect with PuTTY or ssh.
  3. Run node -v and npm -v. If missing, follow Install Node.js and npm.
  4. Create and own the project folder:
ec2.sh
sudo mkdir -p /home/ubuntu/nodejs-website
sudo chown ubuntu:ubuntu /home/ubuntu/nodejs-website

# Demo only � tighten permissions after upload
sudo chmod 777 /home/ubuntu/nodejs-website
EC2 instance running with Elastic IP
Amazon EC2
PuTTY SSH session to Ubuntu EC2
PuTTY
Node and npm versions on Ubuntu
Ubuntu
Creating nodejs-website directory on EC2
Ubuntu
Folder permissions on EC2
Ubuntu

How It Works

  1. SSH gives you a shell on the machine that will run Node.
  2. Node must exist before you start the server or install PM2.
  3. mkdir + chown creates a folder your Ubuntu user can write to.
  4. chmod 777 is demo-only—tighten ownership after FileZilla uploads.

Example 3 — Upload Files with FileZilla

Copy index.js and hello-world.html to the instance.

  1. Open FileZilla with SFTP and your key.
  2. Navigate to /home/ubuntu/nodejs-website on the server.
  3. Upload both files from your laptop.
FileZilla uploading files to EC2
FileZilla

How It Works

  1. SFTP encrypts the transfer over SSH.
  2. Keep paths consistent with __dirname so the HTML file sits next to index.js.
  3. No node_modules for this sample—only the two source files.
  4. Alternative: git clone or scp if you prefer the command line.

Example 4 — Install PM2 and Start the Server

Install the process manager globally, then start index.js.

ec2.sh
sudo npm i -g pm2

cd /home/ubuntu/nodejs-website
sudo pm2 start index.js
sudo pm2 status
Global PM2 install on Ubuntu
Ubuntu
PM2 start index.js on EC2
Ubuntu

How It Works

  1. Global PM2 gives you the pm2 CLI on the server.
  2. sudo pm2 start helps bind privileged port 80 in this demo.
  3. PM2 keeps the process alive after you close PuTTY.
  4. Open TCP 80 on the security group so browsers can reach the instance.
Security group.

Allow inbound TCP 80 (and 22 for SSH) from your test client, or change the app to a high port and open that port instead. See security groups.

Example 5 — Open the Site in a Browser

Hit the public address and confirm both routes respond.

  1. Browse to http://<public-dns-or-ip>/ for the plain-text root.
  2. Open /hello-world for the HTML page.
  3. If it fails, check the security group, sudo pm2 status, and that nothing else owns port 80.
Browser showing Node.js site on EC2
Browser

How It Works

  1. The browser talks HTTP to the instance on port 80.
  2. Your createServer callback matches the path and returns text or HTML.
  3. Security groups sit in front—if 80 is closed, the page never loads.
  4. Next steps: Express, HTTPS, an ALB, or Nginx.

Edge Cases & Pitfalls

Most “site won’t load” issues are security groups, port privileges, or a missing HTML file path.

port

EACCES on port 80

Non-root processes cannot bind low ports. Use sudo pm2 start for demos, or listen on 3000+ behind a proxy/ALB.

sg

Security group blocks HTTP

Inbound TCP 80 must allow your client. SSH still needs TCP 22.

path

500 on /hello-world

Usually the HTML file is missing or not next to index.js. Check the upload path and __dirname.

pm2

App dies after reboot

pm2 start alone does not survive reboot. Use pm2 startup / pm2 save when you want auto-start.

perms

Leaving chmod 777 forever

World-writable app dirs are a security risk. Fix ownership after FileZilla uploads.

scale

Manual if/else routing gets messy

When you need many routes or middleware, graduate to Express.

Key Takeaways

  • Start small: http.createServer is enough to learn EC2 + PM2.
  • PM2: keeps Node running after SSH disconnect; open the right security group ports.
  • Permissions: prefer chown over lasting 777.
  • Next level: Express, HTTPS, and a reverse proxy/ALB for real apps.

One line: upload two files, pm2 start index.js, open port 80, then harden for production.

Frequently Asked Questions

PM2 keeps your Node process running after you close SSH, can restart it on crashes, and can hook into system startup. For a single EC2 instance it is a simple alternative to writing a systemd unit by hand.
Port 80 is the default HTTP port so you can test with http://your-ip/ without a port suffix. On Linux, ports below 1024 are privileged, so the example uses sudo with PM2. Production setups often use a high port plus Nginx on 80/443.
Use Express or another framework when you need cleaner routing, middleware, body parsing, and a large ecosystem. The http module is fine for tiny demos.
No. The sample uses only Node built-ins (http, fs, path). You still need Node installed on the instance, and PM2 is installed globally with npm.
No. It is only a quick demo shortcut for SFTP writes. Prefer chown for your deploy user and tight permissions afterward.
Add HTTPS (ACM + ALB or a reverse proxy), pm2 startup on reboot, monitoring, and move to Express or Next when routing grows.

Next: Express.js on EC2

Add Express for cleaner routes, static files, and a more realistic Node app shape.

Express.js on EC2 →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful