Compute Comparison
How-To Guides11 min read

Self-Hosting Next.js on GPU Cloud: Full Deployment Guide (2026)

How to self-host a Next.js application on GPU cloud providers — Docker setup, GPU access in Node.js, provider comparison, and cost breakdown vs Vercel.

Self-hosting Next.js on a GPU cloud instance gives you the best of both worlds: the full Next.js feature set (App Router, server components, server actions, ISR) plus direct access to GPU compute for inference, image generation, or embedding pipelines — all on the same machine, with no cold starts and no per-request serverless pricing. This guide walks through provisioning a GPU instance, configuring Nginx with SSL, deploying Next.js as a persistent Node.js process, and running a vLLM inference server alongside it.

Choosing a GPU cloud provider

For a Next.js app with an AI backend, you typically don't need a high-end H100. An A10G (24 GB VRAM) or L40S (48 GB VRAM) handles most inference workloads at a fraction of the cost. See live GPU cloud prices for current rates across tracked providers.

Lambda Labs offers the best balance of price and reliability for persistent instances. An A10G on-demand costs ~$0.75/hr ($540/month). CoreWeave is cheaper for reserved instances. RunPod is cheapest for spot/interruptible workloads but instances can be preempted. For a production app, Lambda Labs or CoreWeave on-demand instances are the most reliable choice.

Pick an instance with at least 4 vCPUs and 16 GB RAM for the Next.js app layer. The GPU handles inference; the CPU handles web requests, SSR, and API routes. A combined instance (e.g. Lambda Labs A10G: 30 vCPUs, 200 GB RAM, 1x A10G) is more cost-effective than separate CPU and GPU instances for most workloads.

Initial server setup

Provision an Ubuntu 22.04 instance with CUDA pre-installed (most GPU cloud providers offer this as a base image). SSH in and run the initial setup: `sudo apt update && sudo apt upgrade -y && sudo apt install -y nginx certbot python3-certbot-nginx nodejs npm git`.

Install Node.js 20 LTS via nvm for better version management: `curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash && source ~/.bashrc && nvm install 20 && nvm use 20`.

Install PM2 for process management: `npm install -g pm2`. PM2 keeps your Next.js server running, restarts it on crashes, and manages logs. It also handles zero-downtime reloads during deployments.

Configure the firewall: `sudo ufw allow 22 && sudo ufw allow 80 && sudo ufw allow 443 && sudo ufw enable`. Only expose ports 80 and 443 publicly — your Next.js app and vLLM server communicate internally on localhost.

Deploying Next.js

Clone your repo and install dependencies: `git clone https://github.com/yourorg/yourapp && cd yourapp && npm ci`. Build the production bundle: `npm run build`. This generates the `.next` directory with optimised server and client bundles.

Create a PM2 ecosystem file (`ecosystem.config.js`): set `name: "nextjs"`, `script: "node_modules/.bin/next"`, `args: "start"`, `env: { NODE_ENV: "production", PORT: 3000 }`. Start with `pm2 start ecosystem.config.js && pm2 save && pm2 startup`. The `pm2 startup` command generates a systemd service so Next.js restarts automatically after server reboots.

For deployments, use a simple script: `git pull && npm ci && npm run build && pm2 reload nextjs`. PM2's `reload` command performs a zero-downtime restart — it starts the new process before killing the old one, so users see no interruption. For more sophisticated deployments, add a GitHub Actions workflow that SSHs into the server and runs this script on push to main.

Nginx configuration with SSL

Create an Nginx server block at `/etc/nginx/sites-available/yourapp`. The key settings: `proxy_pass http://127.0.0.1:3000` to forward requests to Next.js, `proxy_http_version 1.1` and `proxy_set_header Upgrade $http_upgrade` for WebSocket support (needed for Next.js HMR in dev and some real-time features), and `proxy_read_timeout 300s` for long AI inference requests.

Get a free SSL certificate with Certbot: `sudo certbot --nginx -d yourdomain.com -d www.yourdomain.com`. Certbot automatically modifies your Nginx config to add HTTPS and sets up auto-renewal via a systemd timer. Verify renewal works: `sudo certbot renew --dry-run`.

