EN · $ USD
Developer and Application Servers

How to Self-Host a Next.js App on Your Own Server

7 min read Updated: Sunucu Atlas technical team

In short

The cleanest way to self-host Next.js is to build with output: 'standalone' in your next.config file and run the generated server.js. With Coolify you get automatic deploys and SSL using a Dockerfile or Nixpacks; with PM2 + Nginx you set up process management and static file caching yourself. Either way, preserve the .next/cache directory for ISR caching and image optimization.

In this guide
  1. Which method is right for you?
  2. What is a standalone build?
  3. Method 1: Deploying with Coolify
  4. Method 2: Deploying with PM2 + Nginx
  5. How do ISR and caching work on your own server?
  6. Image optimization settings
  7. How many resources do you need?
  8. Next step: choose your infrastructure

The cleanest way to deploy a Next.js app on a VPS or VDS is to build it with output: 'standalone' in your next.config file and run the small Node.js server it produces (server.js). You can do this two ways: as a container with Coolify (automatic deploys and SSL when you push to Git) or directly on the operating system with PM2 + Nginx (full control, more manual setup). Below we set up both methods step by step, then cover the things that behave differently on your own server, such as ISR caching and image optimization.

Which method is right for you?

CriteriaCoolify (Docker)PM2 + NginxStatic export
SetupConnect a repo in the panelNode.js, PM2, Nginx, Certbot by handAny web server
DeployAutomatic on git pushVia script or CICopy files
SSLAutomaticWith CertbotDepends on the server
RollbackRevert to a previous deploy in the panelManually, with release foldersPut the old files back
SSR, ISR, API RoutesSupportedSupportedNot supported
Best forTeams that want fast, repeatable deploysPeople who want to manage the server themselvesFully static sites

If your app does not use server-side rendering, API routes or ISR, you can generate static HTML with output: 'export' and upload it to any hosting. This guide focuses on full Next.js apps that need a Node.js server.

What is a standalone build?

Standalone output means Next.js traces the files and dependencies your app actually uses during the build and copies them into the .next/standalone folder. Instead of the entire node_modules, this folder contains only what is needed, which noticeably shrinks your Docker image and the footprint on the server.

// next.config.js
/** @type {import('next').NextConfig} */
const nextConfig = {
  output: 'standalone',
};

module.exports = nextConfig;

An important detail: the public folder and the .next/static directory are not copied into the standalone folder automatically. If you do not move them yourself, the page loads but CSS, JavaScript and images return 404.

npm ci
npm run build
cp -r public .next/standalone/
cp -r .next/static .next/standalone/.next/
PORT=3000 HOSTNAME=127.0.0.1 node .next/standalone/server.js

Current Next.js versions expect Node.js 20 LTS or later; choose the Node.js version you install on the server based on the engines field in your project's package.json.

Method 1: Deploying with Coolify

Coolify is an open-source deploy panel that runs on your own server; it pulls your Git repository, builds the image, connects it to your domain through the Traefik proxy and gets SSL automatically. For setup details, see the automatic deploys from GitHub with Coolify guide.

  1. Add a new resource in Coolify and select your repository.
  2. Pick Nixpacks (auto-detection) as the build pack, or Dockerfile for more predictable results.
  3. Set the Ports Exposes field to 3000.
  4. Enter https://ornek.com in the Domains field; the DNS A record must point to the server.
  5. Mark variables that start with NEXT_PUBLIC_ as available at build time; these values are baked into the code during the build.
  6. Deploy and watch the logs.

If you use a Dockerfile, the multi-stage setup below produces a small, secure image:

FROM node:22-alpine AS deps
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci

FROM node:22-alpine AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN npm run build

FROM node:22-alpine AS runner
WORKDIR /app
ENV NODE_ENV=production
ENV PORT=3000
ENV HOSTNAME=0.0.0.0
RUN addgroup -S nodejs && adduser -S nextjs -G nodejs
COPY --from=builder /app/public ./public
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
USER nextjs
EXPOSE 3000
CMD ["node", "server.js"]

Inside a container, HOSTNAME=0.0.0.0 is required; otherwise the server is reachable only from inside the container itself and the proxy returns a 502. If your project has no public folder, delete the corresponding COPY line.

Method 2: Deploying with PM2 + Nginx

With this method, Next.js runs directly on the operating system. PM2 keeps the process alive and starts it automatically when the server reboots; Nginx handles the reverse proxy, SSL and static file serving. Before opening the server to traffic, complete the first security steps on a new Ubuntu server.

  1. Install Node.js and PM2:
    curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -
    sudo apt install -y nodejs nginx
    sudo npm install -g pm2
  2. Put the project under /var/www/nextjs and run the standalone build and copy commands above.
  3. Create ecosystem.config.js in the project root:
    module.exports = {
      apps: [{
        name: 'nextjs',
        script: '.next/standalone/server.js',
        instances: 1,
        max_memory_restart: '1G',
        env: {
          NODE_ENV: 'production',
          PORT: 3000,
          HOSTNAME: '127.0.0.1'
        }
      }]
    };
  4. Start the process and make it run on boot:
    pm2 start ecosystem.config.js
    pm2 save
    pm2 startup
    pm2 startup prints a command; run that command with sudo.
  5. Define the Nginx server block (/etc/nginx/sites-available/nextjs):
    server {
        listen 80;
        server_name ornek.com www.ornek.com;
    
        location /_next/static/ {
            alias /var/www/nextjs/.next/static/;
            add_header Cache-Control "public, max-age=31536000, immutable";
        }
    
        location / {
            proxy_pass http://127.0.0.1:3000;
            proxy_http_version 1.1;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }
    }
  6. Enable the site and get SSL:
    sudo ln -s /etc/nginx/sites-available/nextjs /etc/nginx/sites-enabled/
    sudo nginx -t && sudo systemctl reload nginx
    sudo apt install -y certbot python3-certbot-nginx
    sudo certbot --nginx -d ornek.com -d www.ornek.com

