Skip to main content
A Next.js website is built as a standalone server and kept running by PM2. nginx proxies the domain to 127.0.0.1:{port}, including WebSocket upgrades.

Prepare your app

Add standalone output to next.config.js (or .ts/.mjs):
Without it, .next/standalone/server.js does not exist and PM2 has nothing to start.

Deploy steps

1

Fetch the code

git clone, or git fetch and reset to the branch.
2

Install

The package manager comes from the lockfile: pnpm-lock.yaml → pnpm, yarn.lock → Yarn (both through Corepack), otherwise npm ci.
3

Build

npm run build (or the pnpm/Yarn equivalent) with the site’s Node version.
4

Copy the standalone extras

public/, .next/static/, and .env are copied into .next/standalone/, as Next.js requires.
5

Start

pm2 startOrReload ecosystem.config.cjs, then nginx is reloaded.

The PM2 file

Edit it on the Processes tab. Saving the environment before the first deploy is fine: PM2 starts after the first successful build.