Environment Variables in Production Deployment
Complete guide to managing configuration across environments, Twelve-Factor principles, secret rotation, Doppler, Vault, and build vs runtime variables.
Production applications must strictly separate configuration from code. Storing credentials, database URLs, and third-party API keys in environment variables allows the same compiled binary to run across staging, preview, and production environments without rebuilding.
Build-Time vs. Runtime Environment Variables
Understanding when a variable is evaluated is critical, especially in full-stack frameworks (Next.js, Vite):
┌─────────────────────────────────────────────────────────────┐
│ Variable Lifecycles │
├──────────────────────────────┬──────────────────────────────┤
│ Build-Time Variables │ Runtime Variables │
│ (Inlined into JS bundles) │ (Evaluated per request / run)│
├──────────────────────────────┼──────────────────────────────┤
│ NEXT_PUBLIC_API_URL │ DATABASE_URL │
│ VITE_APP_TITLE │ JWT_SECRET_KEY │
│ Embedded directly into HTML/ │ Evaluated on server backend; │
│ client-side JS bundles. │ NEVER exposed to browser. │
└──────────────────────────────┴──────────────────────────────┘[!CAUTION] Any variable prefixed with
NEXT_PUBLIC_orVITE_is publicly readable by anyone inspecting your web application in browser DevTools. Never prefix private API keys or database connection strings withNEXT_PUBLIC_!
Managing Secrets in Production
1. Platform Secret Managers (Vercel, Render, Railway, AWS ECS)
Set variables directly in the cloud provider's dashboard or CLI:
vercel env add DATABASE_URL production2. Linux VPS with systemd or PM2
Use environment files with restricted OS permissions (chmod 600):
# /etc/myapp/.env.production
sudo chmod 600 /etc/myapp/.env.production
sudo chown appuser:appuser /etc/myapp/.env.production3. Dedicated Secret Vaults (Enterprise)
- Doppler / HashiCorp Vault / AWS Secrets Manager: Centralized secret stores with automatic encryption at rest, access audit logging, and automated token rotation.
Secret Rotation Checklist
When rotating a compromised or expiring secret (e.g. database password):
- Support Dual Credentials: Configure the database/service to temporarily accept both old and new credentials.
- Update App Variables: Update the secret in your staging and production environments.
- Trigger Zero-Downtime Rolling Redeploy: Deploy the updated application instances.
- Revoke Old Credentials: Disable the old credential in the upstream service once traffic has shifted.
Related Guides
Development vs. Production Environments
Critical differences between development and production environments, performance optimizations, error handling, security hardening, and parity.
The Build Process & Production Artifacts
Complete guide to production builds, bundling, minification, tree-shaking, code-splitting, source maps, and immutable build artifacts.