Because file names under /_next/static/ change with a content hash on every build, a one-year immutable cache is safe and frees the Node.js process from serving static files.

When updating, building in the live folder can break the running process's files until the build finishes. A safer approach is to build each release in its own folder, switch a symlink to the new folder, then run pm2 reload nextjs.

How do ISR and caching work on your own server?

ISR (Incremental Static Regeneration) regenerates statically generated pages in the background at set intervals or on demand. On your own server, these pages and the fetch cache are stored on the server's disk under .next/cache by default. That has three practical consequences:

  • A single instance just works: If you run a single Next.js process, ISR works with no extra configuration.
  • Multiple instances have separate caches: If you run PM2 cluster mode or several containers, each instance keeps its own cache, and a revalidatePath or revalidateTag call updates only the instance that received the request. In that case, use a shared cache such as Redis via the cacheHandler setting, or stay on a single instance.
  • The cache resets when the container is recreated: In Coolify, every deploy means a new container, so ISR pages are regenerated on the first request. This is usually fine, but mounting a persistent volume on the .next/cache directory helps keep the image cache.

Image optimization settings

The next/image component resizes images on request through the /_next/image endpoint and converts them to WebP/AVIF. On Vercel, the platform's infrastructure does this work; on your own server, your CPU does. Current versions use the sharp library for this automatically; if you are on an older version, add it with npm install sharp.

// next.config.js
const nextConfig = {
  output: 'standalone',
  images: {
    formats: ['image/webp'],
    minimumCacheTTL: 2678400,
    remotePatterns: [
      { protocol: 'https', hostname: 'cdn.ornek.com' }
    ]
  }
};
  • formats: AVIF produces smaller files but takes more CPU to encode. On a limited CPU, starting with WebP only makes sense.
  • minimumCacheTTL: How long, in seconds, an optimized image stays in the cache; the value above is 31 days. Keeping it long prevents the same image from being processed over and over.
  • remotePatterns: Only images from domains you allow get processed; this stops your server from being abused to transform other people's images.

If your images are already optimized before the build, or a CDN handles that job, you can turn off server-side processing completely with images.unoptimized: true.

How many resources do you need?

Next.js uses the most memory not while running but during next build. As a guideline: a mid-sized project usually uses a few hundred MB of RAM at runtime, but the build can easily go past 2 GB. On a server with 2 GB of RAM, the build may stop with "JavaScript heap out of memory" or "Killed"; in that case add swap or move up to 4 GB of RAM or more. Detailed fixes for build errors are in the Coolify deploy errors article.

Next step: choose your infrastructure

If you want automatic deploys on git push, automatic SSL and rollbacks from a panel, Sunucu Atlas AI Hosting with the Coolify panel preinstalled is set up for exactly this; for a single Next.js app, SA-AI-1 with 4 GB of RAM is a good starting point. If you prefer to manage the server yourself with PM2 and Nginx, choose a plan with at least 4 GB of RAM from the Ryzen Premium VDS line, with AMD Ryzen 9 5900X CPUs, KVM virtualization and root access. With either option your server is in Istanbul, and if you get stuck during setup you can open a support ticket.

Frequently asked questions

Can I deploy a Next.js app without Vercel?

Yes. An app built with next build runs as a regular Node.js server. Build it with output: 'standalone' and run the server.js file with Coolify, Docker or PM2.

Why do CSS and images return 404 after a standalone build?

The public folder and the .next/static directory are not copied into the standalone folder automatically. You need to copy them to .next/standalone/public and .next/standalone/.next/static.

Does ISR work on my own server?

Yes. When you run a single Next.js process, no extra configuration is needed. If you run multiple instances, use a shared cache such as Redis via cacheHandler.

How much RAM does a VPS need for Next.js?

As a guideline, 4 GB of RAM is a safe starting point. Runtime usage is usually much lower, but the next build step can go past 2 GB and cause out-of-memory errors on small servers.

Should I use PM2 cluster mode?

If you use ISR or a disk-based cache, do not enable cluster mode without setting up a shared cache; instances may serve different content. A single instance is enough for most sites.

For this guide

Recommended plans

If you want to deploy Next.js automatically on git push, choose Coolify-ready AI Hosting; if you would rather manage the server yourself with PM2 and Nginx, Ryzen Premium VDS with root access has you covered.

SA-AI-1

Coolify
Recommended

4 GB RAM

$2.75/mo

+VAT

  • Processor2 CPU
  • Disk40 GB NVMe
  • LocationIstanbul
  • 1G Port
  • Coolify Panel Preinstalled
  • Deploy with GitHub and Docker
  • Automatic SSL Certificate

SA-AI-2

Coolify

8 GB RAM

$4.32/mo

+VAT

  • Processor4 CPU
  • Disk80 GB NVMe
  • LocationIstanbul
  • 1G Port
  • Coolify Panel Preinstalled
  • Deploy with GitHub and Docker
  • Automatic SSL Certificate

SA-AI-3

Coolify

12 GB RAM

$6.29/mo

+VAT

  • Processor6 CPU
  • Disk120 GB NVMe
  • LocationIstanbul
  • 1G Port
  • Coolify Panel Preinstalled
  • Deploy with GitHub and Docker
  • Automatic SSL Certificate

AI Hosting — tüm paketler Ryzen Premium VDS — tüm paketler

Need help with setup?

Send questions about your service in a support ticket, and our technical team will walk you through it step by step.