Skip to content
categoryDeployment

How to Deploy Next.js to Hostinger VPS - Complete Guide 2025

Ayyaz Zafar5 min read

Deploy your Next.js app to Hostinger VPS with PM2, Nginx reverse proxy, and free SSL certificate. Complete guide with all commands included.

You built a Next.js app. It runs perfectly on localhost. Now you need to put it on the internet.

But here's where most people get stuck: Next.js isn't a drag-and-drop deployment. It needs a real server, and if you get one step wrong, you're staring at a 502 error.

In this guide, I'll show you exactly how to deploy your Next.js app to Hostinger VPS — step by step, no steps skipped. By the end, your app will be:

  • Running on your own VPS
  • Secured with HTTPS
  • Auto-restarting with PM2
  • Ready for real users

Video Tutorial

Prefer watching? Here's the complete video walkthrough:

What You'll Need

Before we start, make sure you have:

  1. A Next.js app ready to deploy (any Next.js app works)
  2. A Hostinger VPS plan (I'll show you which one)
  3. A domain name (can be from Hostinger or anywhere)
  4. Basic terminal knowledge (we'll use SSH)
  5. About 30 minutes of focused time

Step 1: Get a Hostinger VPS

Go to hostinger.com and look for VPS Hosting.

For a Next.js app, the KVM1 plan is plenty to start:

  • 1 core
  • 4 GB RAM
  • 50 GB disk space
  • 4 TB bandwidth

You can scale up later without losing your setup.

During setup:

  1. Select Ubuntu 25.10 as your operating system
  2. Create a strong root password (save it somewhere safe)
  3. Optionally add an SSH key for passwordless login

Step 2: Set Up Your Domain

While your VPS is being provisioned, set up your domain:

  1. Go to your domain registrar (Namecheap, GoDaddy, etc.)
  2. Add an A record pointing to your VPS IP address:
    • Type: A
    • Name: @ (or your subdomain like app)
    • Value: Your VPS IP address
    • TTL: Auto

If you want www to work too, add another A record with Name: www

Pro tip: Use dnschecker.org to verify DNS propagation.

Step 3: Connect to Your VPS via SSH

Open your terminal and connect:

Terminal
ssh root@YOUR_VPS_IP

First time connecting? Type yes when asked about the fingerprint.

If you set up an SSH key with a custom path:

Terminal
ssh -i ~/.ssh/your_key_name root@YOUR_VPS_IP

Pro tip: If you get "connection refused", wait a minute — the server might still be starting up.

Step 4: Install Node.js

Your VPS is a blank Ubuntu server. Install Node.js using NVM:

Terminal
# Install NVM and Node.js# Get the latest commands from: https://nodejs.org/en/downloadcurl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bashsource ~/.bashrcnvm install --lts

Verify the installation:

Terminal
node --versionnpm --version

You should see version numbers like v24.x.x for Node.

Step 5: Install PM2

PM2 is a process manager that keeps your Next.js app running even after you close the terminal — and restarts it if it crashes.

Terminal
npm install -g pm2

Verify:

Terminal
pm2 --version

Step 6: Set Up SSH Key for GitHub (Private Repos)

If your repo is private, you need to authenticate. SSH keys are the cleanest way.

Generate a new SSH key on your VPS:

Terminal
ssh-keygen -t ed25519 -C "your-email@example.com"

Press Enter to accept defaults. Then copy your public key:

Terminal
cat ~/.ssh/id_ed25519.pub

Copy the entire output, then:

  1. Go to your GitHub repository
  2. Click Settings → Deploy Keys → Add deploy key
  3. Paste your key and give it a name
  4. Click Add key

Test the connection:

Terminal
ssh -T git@github.com

You should see: "Hi username/repo! You've successfully authenticated..."

Step 7: Clone Your Next.js App

Create the web directory and clone your repo:

Terminal
mkdir -p /var/wwwcd /var/wwwgit clone git@github.com:YOUR_USERNAME/YOUR_REPO.gitcd YOUR_REPO

Note: The /var/www folder doesn't exist on fresh servers — that's normal, we just create it.

If your repo is public, use HTTPS instead:

Terminal
git clone https://github.com/YOUR_USERNAME/YOUR_REPO.git

Step 8: Install Dependencies and Build

Terminal
pnpm install   # or npm installpnpm run build # or npm run build

The build step is important — Next.js needs to compile your app for production.

If build fails:

  • Check your Node version matches what you use locally
  • Make sure all environment variables are set

Setting Environment Variables

If your app uses environment variables, create a .env.local file:

Terminal
nano .env.local

Add your variables:

Text
DATABASE_URL=your_database_urlNEXT_PUBLIC_API_KEY=your_api_key

Save with Ctrl+O, Enter, then Ctrl+X.

Important: After adding env variables, rebuild:

Terminal
pnpm run build

Step 9: Start with PM2

Start your app:

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

Why PM2 instead of just npm start? Because if your SSH session closes, your app dies. PM2 keeps it running in the background forever.

Check status:

Terminal
pm2 status

You should see your app with status "online".

Make PM2 start on server reboot:

Terminal
pm2 startuppm2 save

Your app is now running on port 3000, but we can't access it from the browser yet — we need Nginx.

Step 10: Install and Configure Nginx

Nginx will receive traffic on port 80/443 and forward it to your Next.js app on port 3000.

Terminal
sudo apt install nginx -y

Create a config file:

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

Paste this configuration:

Text
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_cache_bypass $http_upgrade;    }}

