Deploying a Node.js service on Vultr involves provisioning a cloud instance, installing dependencies, and using PM2 to keep the application alive and scalable. This guide covers server setup, Express app creation, Nginx reverse proxy configuration, SSL encryption with Certbot, and dynamic scaling of PM2 processes.
Overview of PM2 and Cluster Mode
PM2 is a production‑grade process manager for Node.js that provides daemonization, monitoring, and load balancing. In cluster mode, PM2 spawns multiple Node.js instances that share the same port, improving CPU utilization and fault tolerance.
Installing PM2 Globally
Run sudo npm install -g pm2 to make the pm2 command available to all users on the server.
Starting an Application in Cluster Mode
Use pm2 start /path/to/app -i 4 --name my-app where -i 4 creates four instances that run in parallel.
Persisting Services Across Reboots
Execute pm2 save to record the current process list, then enable the PM2 startup script with pm2 startup so services restart automatically after a reboot.
Setting Up the Server Environment on Vultr
Vultr offers a Marketplace image pre‑installed with the latest Node.js and npm binaries, reducing setup time. Choose a region, select the Node.js image, configure size and additional features, and launch the instance.
Provisioning a Node.js Marketplace Image
After the instance is created, connect via SSH and update the package index with sudo apt update && sudo apt upgrade -y.
Creating a Sample Express Application
Run the following commands to scaffold a basic app:
mkdir ~/express-demo
cd ~/express-demo
npm init -y
npm install express
cat > index.js <<'EOF'
const express = require('express');
const app = express();
app.get('/', (req, res) => res.send('<h1>Hello World, greetings from Vultr</h1>'));
app.listen(3000, () => console.log('Server listening on port 3000'));
EOF
node index.js
Visit http://SERVER_IP:3000 to verify the response.
Configuring Nginx as a Reverse Proxy
Nginx sits in front of the Node.js process, routing external traffic to the local port and handling TLS termination. This improves security and enables load balancing across PM2 instances.
Creating the Nginx Site Configuration
Create /etc/nginx/sites-available/express-demo.conf with the following content (replace example.com with your domain):
server {
listen 80;
server_name example.com www.example.com;
location / {
proxy_pass http://127.0.0.1:3000/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
Enabling the Site and Testing
Enable the configuration with sudo ln -s /etc/nginx/sites-available/express-demo.conf /etc/nginx/sites-enabled/, test syntax via sudo nginx -t, then reload Nginx: sudo systemctl reload nginx. Allow HTTP traffic with sudo ufw allow 80/tcp.
Securing the Application with SSL via Certbot
Encrypt traffic using Lets Encrypt certificates managed by Certbot. This provides trusted HTTPS connections without additional cost.
Installing Certbot and Obtaining Certificates
Install Certbot: sudo snap install --classic certbot. Request certificates for your domain:
sudo certbot --nginx -d example.com -d www.example.com. Open port 443: sudo ufw allow 443/tcp.
Automatic Renewal
Verify renewal logic with a dry run: sudo certbot renew --dry-run. Certbot sets up a systemd timer to renew automatically.
Scaling and Monitoring with PM2
PM2 offers simple commands to adjust the number of running instances, reload code without downtime, and view logs. The scaling concepts resemble those described in the real‑time payment orchestration framework, illustrating service orchestration patterns.
Adjusting Instance Count
Reduce instances: pm2 scale express-demo 2. Increase instances: pm2 scale express-demo 5. These commands modify the cluster size on the fly.
Live Reloading and Log Management
Apply zero‑downtime updates with pm2 reload all. View real‑time output using pm2 logs. For deeper insight into distributed service design, see the STAC‑based geospatial platform article.