Deploy Next.js on EC2

Beginner
13 min read
Updated: Nov 2026
5 examples
Next.js / PM2 / Ubuntu

Definition and Usage

This guide hosts a Next.js app on Amazon EC2 using the stock production server (next start via npm start) under PM2—no custom Express wrapper required.

What is Next.js on EC2 with next start?

After next build, next start serves the compiled app from .next. On Ubuntu EC2 you install Node, upload sources, build on the server, and let PM2 keep the process running.

When should you use this approach?

Prefer this path for most single-VM demos and small production apps. Use Next.js + Express on EC2 only when you need a custom Node HTTP layer in the same process. For scale and HTTPS, add an ALB, ACM, or CloudFront.

What you will build

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

Why PM2?

Closing SSH would kill a foreground npm start. PM2 supervises the process, shows logs/status, and can restart the app after reboot when you run pm2 startup / pm2 save.

Stock production path.

Build, then npm start (next start). That matches how Next.js expects a single Node host to run.

Single process first.

The sample config uses one fork worker. Enabling PM2 cluster with many instances on a small VM can starve CPU/RAM—measure before scaling workers.

Install and build on EC2.

Skip uploading node_modules and usually .next; regenerate them on the instance for the correct OS/CPU.

Core Concepts

Terms you will use in this deploy:

ConceptMeaning
create-next-appOfficial scaffold for a new Next.js project
next buildCompiles the app into the .next production output
next start / npm startServes the production build (no custom Express file)
pm2.config.jsPM2 app definition: script, env, instances, exec mode
fork modeOne Node process (safer default on small instances)
cluster modeMultiple workers; only after you have spare CPU/RAM
Security groupMust allow inbound TCP 80 (and 22 for SSH)

Prerequisites

Have these ready before you deploy:

Examples

Scaffold Next.js, add a PM2 config, upload to EC2, build, start with next start, 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 app/page.tsx (App Router) or pages/index.tsx (Pages Router)—there is no src/App folder in standard templates.

Default Next.js app on localhost
Browser

How It Works

  1. create-next-app generates scripts, including build and start.
  2. npm run dev is for local editing on port 3000.
  3. Customize pages now so the EC2 deploy shows your content.
  4. Production later uses next start—not the Express custom-server path.

Example 2 — Add pm2.config.js

In the project root (next to package.json), define how PM2 should run npm start.

pm2.config.js
module.exports = {
  apps: [
    {
      name: 'next-app',
      script: 'npm',
      args: 'start',
      instances: 1,
      exec_mode: 'fork',
      env: {
        NODE_ENV: 'production',
        PORT: 80,
      },
    },
  ],
};

How It Works

  1. script: 'npm' + args: 'start' runs the Next production server.
  2. NODE_ENV: 'production' matches a production build.
  3. PORT: 80 is the demo HTTP port (needs privileges / security group).
  4. instances: 1 + fork keeps one process—safe default for small VMs.

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; verify Node with node -v / npm -v.
  3. Upload with FileZilla. Exclude .git, node_modules, and usually .next.
ec2.sh
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 on EC2
Ubuntu
FileZilla uploading Next.js project to EC2
FileZilla

How It Works

  1. Source + lockfile on the wire keeps uploads fast and reproducible.
  2. chown lets your Ubuntu user write the project (prefer over lasting 777).
  3. Skipping .next forces a clean production build on EC2.
  4. Include pm2.config.js so the start command is ready on the server.

Example 4 — Install, Build, and Start with PM2

Install dependencies on the server, compile for production, then start via the PM2 config.

ec2.sh
sudo npm i -g pm2

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

sudo pm2 start pm2.config.js
sudo pm2 status
Global PM2 install on Ubuntu
Ubuntu
PM2 starting Next.js with pm2.config.js
Ubuntu

How It Works

  1. npm install restores Next and other deps for this Linux host.
  2. npm run build writes production output into .next.
  3. pm2 start pm2.config.js runs npm start → next start with your env.
  4. Open TCP 80 on the security group; optionally pm2 save + pm2 startup for reboots.
Port 80 needs privileges.

This demo uses sudo pm2 start. Production often listens on a high port behind Nginx or an ALB on 80/443.

Example 5 — Open the App in a Browser

Confirm the public URL serves your Next.js production build.

  1. Browse to http://<public-dns-or-ip>/.
  2. You should see your Next.js pages served by next start.
  3. On failure, check sudo pm2 logs, build success, and the security group.
Next.js app served from EC2 public address
Browser

How It Works

  1. The browser hits port 80 on the instance public address.
  2. PM2 keeps next start running after you close SSH.
  3. Next serves the compiled app from .next.
  4. Next upgrades: HTTPS, an ALB, CloudFront, or the Express custom server variant.

Edge Cases & Pitfalls

Most failures are a missing build, wrong working directory, or an over-eager PM2 cluster.

build

Started without npm run build

next start needs a successful production compile. Build on EC2 before pm2 start.

cwd

Wrong directory for PM2

Run pm2 start pm2.config.js from the project root so npm start finds package.json.

cluster

Too many PM2 workers

instances: max on a tiny instance can thrash. Keep instances: 1 until you measure spare capacity.

npm

Uploaded laptop node_modules

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

sg

Security group blocks HTTP

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

reboot

App gone after instance reboot

Use pm2 startup and pm2 save so PM2 restores the process after a reboot.

Key Takeaways

  • Path: next build then npm start via PM2—the stock Next production flow.
  • Install on EC2: skip uploading node_modules; rebuild .next on the server.
  • PM2: one fork worker first; add cluster workers only after load testing.
  • Express optional: use the Express guide only when you need a custom server.

One line: build on EC2, pm2 start runs next start, keep one process until you need more.

Frequently Asked Questions

After next build, npm start runs next start, which serves your production build. It is the standard way to run Next.js without a custom Express server.
PM2 keeps the Node process alive after you disconnect SSH, can restart on failure, and integrates with OS startup scripts so the app comes back after reboot.
Start with a single forked process. Running many clustered Node workers on a small VM can exhaust CPU and RAM; scale out or tune clustering after load testing.
Usually no. Upload source and lockfiles, then run npm install on the server so binaries match the instance architecture.
Only when you need a custom Node HTTP layer in the same process. Otherwise prefer next start. See the Next.js + Express on EC2 guide for that pattern.
Yes. next start serves the production output in .next. Without a successful build, the production server will not work correctly.

Need a custom HTTP layer?

When middleware must run in Node before Next handles the request, use the Express + Next pattern.

Next.js + Express 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