vi

Next.js VPS Hosting: Deploy React App Offshore

Next.js là React framework full-stack được sử dụng bởi Vercel, TikTok và nhiều startup hàng đầu. Mặc dù Vercel là lựa chọn phổ biến, self-hosting Next.js trên VPS offshore của AnubizHost mang lại chi phí thấp hơn nhiều, không bị vendor lock-in và toàn quyền kiểm soát domain, SSL và server configuration. Đặc biệt phù hợp cho các dự án cần chạy ở Iceland hoặc Romania với lý do pháp lý hoặc privacy.

Need this done for your project?

We implement, you ship. Async, documented, done in days.

Start a Brief

Build Next.js Standalone cho Production

Cấu hình next.config.js để tạo standalone build (bundle tất cả dependencies):

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

module.exports = nextConfig

Build và test locally trước khi deploy:

npm run build

Sau khi build, thư mục .next/standalone chứa toàn bộ server code. Sao chép lên VPS:

rsync -avz .next/standalone/ user@VPS_IP:/var/www/myapp/
rsync -avz .next/static/ user@VPS_IP:/var/www/myapp/.next/static/
rsync -avz public/ user@VPS_IP:/var/www/myapp/public/

Cấu Hình PM2 cho Next.js

Tạo PM2 ecosystem file trên VPS:

nano /var/www/myapp/ecosystem.config.js
module.exports = {
  apps: [{
    name: 'nextjs-app',
    script: 'server.js',
    cwd: '/var/www/myapp',
    instances: 'max',
    exec_mode: 'cluster',
    env: {
      NODE_ENV: 'production',
      PORT: 3000,
      HOSTNAME: '127.0.0.1',
    },
    env_production: {
      NODE_ENV: 'production',
    },
  }],
}
cd /var/www/myapp
pm2 start ecosystem.config.js --env production
pm2 save
pm2 startup

Cluster mode tận dụng tất cả CPU cores của VPS cho hiệu suất tối đa.

Nginx Reverse Proxy cho Next.js

nano /etc/nginx/sites-available/nextjs-app
upstream nextjs {
    server 127.0.0.1:3000;
    keepalive 64;
}

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

    # Serve Next.js static files directly
    location /_next/static/ {
        alias /var/www/myapp/.next/static/;
        expires 1y;
        add_header Cache-Control "public, immutable";
    }

    location /public/ {
        alias /var/www/myapp/public/;
        expires 1d;
    }

    location / {
        proxy_pass http://nextjs;
        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_cache_bypass $http_upgrade;
    }
}
ln -s /etc/nginx/sites-available/nextjs-app /etc/nginx/sites-enabled/
nginx -t && systemctl reload nginx

Deploy Tự Động với GitHub Actions

Tạo workflow tự động deploy khi push lên main branch:

# .github/workflows/deploy.yml
name: Deploy to VPS

on:
  push:
    branches: [main]

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: '20'
      - run: npm ci
      - run: npm run build
      - name: Deploy to VPS
        uses: appleboy/ssh-action@v1
        with:
          host: ${{ secrets.VPS_HOST }}
          username: root
          key: ${{ secrets.SSH_PRIVATE_KEY }}
          script: |
            cd /var/www/myapp
            pm2 reload nextjs-app

Thêm VPS_HOST và SSH_PRIVATE_KEY vào GitHub Secrets. Mỗi lần push lên main sẽ tự động deploy lên VPS offshore AnubizHost.

Why Anubiz Host

100% async — no calls, no meetings
Delivered in days, not weeks
Full documentation included
Production-grade from day one
Security-first approach
Post-delivery support included

Ready to get started?

Skip the research. Tell us what you need, and we'll scope it, implement it, and hand it back — fully documented and production-ready.

Anubiz Chat AI

Online