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.
Node.js and npm on the instance, and Node locally for create-next-app.
Walkthrough
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.
Browser
How It Works
create-next-app generates a ready project with scripts and dependencies.
npm run dev starts the default Next dev server on port 3000.
You customize pages first so the later EC2 deploy shows your content.
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
next({ dev }) creates the Next app; production uses NODE_ENV=production.
app.prepare() loads the build before Express accepts traffic.
getRequestHandler() lets Express pass every path to Next.
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.
Confirm the instance is running; note the public DNS or Elastic IP.
SSH with PuTTY or ssh, then create the deploy directory.
Upload with FileZilla. Exclude .git, node_modules, and usually .next.
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.