Deploy Express.js on EC2

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

Definition and Usage

This guide shows how to host a small Express.js website on an Amazon EC2 Ubuntu instance: upload the app, install dependencies, and keep it running with PM2 so it survives SSH disconnects.

What is Express.js on EC2?

Express is a minimal Node.js web framework. On EC2 you get a Linux VM where you install Node, copy project files, and listen on a port the security group allows. That pattern suits APIs, SSR apps, and small sites.

When should you use this approach?

Use a single EC2 + Express setup for learning, demos, and small workloads. For production scale, put an Application Load Balancer (and often CloudFront) in front, add HTTPS, and avoid running Node as root on port 80.

What you will build

A two-file sample (index.js + hello-world.html), uploaded with FileZilla, started with PM2, and opened in a browser via the instance public DNS or Elastic IP.

Tools in this walkthrough

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

Local Express install.

Always run npm install express inside the project folder so require('express') resolves from node_modules.

Avoid chmod 777 in production.

Wide-open permissions are only for a quick demo upload. Prefer chown for your deploy user and tight modes on app folders and keys.

Port 80 is privileged.

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

Core Concepts

Terms you will use while deploying:

ConceptMeaning
EC2 instanceVirtual server (here: Ubuntu) that runs your Node process
ExpressNode framework for routes, static files, and HTTP responses
PM2Process manager that keeps the app up after SSH logout
Security groupFirewall allowing inbound TCP (e.g. 22 for SSH, 80 for HTTP)
Elastic IPStable public IPv4 so the address survives stop/start
SFTP / FileZillaSecure file transfer of project files to the instance
express.staticMiddleware that serves files from a folder such as public/

Prerequisites

Have these ready before you deploy:

Examples

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

Example 1 — Minimal Express.js Sample

Create index.js and hello-world.html locally (and a public/ folder for static assets if you need one).

index.js
const express = require('express');
const path = require('path');

const app = express();
const port = 80;

app.use(express.static(path.join(__dirname, 'public')));

app.get('/', (req, res) => {
  res.send('Hello, this is the root route!');
});

app.get('/hello-world', (req, res) => {
  res.sendFile(path.join(__dirname, 'hello-world.html'));
});

app.listen(port, () => {
  console.log(`Server is 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 Express.js.</p>
</body>
</html>

How It Works

  1. express() creates the app; listen(80) opens HTTP on port 80.
  2. express.static serves files from public/ automatically.
  3. GET / returns a plain text hello from the root route.
  4. GET /hello-world sends the HTML file with res.sendFile.

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:
terminal.sh
sudo mkdir -p /home/ubuntu/express-website
sudo chown ubuntu:ubuntu /home/ubuntu/express-website
EC2 instance running with Elastic IP
Amazon EC2
PuTTY SSH session to Ubuntu EC2
PuTTY
Terminal showing node and npm versions
Ubuntu
Creating express-website directory on EC2
Ubuntu

How It Works

  1. SSH gives you a shell on the same machine that will run Express.
  2. Node and npm must exist before npm install or PM2 can work.
  3. mkdir + chown creates a folder your Ubuntu user can write to.
  4. Demo-only: some tutorials use sudo chmod 777 … for FileZilla—tighten that afterward.
demo-only.sh
# Demo only — tighten permissions after upload
sudo chmod 777 /home/ubuntu/express-website
Folder permissions after chmod on EC2
Ubuntu

Example 3 — Upload Files with FileZilla

Copy index.js, hello-world.html, and optional public/ assets to the instance.

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

How It Works

  1. SFTP encrypts the transfer over SSH—safer than plain FTP.
  2. You place source files on disk; dependencies still come from npm install on the server.
  3. Keep paths consistent with __dirname in index.js (HTML next to the script).
  4. Alternative: git clone on the server if your repo is already remote.

Example 4 — Install Express Locally and Start with PM2

Install the process manager and project dependency, then start the app.

install-and-start.sh
sudo npm i -g pm2

cd /home/ubuntu/express-website
npm init -y
npm install express

sudo pm2 start index.js
sudo pm2 status
npm global install of PM2 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. npm install express in the project creates node_modules for require('express').
  3. sudo pm2 start helps bind privileged port 80 in this demo.
  4. PM2 keeps the process alive after you close PuTTY; check with sudo pm2 status.
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.

Example 5 — Open the Site in a Browser

Hit the public address and confirm both routes respond.

  1. Browse to http://<Public IPv4 DNS> or http://<Elastic IP>.
  2. Confirm the root message, then try /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 Express app served from EC2
Browser

How It Works

  1. The browser talks HTTP to the instance IP/DNS on port 80.
  2. Express matches the path and returns text or the HTML file.
  3. Security groups sit in front—if 80 is closed, the page never loads.
  4. Next upgrades: HTTPS, an ALB, HTTP→HTTPS, or CloudFront.

Edge Cases & Pitfalls

Most “site won’t load” issues are security groups, missing node_modules, or port 80 privileges.

npm

Cannot find module ‘express’

You started the app without a local install. Run npm install express in the same folder as index.js.

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 (or 0.0.0.0/0 for a public demo). SSH still needs TCP 22.

pm2

App dies after reboot

pm2 start alone does not survive instance 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.

ip

Public IP changed after stop/start

Attach an Elastic IP so the address stays stable.

Key Takeaways

  • Install locally: npm install express in the project—not a global Express for require.
  • PM2: keeps the process running after SSH disconnect; open the right security group ports.
  • Demo vs prod: port 80 + sudo is fine for learning; production wants proxy/ALB + HTTPS.
  • Permissions: skip lasting 777; use least privilege after uploads.

One line: upload the app, npm install express, pm2 start, open port 80, then harden for production.

Frequently Asked Questions

PM2 is a Node.js process manager. It keeps your app running after you disconnect SSH, can restart it on crash, and provides basic status and logging commands.
No. Install Express in the project folder with npm install express so require('express') resolves from node_modules. Global installs are not required for a normal Express app.
On Linux, ports below 1024 are privileged. Run PM2 with elevated privileges for demos, or listen on a high port and put Nginx (or an ALB) on 80/443 in front.
It is strongly recommended. Without an Elastic IP, the public IPv4 address can change when you stop and start the instance, which breaks bookmarks and DNS until you update them.
No. Use it only for a quick FileZilla demo if needed, then tighten ownership and permissions. Prefer chown for your deploy user and least-privilege modes.
Add HTTPS (ACM + ALB or a reverse proxy), pm2 startup on reboot, log rotation, monitoring, and avoid exposing Node directly on port 80 in production.

Next: Deploy React on EC2

Build and serve a React front end on the same Ubuntu + Node pattern.

React 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.

7 people found this page helpful