To deploy Next.js on a Hostinger VPS, you build the app in standalone mode, run it with PM2 as a non-root user, and put Nginx in front for HTTPS and static files. This guide takes a fresh Ubuntu 24.04 server to a live HTTPS site with Namecheap DNS and CDN cache rules. You pay a flat monthly price, and because the Node process stays running there are no serverless cold starts.
Quick answer: Build with output: 'standalone', copy public and .next/static into .next/standalone, and run its server.js under PM2 on 127.0.0.1:3000. Nginx proxies your domain to it and Certbot adds HTTPS.
- Install Node.js 24 LTS, Nginx and PM2, and create a non-root
deployuser. - Add
output: 'standalone'tonext.config.js, build, and copy the static folders. - Start the app from a PM2 ecosystem file, then run
pm2 saveandpm2 startup. - Create the Nginx reverse proxy and run
sudo nginx -tbefore reloading. - Point the
@andwwwA records at the VPS in Namecheap, then runsudo certbot --nginx.
In this guide
- Before You Start
- 1. Server Prep and Node.js Installation
- 2. Prepare Next.js for Standalone Deployment
- 3. Manage the Process with PM2
- 4. Configure Nginx Reverse Proxy
- 5. Point Namecheap DNS to Your VPS
- 6. Generate Free Let’s Encrypt SSL Certificates
- 7. Add CDN Caching and Avoid Redirect Loops
- 8. Common Problems and Fixes
- Frequently Asked Questions
- Next Steps

