← Back to list

How to Deploy a Next.js Application on a VPS + Nginx

Using Nginx, PM2, and npm (Production Guide)

Amir · 2026-01-07 16:47 · 0 claps · 1.8 min read
#nextjs #nodejs #vps #nginx #deployment
Open on Medium ↗
Wiki topics: 🌐 · Web Development

How to Deploy a Next.js Application on a VPS + Nginx

Using Nginx, PM2, and npm (Production Guide)

Deploying a Next.js application to your own VPS gives you full control, lower long-term costs, and zero platform lock-in.

This guide walks through a production-ready setup using:

  • npm for dependencies
  • PM2 for process management
  • Nginx as a reverse proxy
  • A Linux VPS (Ubuntu recommended)

No Docker. No platform magic. Just a clean, reliable deployment.

🧰 What You’ll Need

Before starting, make sure you have:

  • A VPS
  • Ubuntu 20.04+ installed
  • A domain name (optional but recommended)
  • SSH access with a non-root user

Tech Stack Overview

  • Next.js (production build)
  • Node.js (LTS)
  • npm
  • PM2 (process manager)
  • Nginx (reverse proxy + SSL)

1️⃣ Connect to Your VPS

ssh user@your_server_ip

Update the system:

sudo apt update && sudo apt upgrade -y

2️⃣ Install Node.js & npm

Install Node.js LTS (recommended via NodeSource):

curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt install nodejs -y

Verify installation:

node -v
npm -v

3️⃣ Upload Your Next.js App

Clone from GitHub

git clone https://github.com/yourusername/your-nextjs-app.git
cd your-nextjs-app

4️⃣ Install Dependencies & Build

Inside your project directory:

npm install
npm run build

This creates the optimized production build.

5️⃣ Start Next.js with PM2

Install PM2 globally:

sudo npm install -g pm2

Start your app:

pm2 start npm --name "nextjs-app" -- start

Check status:

pm2 status

Enable auto-restart on server reboot:

pm2 startup
pm2 save

6️⃣ Configure Nginx as a Reverse Proxy

Install Nginx:

sudo apt install nginx -y

Create a new config file:

sudo nano /etc/nginx/sites-available/nextjs-app

Paste the following:

server {
    listen 80;
    server_name yourdomain.com www.yourdomain.com;

    location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;

        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        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 X-Forwarded-Host $host;

        proxy_cache_bypass $http_upgrade;
    }
}

Enable the site:

sudo ln -s /etc/nginx/sites-available/nextjs-app /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx

7️⃣ Allow Firewall Access (Optional but Recommended)

sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw enable

8️⃣ Add HTTPS with Let’s Encrypt (Free SSL)

Install Certbot:

sudo apt install certbot python3-certbot-nginx -y

Generate SSL:

sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com

Auto-renew SSL:

sudo certbot renew --dry-run

✅ Final Result

Your Next.js app is now:

  • Running via PM2
  • Served through Nginx
  • Secured with HTTPS
  • Hosted on your own VPS

Visit:

https://yourdomain.com

메타데이터
post_id
01ce7461dddc
slug
how-to-deploy-a-next-js-application-on-a-vps-01ce7461dddc
url
https://medium.com/@amirjld/how-to-deploy-a-next-js-application-on-a-vps-01ce7461dddc
canonical_url
https://medium.com/@amirjld/how-to-deploy-a-next-js-application-on-a-vps-01ce7461dddc
author_url
https://medium.com/@amirjld
status
ok
fetched_at
2026-06-12 18:14:10