Skip to content
zefıly
← All posts

Self-hosting Next.js on your own server, without Vercel

5 min read

Next.js runs on any server with Node.js. How to build the standalone output, the two folders it does not copy for you, how to run it as a service behind nginx, and what to know about caching and streaming. Tested with Next.js 16.

Next.js does not need Vercel. next build makes a Node.js server that runs anywhere, and all features work on it: server components, server actions, image optimisation, incremental revalidation. What you take over is the part a platform does for you: the process, the proxy, HTTPS and deployments.

We tested the steps here on 12 November 2026 with a new project from create-next-app, which installed Next.js 16.4.6, on Node.js 24.

In short

  • Set output: "standalone" and deploy the .next/standalone folder: 39 MB in our test, against 366 MB of node_modules.
  • The standalone folder does not contain public/ and .next/static/. Copy them in, or the site has no styles and images.
  • Start it with node server.js, with PORT and HOSTNAME set.
  • Run it as a systemd service on 127.0.0.1 and put nginx in front for HTTPS.
  • One server: caching just works. Several servers: you need a shared cache handler.

1. Build the standalone output

In next.config.ts:

const nextConfig: NextConfig = {
  output: "standalone",
};
npm ci
npm run build

Next.js traces which files the server really needs and writes them to .next/standalone:

node_modules  package.json  server.js

In our test this folder was 39 MB. The project's full node_modules was 366 MB. You deploy the small one and never run npm install on the server.

2. Copy the two missing folders

This is the step everybody misses once. The standalone folder leaves out static files, because Next.js expects a CDN to serve them. On your own server, copy them in:

cp -r public .next/standalone/
cp -r .next/static .next/standalone/.next/

Without this the pages load as plain HTML: no CSS, no JavaScript, no images.

3. Start it

cd .next/standalone
PORT=3000 HOSTNAME=127.0.0.1 node server.js
▲ Next.js 16.4.6
- Local:         http://127.0.0.1:3000
✓ Ready in 0ms

HOSTNAME=127.0.0.1 makes the server listen only locally. The web server in front is the only way in.

A static page answered in our test with:

x-nextjs-cache: HIT
Cache-Control: s-maxage=31536000

4. Run it as a service

Do not start it in a terminal session. A systemd unit restarts it after a crash or a reboot. /etc/systemd/system/myapp.service:

[Unit]
Description=My Next.js app
After=network.target

[Service]
User=myapp
WorkingDirectory=/home/myapp/app/.next/standalone
Environment=NODE_ENV=production
Environment=PORT=3000
Environment=HOSTNAME=127.0.0.1
EnvironmentFile=/home/myapp/app/.env
ExecStart=/usr/bin/node server.js
Restart=always

[Install]
WantedBy=multi-user.target
sudo systemctl enable --now myapp

Run it as its own Linux user, not as root. Next.js handles SIGTERM itself: it stops taking new connections and finishes the running requests, so a restart does not cut visitors off.

5. nginx in front

server {
    server_name example.com;

    location /_next/static/ {
        alias /home/myapp/app/.next/standalone/.next/static/;
        expires 1y;
        add_header Cache-Control "public, immutable";
    }

    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_buffering off;
    }
}

Two details:

  • Static files from nginx. Files under /_next/static/ have a hash in their name and never change. nginx serves them faster than Node.js, and the long cache time is safe.
  • proxy_buffering off. Next.js streams pages: the shell first, slow parts later. A proxy that buffers the whole response removes that benefit. The Next.js documentation suggests the same effect with an X-Accel-Buffering: no header from the app.

6. Environment variables

  • Variables that start with NEXT_PUBLIC_ are written into the JavaScript at build time. Changing them needs a new build.
  • All others are read by the server at run time. Changing them needs only a restart.

So build with the public values of the environment you deploy to.

7. Caching and more than one server

Next.js keeps its cache (rendered pages, fetched data) in memory and on disk in .next/cache. With one server, this works without any setup.

With several servers behind a load balancer, each has its own cache, and a revalidation on one does not reach the others. For that, Next.js lets you set a cacheHandler in next.config that stores the cache in a shared place, such as Redis. Do not start there. One server carries a lot.

8. Deployments

A simple, safe order:

  1. Build in a new folder (releases/2026-11-13-1), not in the running one.
  2. Copy public and .next/static into the standalone folder.
  3. Point a current symlink to the new release.
  4. systemctl restart myapp.
  5. If the app does not answer, point the symlink back and restart.

Build on the server only if it has memory to spare: next build needs much more than running the app does. On a small VPS, build in CI and upload the standalone folder.

With Zefily

Zefily has a Next.js + PostgreSQL starter stack: nginx as the front proxy, Node.js 24 and PostgreSQL. You connect a Git repository; each deployment builds a new release, starts it as a managed systemd service on a local port under the site's own user, and switches back to the previous release if the app does not answer. Other Node apps (Nuxt, SvelteKit, Astro, Express) run the same way. See Node.js and Python apps and Deploy from GitHub.

Frequently asked questions

Can I host Next.js without Vercel?

Yes. next build produces a Node.js server that runs on any machine with Node.js. Server components, server actions, image optimisation and revalidation all work when self-hosted.

What does output standalone do in Next.js?

It writes a .next/standalone folder with server.js and only the node_modules files the app needs. In our test it was 39 MB instead of 366 MB.

Why does my self-hosted Next.js site have no CSS?

The standalone folder does not include public/ and .next/static/. Copy both into .next/standalone before starting server.js.

How do I change the port of a standalone Next.js server?

Set the PORT environment variable, and HOSTNAME for the address to listen on, before running node server.js.

Do I need Redis to self-host Next.js?

Not with one server; the built-in cache is enough. A shared cache handler is only needed when several servers run the same app.

See App stacks for the starter stacks Zefily installs.

More posts

Black Friday & Cyber Monday

Before you go: lifetime plans are on sale

Pay once for a lifetime plan, or get 30% off the first year of any yearly plan.

Only 100 lifetime plans per plan. When they are gone, they are gone.

See the campaign

Amazing November

See the campaign →