Replace yourdomain.com with your actual domain.

Enable the site:

Terminal
sudo ln -s /etc/nginx/sites-available/nextjs /etc/nginx/sites-enabled/

Important: Remove the default Nginx site:

Terminal
sudo rm /etc/nginx/sites-enabled/default

Test and restart:

Terminal
sudo nginx -tsudo systemctl restart nginx

At this point, visiting your server's IP should show your Next.js app!

Step 11: Add SSL (HTTPS) with Let's Encrypt

Free SSL with Certbot:

Terminal
sudo apt install certbot python3-certbot-nginx -ysudo certbot --nginx -d yourdomain.com -d www.yourdomain.com

Follow the prompts — enter your email and agree to terms.

Certbot automatically:

  • Obtains your certificate
  • Configures Nginx for HTTPS
  • Sets up auto-renewal

Your App is Live!

Open your browser and go to your domain. You should see your Next.js app running with HTTPS!

Your app is now:

  • ✅ Running on your own VPS
  • ✅ Secured with HTTPS
  • ✅ Auto-restarting with PM2
  • ✅ Ready for real users

Updating Your App

When you make changes and push to GitHub:

Terminal
cd /var/www/YOUR_REPOgit pullpnpm run build   # or npm run buildpm2 restart nextjs-app

Quick Reference Checklist

  1. Get Hostinger VPS, connect via SSH
  2. Install Node.js and PM2
  3. Set up SSH key for GitHub (if private repo)
  4. Clone your repo, install dependencies, build
  5. Start with PM2
  6. Install Nginx as reverse proxy
  7. Point domain, add SSL

Bookmark this guide. Next time you deploy, it'll take 10 minutes.

Common Issues

Nginx shows default page instead of Next.js

Remove the default site: sudo rm /etc/nginx/sites-enabled/default

"Connection refused" when SSHing

Wait a minute — the server might still be starting up.

Build fails

  • Check Node version matches local
  • Verify environment variables are set

SSL certificate fails

  • Make sure domain is pointing to server first (check with dnschecker.org)
  • Ensure port 80 is open

What's Next?

Want automatic deployments where every git push updates your live site? That's CI/CD with GitHub Actions — coming in the next tutorial!


Have questions? Drop a comment on the video — I read every single one.

More in Deployment

All 2 →

Get new tutorials by email

One email when something new is published. Unsubscribe anytime.

Written by Ayyaz Zafar, full-stack developer for 15+ years. Work with me