Skip to main content
Preparing a deployment environment is the process of configuring a server so it can run your Node.js application reliably, securely, and continuously. This page covers everything from choosing a deployment target to configuring PM2, Nginx, a firewall, and SSL certificates.

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

Start with ecosystem file:

Make PM2 Survive Reboots

Nginx: Reverse Proxy

Nginx sits in front of Node.js, handling SSL, static files, and load balancing.

Configure Nginx

Enable and test:

Firewall with ufw

Always run ufw allow 22 BEFORE ufw enable. If you lock yourself out of SSH, you will need to use the server provider’s emergency console to recover access.

SSL Certificate with Certbot

Certbot auto-renews certificates via a cron job. Let’s Encrypt certificates are free and valid for 90 days.

NODE_ENV=production Effects

When NODE_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

Key Terms