Next.js on EC2 with Express

Beginner
15 min read
Updated: Nov 2026
5 examples
Next.js / Express / PM2

Definition and Usage

This guide runs a Next.js app on Amazon EC2 behind a thin Express custom server and PM2. Express listens on port 80 and forwards requests to Next with next.getRequestHandler().

What is Next.js + Express on EC2?

Next.js is a React framework for full-stack apps. On a single Ubuntu VM you can wrap it with Express when you want one Node process for HTTP entry and Next rendering—useful for demos and some legacy patterns.

When should you use a custom server?

Use this pattern when you need Express middleware in the same process or are learning how custom servers work. For many production apps, next start behind Nginx or an Application Load Balancer is simpler. Read the current Next.js custom server docs for your major version.

What you will build

Scaffold with create-next-app, add root index.js, upload with FileZilla (no node_modules), then npm install, npm run build, and pm2 start index.js on the instance.

Related simpler path

Prefer a baseline Next deploy without Express? See Deploy Next.js on EC2. For a plain Express site, see Express.js on EC2.

Install on the server.

Upload source and lockfiles, then run npm install on EC2 so binaries match the instance CPU/OS.

Custom servers are optional.

Next.js ships next start for production. Some App Router / deployment features assume the default server—confirm trade-offs before locking this in.

Build before start.

Always run a production npm run build on EC2 before pm2 start so .next matches the server environment.

Core Concepts

Terms you will use in this deploy:

ConceptMeaning
create-next-appOfficial scaffold for a new Next.js project
Custom serverYour Node entry (here Express) that calls Next’s request handler
getRequestHandler()Next API that serves pages, assets, and API routes
npm run buildProduction compile into the .next folder
PM2Keeps index.js running after SSH disconnect
node_modulesInstall on EC2; do not upload from your laptop
Security groupMust allow inbound TCP 80 (and 22 for SSH)

Prerequisites

Have these ready before you deploy:

Examples

Scaffold Next.js, add Express, upload to EC2, build, start with PM2, and open the public URL.

Example 1 — Create the Next.js App Locally

Scaffold with the official CLI and confirm the template runs.

local.sh
npx create-next-app@latest my-nextjs-app
cd my-nextjs-app
npm run dev

Open http://localhost:3000/. Edit the home page under app/page.tsx (App Router) or pages/index.tsx (Pages Router)—there is no src/App folder in standard templates.

Default Next.js welcome page on localhost
Browser

How It Works

  1. create-next-app generates a ready project with scripts and dependencies.
  2. npm run dev starts the default Next dev server on port 3000.
  3. You customize pages first so the later EC2 deploy shows your content.
  4. Router choice (App vs Pages) only changes which file you edit—not the Express wrapper idea.

Example 2 — Add an Express Custom Server

In the project root (next to package.json), install Express and add index.js.

local.sh
npm install express
index.js
const express = require('express');
const next = require('next');

const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();

app.prepare().then(() => {
  const server = express();

  server.get('*', (req, res) => {
    return handle(req, res);
  });

  const PORT = process.env.PORT || 80;
  server.listen(PORT, (err) => {
    if (err) throw err;
    console.log(`> Ready on http://localhost:${PORT}`);
  });
});

How It Works

  1. next({ dev }) creates the Next app; production uses NODE_ENV=production.
  2. app.prepare() loads the build before Express accepts traffic.
  3. getRequestHandler() lets Express pass every path to Next.
  4. Port 80 is the demo default; set PORT for a high-port + proxy setup later.

Example 3 — Prepare EC2 and Upload (No node_modules)

Create a folder on the instance and SFTP the project without heavy folders.

  1. Confirm the instance is running; note the public DNS or Elastic IP.
  2. SSH with PuTTY or ssh, then create the deploy directory.
  3. Upload with FileZilla. Exclude .git, node_modules, and usually .next.
ec2.sh
node -v && npm -v
sudo mkdir -p /home/ubuntu/nextjs-website
sudo chown ubuntu:ubuntu /home/ubuntu/nextjs-website

# Demo only — tighten after upload
sudo chmod 777 /home/ubuntu/nextjs-website
EC2 instance running with Elastic IP
Amazon EC2
PuTTY session to Ubuntu EC2
PuTTY
Node and npm versions on Ubuntu
Ubuntu
nextjs-website folder permissions on EC2
Ubuntu
FileZilla uploading Next.js project to EC2
FileZilla

