Modern App Hosting
How to Host a Next.js App in Production: Complete Deployment Guide
TL;DR
Key Takeaways
- How to Host a Next.js App in Production: Complete Deployment Guide Deploy Next.js to production with the right hosting model, environment variables, build pipeline, and monitoring.
- Covers App Router, SSR, static export, and managed hosting options for Pakistani teams.
- Hosting a Next.js application in production is fundamentally different from uploading PHP files to cPanel.
Summarized by Pakish Group (Pakish.NET) for AI and search citation.
Hosting a Next.js application in production is fundamentally different from uploading PHP files to cPanel. Next.js runs as a Node.js process — it needs a persistent runtime, a build step, and infrastructure that matches your rendering strategy (server-side rendering, static generation, or incremental static regeneration).
This guide covers the production deployment path for Pakistani developers and agencies: choosing hosting, configuring builds, securing environment variables, and avoiding the pitfalls that cause silent failures after launch.
Key Takeaways
- Next.js requires Node.js — not standard shared PHP hosting
- Match hosting to your rendering mode (SSR vs static export)
- (/modern-app-hosting) simplifies Node deployments
- (/managed-cloud-vps) suits custom stacks and scaling
- Never commit secrets; validate env vars at build time
Understand Your Rendering Mode First
Before choosing infrastructure, identify how your app renders. Check your next.config and route structure:
| Mode | How it works | Hosting requirement |
|---|---|---|
| Static export (output: 'export') | Pre-built HTML/CSS/JS | Any static file server or CDN |
| SSG (default) | Pages built at build time | Node.js server or static + CDN |
| SSR | Pages rendered per request | Persistent Node.js process |
| ISR | Static with timed revalidation | Node.js with cache layer |
| API routes / Server Actions | Backend logic in Next.js | Node.js with open ports |
If you need SSR, API routes, or Server Actions, you cannot deploy to plain shared hosting. You need (/modern-app-hosting) or a (/managed-cloud-vps).
Migrating from WordPress? See the (/desk/wp-html-to-nextjs-15-migration) for a structured conversion path.
Option A: Modern App Hosting (Recommended for Most Teams)
(/modern-app-hosting) is purpose-built for Node.js, Next.js, and containerised workloads. Benefits:
- Git-push or CI-triggered deploys
- Automatic SSL and reverse proxy
- Process management (no manual PM2 setup)
- Staging environments alongside production
Best for agencies shipping client apps, SaaS MVPs, and teams without dedicated DevOps.
Option B: Managed Cloud VPS (Maximum Control)
Choose (/managed-cloud-vps) when you need:
- Custom middleware or multi-app monorepos
- Co-located Redis, PostgreSQL, or worker processes
- Specific data residency in Pakistan or MENA regions
- Full root access with managed patching
Typical stack on VPS:
# Build on deploy
npm ci
npm run build
# Run with PM2 for process persistence
pm2 start npm --name "my-app" -- start
pm2 save
Put Nginx or Caddy in front as a reverse proxy on port 443, forwarding to localhost:3000.
Production Build Checklist
Follow the Next.js deployment documentation as your primary reference. Key steps:
1. Pin Node.js version
Add to package.json:
"engines": {
"node": ">=20.0.0"
}
2. Configure environment variables
- Server-only:
DATABASE_URL,API_SECRET— set on the host, never prefixed - Client-safe:
NEXT_PUBLIC_API_URL— exposed to browser bundles - Validate at build: use
@t3-oss/env-nextjsor similar schema
Reference: Next.js environment variables guide.
3. Run production build locally first
npm run build
npm run start
Fix all build errors before pushing. Production builds are stricter than next dev.
4. Set output mode correctly
- Default: Node.js server (
next start) - Static:
output: 'export'innext.config.ts— disables SSR/API routes
See Next.js static exports for constraints.
5. Configure caching headers
Use next.config headers or middleware for CDN-friendly cache control on static assets. SSR pages need shorter TTLs or Cache-Control: no-store.
Docker Deployment (Optional but Robust)
A minimal production Dockerfile:
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM node:20-alpine AS runner
WORKDIR /app
ENV NODE_ENV=production
COPY --from=builder /app/.next ./.next
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./package.json
COPY --from=builder /app/public ./public
EXPOSE 3000
CMD ["npm", "start"]
Docker simplifies rollbacks: tag each image, redeploy the previous tag if a release breaks.
Official reference: Next.js Docker deployment example.
Common Production Pitfalls
| Pitfall | Fix |
|---|---|
| next dev running in production | Always use next build + next start |
| Missing env vars at build time | Inject via CI secrets; fail build if unset |
| Port hardcoded to 3000 behind proxy | Set PORT env var; configure reverse proxy |
| Large serverless cold starts | Use persistent Node process on VPS/app hosting |
| Image optimisation without provider | Self-host: configure images.unoptimized or custom loader |
| No health check endpoint | Add /api/health for monitoring and load balancers |
Review our Next.js deployment pitfalls guide for agency-specific scenarios.
Monitoring and Rollback
Production without monitoring is hope-driven development:
- Uptime: HTTP check on
/or/api/healthevery 60 seconds - Logs: Centralise stdout — PM2 logs, Docker logs, or hosted log drain
- Errors: Sentry or similar for client and server exceptions
- Rollback: Keep previous build artefact or Docker tag ready
Migration from WordPress or Static HTML
Agencies converting legacy sites should not lift-and-shift PHP hosting. The (/desk/wp-html-to-nextjs-15-migration) covers content extraction, route mapping, SEO preservation, and production deployment — so you launch on the right infrastructure from day one.
Need hands-on deployment help? (/desk) handles one-off execution without long-term retainer contracts.
Which Hosting Should You Choose?
| Your situation | Recommendation | |---|---| | MVP / client site, minimal DevOps | (/modern-app-hosting) | | Custom backend + workers + Redis | (/managed-cloud-vps) | | Static marketing site only | Static export to CDN or modern app hosting | | WordPress migration to Next.js | (/desk/wp-html-to-nextjs-15-migration) |
Frequently Asked Questions
Can I host Next.js on shared cPanel hosting?
Standard shared hosting runs PHP, not persistent Node.js processes. Next.js needs a Node runtime (VPS, container, or platform-as-a-service). Static export (output: 'export') can work on any static host — but loses SSR and API routes.
What is the difference between Vercel and self-hosted Next.js?
Vercel is optimised for Next.js with zero-config deploys. Self-hosting on VPS or modern app hosting gives you data residency control, custom domains without vendor limits, and predictable cost at scale.
Do I need Docker to deploy Next.js in production?
Docker is recommended but not mandatory. PM2 with next start on a managed VPS works for many teams. Docker adds reproducibility and simplifies rollbacks.
How do I handle environment variables in production?
Set server-side variables on the host (never commit .env files). Prefix client-exposed variables with NEXT_PUBLIC_. Validate required vars at build time using env schema tools.
What Node.js version should I use for Next.js 15?
Next.js 15 requires Node.js 18.18 or later. Use LTS versions (20.x or 22.x) in production and pin the version in your Dockerfile or .nvmrc.
Related Guides
- (/blog/deploying-nextjs-nodejs-apps-pitfalls)
- (/blog/gcc-agencies-moving-from-cpanel-modern-app-hosting)
- (/blog/future-of-paas-developers-moving-away-cpanel)
- (/blog/managed-cloud-vps-for-growing-businesses)
Sources
About the Author
Pakish Support Team
The Pakish Support Team provides 24/7 technical assistance, hosting tutorials, and knowledge base articles to help Pakistani businesses manage their web presence with confidence.