Next Starter Kit
Deployment

Deployment

Deploy your Next.js starter kit to production

Deployment

Deploy your application to production with this comprehensive guide.

Pre-Deployment Checklist

Before deploying, ensure you have:

Database

  • Production database ready (Neon, Supabase, etc.)
  • Database backups configured
  • Connection pooling enabled
  • SSL connections configured

Environment Variables

  • All required env vars set
  • Production secrets generated
  • API keys for production services
  • NODE_ENV=production

Services

  • Stripe live keys configured
  • Email provider production account
  • UploadThing production token
  • OAuth apps configured for production URLs

Code

  • All tests passing
  • Build succeeds locally
  • No console.log statements
  • Error handling implemented
  • Rate limiting configured

Security

  • Security headers enabled
  • HTTPS enforced
  • Secrets rotated from development
  • Admin accounts secured

Deployment Options

Vercel is the easiest option for Next.js applications.

1. Install Vercel CLI

npm i -g vercel

2. Login

vercel login

3. Deploy

# From project root
cd nextjs-starter-kit
vercel

Follow prompts to link project.

4. Set Environment Variables

Option A: Vercel CLI

vercel env add BETTER_AUTH_SECRET
# Paste value when prompted

vercel env add DATABASE_URL
vercel env add STRIPE_SECRET_KEY
# ... add all env vars

Option B: Vercel Dashboard

  1. Go to project settings
  2. Navigate to Environment Variables
  3. Add each variable
  4. Select environments (Production, Preview, Development)

5. Deploy to Production

vercel --prod

Your app will be live at https://your-project.vercel.app!

6. Custom Domain

  1. Go to project settings > Domains
  2. Add your custom domain
  3. Configure DNS records
  4. Update BETTER_AUTH_URL and NEXT_PUBLIC_APP_URL
vercel env add BETTER_AUTH_URL production
# Enter: https://yourdomain.com

vercel env add NEXT_PUBLIC_APP_URL production
# Enter: https://yourdomain.com

Other Platforms

The app can also be deployed to:

Railway

# Install Railway CLI
npm i -g @railway/cli

# Login
railway login

# Initialize project
railway init

# Add env vars in Railway dashboard
# Deploy
railway up

Netlify

# Install Netlify CLI
npm i -g netlify-cli

# Login
netlify login

# Deploy
netlify deploy --prod

Docker

# Dockerfile
FROM node:20-alpine AS base

# Install dependencies only when needed
FROM base AS deps
RUN apk add --no-cache libc6-compat
WORKDIR /app

COPY package.json pnpm-lock.yaml ./
RUN corepack enable pnpm && pnpm i --frozen-lockfile

# Build the app
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .

RUN corepack enable pnpm && pnpm build

# Production image
FROM base AS runner
WORKDIR /app

ENV NODE_ENV production

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

COPY --from=builder /app/public ./public
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static

USER nextjs

EXPOSE 3000

ENV PORT 3000

CMD ["node", "server.js"]
# Build
docker build -t nextjs-starter .

# Run
docker run -p 3000:3000 --env-file .env nextjs-starter

Database Migration

  1. Create production database at neon.tech
  2. Copy connection string
  3. Add to Vercel env vars
vercel env add DATABASE_URL production
# Paste Neon connection string
  1. Push schema
# Set DB URL temporarily for migration
export DATABASE_URL="your-production-db-url"
pnpm db:push

Supabase

  1. Create project at supabase.com
  2. Get connection string from Settings > Database
  3. Add to environment variables
  4. Run migrations

Stripe Configuration

1. Switch to Live Mode

In Stripe Dashboard, toggle to "Live mode".

2. Update API Keys

vercel env add STRIPE_SECRET_KEY production
# Enter live secret key (sk_live_...)

vercel env add STRIPE_PUBLISHABLE_KEY production
# Enter live publishable key (pk_live_...)

3. Create Products & Prices

Create products in live mode with same structure as test mode.

4. Update Price IDs

vercel env add STRIPE_PRICE_PRO_MONTHLY production
vercel env add STRIPE_PRICE_PRO_YEARLY production
vercel env add STRIPE_PRICE_STARTUP_MONTHLY production
vercel env add STRIPE_PRICE_STARTUP_YEARLY production

5. Configure Webhook

  1. Go to Developers > Webhooks
  2. Add endpoint: https://yourdomain.com/api/stripe/webhook
  3. Select all subscription and invoice events
  4. Copy signing secret
vercel env add STRIPE_WEBHOOK_SECRET production
# Paste webhook secret

Email Configuration

  1. Verify your domain at resend.com
  2. Get production API key
  3. Configure:
vercel env add EMAIL_PROVIDER production
# Enter: resend

vercel env add RESEND_API_KEY production
# Enter API key

vercel env add EMAIL_FROM production
# Enter: Your App <noreply@yourdomain.com>

Other Providers

Configure similarly with production credentials.

OAuth Configuration

Google OAuth

  1. Go to Google Cloud Console
  2. Update OAuth consent screen
  3. Add authorized redirect URI:
    • https://yourdomain.com/api/auth/callback/google
  4. Get production credentials
  5. Add to Vercel:
vercel env add GOOGLE_CLIENT_ID production
vercel env add GOOGLE_CLIENT_SECRET production

GitHub OAuth

  1. Go to GitHub OAuth Apps
  2. Create new app or update existing
  3. Set callback URL: https://yourdomain.com/api/auth/callback/github
  4. Add credentials to Vercel

Post-Deployment Steps

1. Verify Deployment

Check your deployed app:

  • ✅ Homepage loads
  • ✅ Sign up works
  • ✅ Sign in works
  • ✅ OAuth works
  • ✅ Email sending works
  • ✅ Blog posts display
  • ✅ Admin dashboard accessible

2. Create Admin Account

  1. Sign up with your admin email
  2. SSH into server or use Vercel CLI
# Run admin setup script
vercel env pull # Get env vars locally
pnpm admin:setup

Or manually update user role in database.

3. Seed Production Data

# Run setup script
pnpm project:setup

# Or individually
pnpm plans:setup
pnpm blog:seed
pnpm testimonials:seed

4. Test Critical Flows

  • Authentication: Sign up, sign in, password reset
  • Subscriptions: Create test subscription
  • Webhooks: Verify Stripe webhooks work
  • Emails: Send test emails
  • File Uploads: Upload test files

5. Monitor

Set up monitoring:

  • Vercel Analytics
  • Sentry for error tracking
  • Uptime monitoring (UptimeRobot, Pingdom)
  • Log aggregation (LogDNA, Datadog)

Security Headers

Verify security headers are enabled in production:

curl -I https://yourdomain.com

Should include:

Strict-Transport-Security: max-age=31536000
X-Frame-Options: DENY
X-Content-Type-Options: nosniff
Content-Security-Policy: ...

Performance Optimization

Enable Edge Caching

Configure Next.js to use edge caching:

// app/blog/page.tsx
export const revalidate = 3600; // Revalidate every hour

Image Optimization

Images are auto-optimized by Next.js Image component.

CDN

Vercel automatically uses CDN for static assets.

Database Connection Pooling

Enable connection pooling in database settings for better performance.

Monitoring & Maintenance

Logs

View logs in Vercel Dashboard > Functions > Logs.

Error Tracking

Install Sentry:

pnpm add @sentry/nextjs
// sentry.client.config.ts
import * as Sentry from "@sentry/nextjs";

Sentry.init({
  dsn: process.env.NEXT_PUBLIC_SENTRY_DSN,
  tracesSampleRate: 1.0,
});

Uptime Monitoring

Set up alerts for downtime:

Backup Strategy

  1. Database: Daily automated backups (Neon/Supabase)
  2. User uploads: S3/R2 with versioning
  3. Code: Git repository
  4. Environment vars: Secure vault

Rollback Strategy

Vercel

  1. Go to Deployments
  2. Find working deployment
  3. Click "..." > Promote to Production

Manual

# Revert to previous commit
git revert HEAD
git push

# Or
git reset --hard <commit-hash>
git push --force

Scaling

Database

  • Upgrade Neon/Supabase plan
  • Enable connection pooling
  • Add read replicas
  • Index frequently queried columns

Application

Vercel auto-scales. For other platforms:

  • Horizontal scaling (multiple instances)
  • Load balancer
  • Cache frequently accessed data

File Storage

  • Use CDN for uploads
  • Implement lazy loading
  • Compress images

Cost Optimization

Vercel

  • Free tier: Hobby projects
  • Pro ($20/month): Small apps
  • Enterprise: Large apps

Database

  • Neon: Free tier available, pay as you grow
  • Supabase: Free tier, then $25/month

Other Services

  • Resend: 100 emails/day free
  • UploadThing: 2GB free
  • OpenRouter: Free models available

Troubleshooting

Build Failures

Check build logs in Vercel dashboard. Common issues:

  • Missing environment variables
  • TypeScript errors
  • Dependency issues

Database Connection Errors

  • Verify connection string
  • Check database is running
  • Ensure IP allowlist includes Vercel IPs
  • Enable SSL

Webhook Issues

  • Verify endpoint is accessible (HTTPS)
  • Check webhook secret matches
  • Review Stripe webhook logs

Email Not Sending

  • Verify domain is verified with provider
  • Check API keys are correct
  • Review email logs in provider dashboard

Best Practices

  1. Use Environment Variables - Never hardcode secrets
  2. Enable HTTPS - Always use secure connections
  3. Monitor Errors - Set up error tracking
  4. Regular Backups - Automate database backups
  5. Update Dependencies - Keep packages up-to-date
  6. Test Before Deploy - Run tests in CI/CD
  7. Gradual Rollout - Use preview deployments
  8. Document Changes - Keep changelog updated

CI/CD Pipeline

GitHub Actions

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

on:
  push:
    branches: [main]

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: "20"
      - uses: pnpm/action-setup@v2

      - name: Install dependencies
        run: pnpm install

      - name: Run tests
        run: pnpm test

      - name: Build
        run: pnpm build

      - name: Deploy to Vercel
        run: vercel --prod --token=${{ secrets.VERCEL_TOKEN }}

Next Steps

On this page