Add security headers to your Nginx config: `add_header X-Frame-Options SAMEORIGIN`, `add_header X-Content-Type-Options nosniff`, `add_header Referrer-Policy strict-origin-when-cross-origin`. For AI apps handling user data, also add `add_header Strict-Transport-Security "max-age=31536000; includeSubDomains"`.

Running vLLM alongside Next.js

Start vLLM on a local port (not exposed publicly): `python -m vllm.entrypoints.openai.api_server --model meta-llama/Meta-Llama-3.1-8B-Instruct --port 8000 --api-key YOUR_INTERNAL_SECRET`. vLLM binds to `0.0.0.0:8000` by default — change to `--host 127.0.0.1` to ensure it's only accessible locally.

In your Next.js API routes, call vLLM via its OpenAI-compatible API: set `baseURL: "http://127.0.0.1:8000/v1"` and `apiKey: process.env.VLLM_API_KEY` in your OpenAI client. Since both processes are on the same machine, latency is under 1ms — far lower than calling an external API.

Add vLLM to PM2 as well: create a second entry in `ecosystem.config.js` with `script: "python"`, `args: "-m vllm.entrypoints.openai.api_server --model meta-llama/Meta-Llama-3.1-8B-Instruct --port 8000"`, `interpreter: "/usr/bin/python3"`. This ensures vLLM restarts automatically alongside Next.js.

Monitor both processes: `pm2 monit` shows real-time CPU, memory, and request logs for all managed processes. `pm2 logs nextjs` and `pm2 logs vllm` stream individual process logs.

Cost comparison vs Vercel + managed API

A Lambda Labs A10G instance at $0.75/hr costs $540/month. Compare this to Vercel Pro ($20/month) + OpenAI API costs. At $0.15/1M input tokens (GPT-4o-mini), $540/month buys 3.6 billion tokens. If your app uses more than that, self-hosting wins on cost. If you use less, Vercel + managed API is cheaper.

The self-hosted advantage grows with volume. At 10M tokens/day (300M/month), OpenAI GPT-4o-mini costs $45/month — well under the $540 self-hosted cost. But at 100M tokens/day (3B/month), OpenAI costs $450/month vs $540 self-hosted — and self-hosting gives you a much more capable model (Llama 3.1 70B) for the same price.

Use the LLM ROI Calculator to find your exact break-even point. Factor in engineering time for setup and maintenance — typically 4–8 hours initial setup and 1–2 hours/month ongoing.

Frequently asked questions

Can I use Next.js App Router on a self-hosted server?
Yes. Next.js App Router, server components, server actions, and all other Next.js features work identically on a self-hosted Node.js server. The only features that don't work are Vercel-specific ones: Edge Runtime functions, Vercel Analytics, and Vercel's image optimisation CDN (use a self-hosted sharp-based solution or Cloudflare Images instead).
How do I handle zero-downtime deployments?
PM2's reload command starts the new process before killing the old one. For true zero-downtime with database migrations, use a blue-green deployment: keep two instances running, deploy to the inactive one, run migrations, then switch Nginx upstream to the new instance. For most apps, PM2 reload is sufficient.
What about database hosting?
For PostgreSQL, you have three options: run it on the same GPU instance (simplest, no network latency, but no managed backups), use a managed service like Supabase or Neon (adds ~5ms latency but handles backups and scaling), or run a separate database instance on the same GPU cloud provider (low latency via private networking, more control than managed).
How do I handle static assets and images?
Next.js serves static assets from the .next/static directory via Nginx. For user-uploaded images, store them in object storage (Cloudflare R2 is cheapest at $0.015/GB/month with free egress, or AWS S3). Use Next.js Image component with a custom loader pointing to your CDN URL for optimised image delivery.
Is self-hosting secure?
Yes, if configured correctly. Key security steps: keep Ubuntu and packages updated (enable unattended-upgrades), use SSH key authentication only (disable password auth), configure UFW to allow only ports 22/80/443, run Next.js and vLLM as non-root users, and use environment variables (not hardcoded secrets) for API keys. Consider Fail2ban to block brute-force SSH attempts.

More guides

Was this guide useful?