Before You Start
- Server: a Hostinger VPS running Ubuntu 24.04, its public IP, and root SSH access (password or key).
- Domain: a domain at Namecheap using Namecheap BasicDNS. You can add the DNS records from Step 5 first, so they are live by the time you request the certificate.
- Code: your Next.js app in a Git repository the server can clone.
- Memory:
next buildis memory-hungry. On a 1–2 GB plan, create the swap file from the Common Problems section before you build. - Time: about 45–60 minutes.
- Live site warning: if the domain already serves a site elsewhere, changing its A records moves visitors to the VPS. Finish and test Steps 1–4 before you touch DNS.
1. Server Prep and Node.js Installation
Start with a fresh Ubuntu 24.04 LTS instance. SSH in as root and run the commands below. They update the base packages and add NodeSource’s Node 24 LTS repository (Node.js 20 is end-of-life). They also create a non-root deploy user so the app never runs as root:
apt update && apt upgrade -y
apt install -y curl git ufw nginx build-essential
# The NodeSource script adds an apt repository as root: download it and read it before running it
curl -fsSL https://deb.nodesource.com/setup_24.x -o nodesource_setup.sh
bash nodesource_setup.sh
apt install -y nodejs
node -v
npm -v
# Create the user that will own and run the app
adduser deploy
usermod -aG sudo deploy
rsync --archive --chown=deploy:deploy ~/.ssh /home/deployVerify that Node is at v24.x.x and npm is ready. Then install PM2 globally so it can manage your processes in the background:
npm install -g pm2Set up the firewall right away so no other ports are exposed. SSH is allowed before the firewall is switched on, so your session stays connected. Answer y when ufw enable warns about existing SSH connections:
ufw allow OpenSSH
ufw allow 'Nginx Full'
ufw enableRun ufw status to check: it should list OpenSSH and Nginx Full as allowed. From here on, work as the new user (ssh deploy@YOUR_SERVER_IP); commands that need root are prefixed with sudo.
2. Prepare Next.js for Standalone Deployment
A default Next.js production setup needs the whole node_modules folder on the server. Standalone mode makes Next.js trace which files the server really imports and copy only those into a small, self-contained folder.
In your project, add output: 'standalone' to next.config.js (in a next.config.mjs, end the file with export default nextConfig; instead of module.exports):
/** @type {import('next').NextConfig} */
const nextConfig = {
output: 'standalone',
poweredByHeader: false,
compress: true,
};
module.exports = nextConfig;On the server, logged in as deploy, clone your repository into /var/www/my-next-app and build it. If the build stops with just Killed, the server ran out of memory; add swap (see Common Problems) and run it again:
sudo mkdir -p /var/www/my-next-app
sudo chown -R $USER:$USER /var/www/my-next-app
cd /var/www/my-next-app
git clone https://github.com/your-username/your-repo.git .
npm install
npm run buildHere is the catch with standalone mode: Next.js outputs a self-contained runtime at .next/standalone/server.js, but it leaves out the public directory and the .next/static build assets.
Copy those folders into the standalone folder after every build (the Common Problems section shows how to make the build script do it for you):
cp -r public .next/standalone/
cp -r .next/static .next/standalone/.next/3. Manage the Process with PM2
Don’t run npm start directly in an SSH terminal; it stops the moment your session closes. The PM2 process manager keeps the app running in the background, restarts it if it crashes or grows past a memory limit, and reloads it without downtime.
Create an ecosystem.config.js file in your project root at /var/www/my-next-app:
module.exports = {
apps: [
{
name: 'nextjs-app',
script: '.next/standalone/server.js',
instances: 'max',
exec_mode: 'cluster',
env: {
PORT: 3000,
HOSTNAME: '127.0.0.1',
NODE_ENV: 'production',
},
max_memory_restart: '800M',
},
],
};Start your application cluster and make sure it automatically restarts after server reboots:
pm2 start ecosystem.config.js
pm2 save
pm2 startup systemdRun the generated sudo env PATH=... startup command that PM2 prints out to register the systemd service.
Check that it is running: pm2 status should show nextjs-app as online (one row per CPU core), and curl -I http://127.0.0.1:3000 on the server should return HTTP/1.1 200 OK. If a row says errored, read the reason with pm2 logs nextjs-app.
4. Configure Nginx Reverse Proxy
Next.js is now listening internally on 127.0.0.1:3000. That is what the HOSTNAME variable in the ecosystem file is for; without it the standalone server binds to all interfaces. Nginx goes in front to handle HTTPS, compression and serving static files straight from disk.
Create an Nginx configuration file at /etc/nginx/sites-available/nextjs.conf:
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
# Proxy Next.js standalone server
location / {
proxy_pass http://127.0.0.1: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;
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;
}
# Direct static file serving for faster cache responses
location /_next/static/ {
alias /var/www/my-next-app/.next/static/;
access_log off;
add_header Cache-Control "public, max-age=31536000, immutable";
}
}Enable the site config and test Nginx syntax before reloading. Only reload once nginx -t reports syntax is ok and test is successful:
sudo ln -s /etc/nginx/sites-available/nextjs.conf /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginxTo test before DNS is ready, run curl -I -H "Host: example.com" http://127.0.0.1 on the server; you should get the same 200 OK as from port 3000. To serve the app from a subdomain such as app.example.com instead, see how to point a Namecheap subdomain to a server with Nginx.
5. Point Namecheap DNS to Your VPS
Head to Namecheap and open Domain List > Manage > Advanced DNS. Add two A records targeting your Hostinger VPS public IP address:
- Type: A Record | Host:
@| Value:YOUR_SERVER_IP| TTL: Automatic - Type: A Record | Host:
www| Value:YOUR_SERVER_IP| TTL: Automatic
If you are deciding how to structure your root records, take a look at our breakdown on configuring root domains in Namecheap DNS: A Record vs CNAME.
From your own computer, check that both names resolve. Each command should print your VPS IP; if it prints nothing or an old IP, wait a few minutes and try again:
dig +short example.com
dig +short www.example.com6. Generate Free Let’s Encrypt SSL Certificates
Once DNS points to your server, install Certbot to provision free SSL/TLS certificates through Nginx:
sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.comCertbot updates your nextjs.conf file, adds the HTTPS redirect, and relies on a systemd timer for renewals. Test the renewal with sudo certbot renew --dry-run, then open https://example.com; curl -I https://example.com should return a 200.
7. Add CDN Caching and Avoid Redirect Loops
Putting a CDN in front of your VPS shields the origin server from traffic spikes and delivers static assets from edge locations closest to your users. When configuring CDN edge rules, set your cache headers like this:
- Static assets (
/_next/static/*): Cache for 1 year (immutable content hashes). - Images (
/_next/image*): Cache for 7 days at the edge. - HTML / Server actions: Bypass cache entirely (pass-through to origin).
The most common bug here is an infinite redirect loop caused by the CDN requesting origin content over plain HTTP while enforcing HTTPS for the user. If you hit this issue, check our fix for SSL ERR_TOO_MANY_REDIRECTS on Nginx behind a CDN.
8. Common Problems and Fixes
502 Bad Gateway from Nginx
A 502 means Nginx is running but cannot reach the app on port 3000. Run pm2 status; if nextjs-app is not online, check pm2 logs nextjs-app. A common cause is a missing .next/standalone/server.js because the build failed or output: 'standalone' is not set.
Out of Memory Error During Build
Next.js builds are heavy on CPU and RAM. On a 1GB or 2GB VPS, running npm run build can trigger the Linux OOM killer, which ends the build with nothing but Killed in your terminal.
Fix this by setting up a 2GB swap file before running your builds:
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstabCSS, JS or Images Return 404 in Standalone Mode
If your CSS files return 404 errors after running standalone mode, you forgot to copy public/ and .next/static/ into .next/standalone. Automate this inside your package.json build script so it runs automatically every deploy:
{
"scripts": {
"dev": "next dev",
"build": "next build && cp -r public .next/standalone/ && cp -r .next/static .next/standalone/.next/",
"start": "node .next/standalone/server.js"
}
}Frequently Asked Questions
How much RAM do I need for Next.js on a VPS?
A 2-core / 4GB RAM VPS handles PM2 running 2 cluster workers easily under moderate traffic. If you are building directly on the server with 2GB of RAM or less, create a 2GB swap file so your build process does not run out of memory.
Can I deploy automated updates with GitHub Actions?
Yes. Set up a workflow that connects to your VPS over SSH, pulls the main branch, runs npm run build, and triggers pm2 reload nextjs-app for a zero-downtime deploy.
Why use PM2 instead of Docker?
PM2 runs directly on the host without the overhead of container runtimes. On entry-level VPS tiers, skipping the Docker daemon and image builds leaves more memory for the app itself. Docker is great, but PM2 is lightweight and quick to debug.
Do I need Nginx if Next.js can serve requests itself?
For production, yes. Nginx handles HTTPS with Certbot, serves /_next/static from disk with long cache headers, and lets the Node process listen only on 127.0.0.1, so it is never exposed directly. Next.js on its own would need extra setup to bind to ports 80 and 443 and manage certificates.
Next Steps
Your app is live behind Nginx with HTTPS. Two things are worth doing next: add the build-script change above so every deploy copies the static folders, and stop PM2’s log files from growing without limit. Our guide to configuring PM2 startup scripts and log rotation on a VPS covers both the boot service and log rotation.