How It Works

  1. Source only on the wire keeps SFTP fast and avoids laptop-vs-server binary mismatches.
  2. chown lets the Ubuntu user write the project (prefer this over lasting 777).
  3. Skipping .next forces a clean production build on EC2.
  4. Include package-lock.json (or your lockfile) so installs are reproducible.

Example 4 — Install, Build, and Start with PM2

Install dependencies on the server, compile for production, then supervise with PM2.

ec2.sh
sudo npm i -g pm2

cd /home/ubuntu/nextjs-website
npm install
NODE_ENV=production npm run build

sudo pm2 start index.js
sudo pm2 status
Global PM2 install on Ubuntu
Ubuntu
PM2 start index.js for Next.js on EC2
Ubuntu

How It Works

  1. npm install restores Next, Express, and the rest for this Linux host.
  2. npm run build writes production output into .next.
  3. sudo pm2 start index.js runs the Express wrapper (helps bind port 80 in this demo).
  4. Open TCP 80 on the security group so browsers can reach the instance.
Prefer chown over root npm.

Use sudo npm install only if ownership is broken. Long-term, fix chown and run npm as your deploy user.

Example 5 — Open the App in a Browser

Confirm the public URL serves your Next.js UI through Express + PM2.

  1. Browse to http://<public-dns-or-ip>/.
  2. You should see your Next.js pages (same routes as local, now on port 80).
  3. On failure, check sudo pm2 logs, build success, and that express is in package.json.
Next.js app in browser via EC2 public address
Browser

How It Works

  1. The browser hits Express on 80; Express hands the request to Next.
  2. Next serves the production build from .next and your app routes.
  3. PM2 keeps the process up after you close SSH.
  4. Next upgrades: HTTPS, an ALB, CloudFront, or Nginx in front.

Edge Cases & Pitfalls

Most failures are a missing build, uploaded node_modules, or a closed security group.

build

Started without npm run build

Production mode expects a compiled .next folder. Build on EC2 before pm2 start.

npm

Uploaded laptop node_modules

Native modules can break across OS/CPU. Delete them on the server and run a fresh npm install.

express

Cannot find module ‘express’

Add Express to the project dependencies and reinstall on EC2. Confirm it appears in package.json.

port

EACCES on port 80

Low ports need privileges. Use sudo pm2 start for demos, or a high port behind Nginx/ALB.

sg

Security group blocks HTTP

Allow inbound TCP 80 from your client (and 22 for SSH) or the browser will time out.

next

Custom server feature gaps

Some Next features and hosting guides assume next start. Re-check docs if you rely on newer App Router deployment options.

Key Takeaways

  • Flow: create-next-app → Express index.js → upload source → install & build → PM2.
  • Install on EC2: never rely on uploaded node_modules for production deploys.
  • Build first: NODE_ENV=production npm run build before starting the custom server.
  • Optional pattern: Express is optional; next start + proxy/ALB is often enough.

One line: Express hands HTTP to Next; install and build on EC2; PM2 keeps it running.

Frequently Asked Questions

Express can listen on port 80 and pass every request to Next.js via next.getRequestHandler(). Some teams add middleware or legacy routes in one Node process. Many deployments instead use next start behind Nginx or a load balancer.
Usually no. Upload source and lockfiles, then run npm install on the server so native binaries match the instance architecture. Skipping node_modules also speeds up SFTP uploads.
Yes for production. next build compiles your app. Without a successful build, the production custom server will not serve optimized output.
No. Next.js includes next start for production. Use Express only when you need a custom Node entrypoint—and check the Next.js docs for your major version, because some features assume the default server.
Rebuild on the server with NODE_ENV=production npm run build so the output matches the EC2 Node version and environment. Uploading a laptop .next folder can cause subtle mismatches.
Add HTTPS (ACM + ALB or a reverse proxy), pm2 startup on reboot, log rotation, and avoid leaving chmod 777 or Node bound as root on port 80 in production.

Next: WordPress on AWS

Switch stacks and host WordPress with a guided EC2-oriented walkthrough.

WordPress on AWS →

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.

6 people found this page helpful