Next.js is the dominant React framework for production web applications — combining server-side rendering (SSR), static generation (SSG), and API routes in a single deployable unit. Running Next.js on a Hong Kong VPS with CN2 GIA routing instead of Vercel gives you full control over your infrastructure, no per-function or per-seat pricing, and sub-50ms first-byte response times to mainland Chinese users that Vercel’s US-based edge cannot match.
Why Self-Host Next.js Instead of Vercel?
- China accessibility — Vercel’s network delivers poor performance in mainland China; a Hong Kong VPS with CN2 GIA routing serves Chinese users at 30–60ms TTFB
- No Vercel pricing surprises — bandwidth and function invocation costs on Vercel scale unpredictably at volume; a flat-rate VPS is predictable
- Full environment control — run custom Node.js versions, install system dependencies, configure environment variables without Vercel’s constraints
- Persistent storage — write to the filesystem, run SQLite, or manage local media — impossible on Vercel’s stateless functions
Step 1: Install Node.js and PM2
apt update && apt upgrade -y
apt install -y nginx certbot python3-certbot-nginx git ufw
# Install Node.js 20 LTS via NodeSource
curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
apt install -y nodejs
# Verify
node --version # Should show v20.x
npm --version
# Install PM2 (production process manager for Node.js)
npm install -g pm2
# Configure PM2 to start on boot
pm2 startup systemd
# Run the generated command that PM2 outputs
Step 2: Deploy Your Next.js Application
useradd -m -s /bin/bash nodeapp
mkdir -p /var/www/nextapp
chown nodeapp:nodeapp /var/www/nextapp
su - nodeapp
cd /var/www/nextapp
git clone https://github.com/your-org/your-nextjs-app.git .
npm install --production=false # Need devDependencies for build
# Build the production bundle
npm run build
# Test it starts correctly
npm start
Create PM2 Ecosystem File
cat > /var/www/nextapp/ecosystem.config.js << 'EOF'
module.exports = {
apps: [{
name: 'nextapp',
script: 'node_modules/.bin/next',
args: 'start',
cwd: '/var/www/nextapp',
instances: 'max', // One process per CPU core
exec_mode: 'cluster', // Cluster mode for load balancing
watch: false,
max_memory_restart: '1G',
env_production: {
NODE_ENV: 'production',
PORT: 3000,
},
error_file: '/var/log/nextapp/error.log',
out_file: '/var/log/nextapp/out.log',
merge_logs: true,
time: true
}]
};
EOF
mkdir -p /var/log/nextapp
chown nodeapp:nodeapp /var/log/nextapp
# Start with PM2
pm2 start ecosystem.config.js --env production
pm2 save # Persist process list across reboots
Step 3: Configure Nginx as Reverse Proxy
exit # Back to root
cat > /etc/nginx/sites-available/nextapp << 'EOF'
upstream nextjs_upstream {
server 127.0.0.1:3000;
keepalive 64;
}
server {
listen 80;
server_name yourdomain.com www.yourdomain.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl http2;
server_name yourdomain.com www.yourdomain.com;
ssl_certificate /etc/letsencrypt/live/yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/yourdomain.com/privkey.pem;
# Security headers
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
# Next.js static assets — aggressive caching
location /_next/static/ {
alias /var/www/nextapp/.next/static/;
expires 1y;
add_header Cache-Control "public, immutable";
access_log off;
}
# Public folder static files
location /public/ {
alias /var/www/nextapp/public/;
expires 30d;
add_header Cache-Control "public";
}
# All other requests → Next.js server
location / {
proxy_pass http://nextjs_upstream;
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_cache_bypass $http_upgrade;
proxy_read_timeout 30s;
}
}
EOF
ln -s /etc/nginx/sites-available/nextapp /etc/nginx/sites-enabled/
certbot --nginx -d yourdomain.com -d www.yourdomain.com
nginx -t && systemctl reload nginx
Step 4: Environment Variable Management
# Create production .env.local (never commit this to git)
cat > /var/www/nextapp/.env.local << 'EOF'
NEXT_PUBLIC_API_URL=https://yourdomain.com/api
DATABASE_URL=postgresql://user:password@localhost:5432/mydb
NEXTAUTH_URL=https://yourdomain.com
NEXTAUTH_SECRET=GENERATE_WITH_openssl_rand_-hex_32
EOF
chown nodeapp:nodeapp /var/www/nextapp/.env.local
chmod 600 /var/www/nextapp/.env.local
Restart PM2 after changing environment variables:
su - nodeapp -c "pm2 restart nextapp --env production"
Step 5: Deployment Script
cat > /home/nodeapp/deploy.sh << 'EOF'
#!/bin/bash
set -e
cd /var/www/nextapp
echo "Pulling latest code..."
git pull origin main
echo "Installing dependencies..."
npm install --production=false
echo "Building..."
npm run build
echo "Restarting PM2..."
pm2 reload nextapp --env production
echo "Deploy complete!"
EOF
chmod +x /home/nodeapp/deploy.sh
The pm2 reload command performs a zero-downtime reload — it starts new worker processes with the updated code before gracefully shutting down the old ones, ensuring users experience no interruption during deployment.
Step 6: Next.js App Router Performance Optimisations
Enable Next.js Output Standalone Mode
For smaller deployment footprint, configure standalone output in next.config.js:
/** @type {import('next').NextConfig} */
const nextConfig = {
output: 'standalone',
compress: true,
poweredByHeader: false,
// Image optimisation configuration
images: {
domains: ['yourdomain.com', 'cdn.yourdomain.com'],
formats: ['image/avif', 'image/webp'],
},
// Headers for security and caching
async headers() {
return [
{
source: '/(.*)',
headers: [
{ key: 'X-DNS-Prefetch-Control', value: 'on' },
{ key: 'X-Content-Type-Options', value: 'nosniff' },
],
},
]
},
}
module.exports = nextConfig
Remove Google Fonts for China Compatibility
If your Next.js app loads Google Fonts, replace with Next.js’s built-in font system which self-hosts fonts:
// Before (blocks in China)
import { Inter } from 'next/font/google'
const inter = Inter({ subsets: ['latin'] })
// After (self-hosted, works everywhere)
// next/font/google automatically downloads and self-hosts fonts at build time
// — no runtime Google CDN requests, compatible with mainland China
Next.js 13+ self-hosts Google Fonts automatically — the font files are included in your build output, served from your Hong Kong VPS, and accessible from mainland China without any external CDN dependency.
Step 7: Monitoring with PM2
# Real-time process monitoring
pm2 monit
# Check logs
pm2 logs nextapp --lines 100
# Process status
pm2 status
# CPU and memory usage over time
pm2 show nextapp
For production monitoring dashboards, connect PM2 to Prometheus using pm2-prometheus-exporter and visualise in Grafana — see our monitoring guide for the full setup.
Common Issues and Fixes
Port Already in Use
lsof -i :3000
kill -9 PID_NUMBER
pm2 start nextapp
Build Out of Memory
# Increase Node.js heap for large Next.js builds
NODE_OPTIONS="--max-old-space-size=4096" npm run build
Static Files 404
# Ensure Nginx alias path ends with /
location /_next/static/ {
alias /var/www/nextapp/.next/static/; # Trailing / is required
}
Conclusion
Deploying Next.js on a Hong Kong VPS with PM2 cluster mode and Nginx reverse proxy delivers a self-hosted, China-accessible alternative to Vercel — at a predictable monthly cost regardless of function invocations or bandwidth. The CN2 GIA routing ensures mainland Chinese users receive fast SSR responses at 30–60ms TTFB, while self-hosting fonts eliminates Google CDN dependencies that block in China.
Deploy your Next.js app: Browse Server.HK Hong Kong VPS plans — a 4 GB plan with 4 vCPU handles PM2 cluster mode with one worker per core comfortably.