C.W.K.
Stream
Lesson 02 of 08 · published

Self-Hosting

~22 min · self-host, next start, PM2

Level 0Curious
0 XP0/68 lessons0/11 achievements
0/120 XP to next level120 XP to go0% complete

Every framework feature works self-hosted

Server Components, RSC streaming, ISR, Server Actions, image/font optimization — all run on a vanilla Node.js server via next start. The pieces that change: you operate the SSL, the process manager, the CDN.

Production server

npm run build then npm run start. Default port 3000; override with -p. The server is single-process by default; use a process manager (PM2, systemd, launchd, Docker orchestrator) to keep it alive and scale.

Reverse proxy

Put nginx or Caddy in front of next start for TLS termination, request logging, and gzip/brotli. Caddy is the simplest because TLS is automatic.

Image optimization needs sharp

Self-hosted, the optimizer uses sharp internally. Install it explicitly so production doesn't fall back to unoptimized images.

Code

Build and run·bash
npm run build
npm install sharp                  # for next/image optimization
NODE_ENV=production npm run start  # default port 3000

# Custom port
NODE_ENV=production npm run start -- -p 8080
PM2 keeps it alive·bash
npm install -g pm2
pm2 start npm --name nextjs -- start
pm2 save
pm2 startup       # generate launchd / systemd integration

pm2 status
pm2 logs nextjs
pm2 reload nextjs # zero-downtime restart
Caddy reverse proxy·text
# Caddyfile
myapp.com {
  reverse_proxy localhost:3000
}
# Caddy handles HTTPS automatically via Let's Encrypt

External links

Exercise

Self-host a Next.js app on a Mac or Linux box behind Caddy. Verify HTTPS works automatically and that PM2 restarts the process if you kill it.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.