Development vs Staging vs Production
Never test on production. Always stage before deploying.
Deployment Targets
This page covers VPS deployment on Ubuntu.
Server Setup: Ubuntu 22.04 LTS
Step 1: Update the Server
Step 2: Install Node.js via NVM
Always use NVM on servers, not
apt install nodejs. NVM lets you switch Node.js versions without reinstalling and avoids permission issues with global npm packages.Step 3: Install PM2
Step 4: Install Nginx
PM2: Process Manager
PM2 keeps your Node.js app alive, restarts it on crashes, and provides monitoring. This is the standard for production Node.js.Why Not Just node app.js?
PM2 Commands Reference
ecosystem.config.js
Make PM2 Survive Reboots
Nginx: Reverse Proxy
Nginx sits in front of Node.js, handling SSL, static files, and load balancing.Configure Nginx
Firewall with ufw
SSL Certificate with Certbot
NODE_ENV=production Effects
WhenNODE_ENV=production:
- Express disables detailed error messages
- Express enables view caching and performance optimizations
- Many libraries disable development-only features
- Your config module should load production values
Pre-Deployment Checklist
1
Environment variables
Verify all required .env variables are set on the server
2
Database connection
Confirm DB_URI points to production database and connection works
3
Firewall rules
Only ports 22, 80, and 443 open
4
SSL certificate
HTTPS working, HTTP redirecting to HTTPS
5
PM2 startup
PM2 configured to restart on server reboot
6
Health check
GET /health returns 200
7
npm audit
No high or critical vulnerabilities