Back

Deploying Multiple Node.js APIs on Single Linux Server with HTTPS

Linux
Node.js
Nginx
HTTPS
DevOps
Server Setup
Deploying Multiple Node.js APIs on Single Linux Server with HTTPS

Deploying Multiple Node.js APIs on Single Linux Server with HTTPS

This guide will walk you through the process of deploying multiple Node.js APIs on a single Linux server and securing them with HTTPS using Nginx as a reverse proxy.

Prerequisites

  • A Linux server (Ubuntu/Debian)
  • Root access to the server
  • Domain name pointed to your server
  • Basic command line knowledge
  • Node.js applications ready for deployment

Step 1: Initial Server Access and Setup

First, connect to your server and update the package list:

# Connect to your server
ssh root@your_droplet_ip

# Update package list
sudo apt update

# Install Git
sudo apt install git -y

Step 2: Installing Node.js using NVM

Install Node Version Manager (NVM) and Node.js:

# Install NVM
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash

# Load NVM
source ~/.bashrc

# Install LTS version of Node.js
nvm install --lts

Step 3: Setting Up Project Directory

Create and navigate to the project directory:

# Create directory structure
mkdir -p /var/www && cd /var/www

# Clone your repository
git clone https://github.com/your-username/your-repo.git

# Navigate to project directory
cd your-repo

# Install project dependencies
npm install

Step 4: Environment Configuration

Create and configure environment variables:

# Create .env file
nano .env

# Add environment variables
PORT=3001
DATABASE_URL=your-database-url

Step 5: Setting Up Process Manager (PM2)

Install and configure PM2 for managing Node.js applications:

# Install PM2 globally
npm install pm2 -g

# Start first API
pm2 start index.js --name api1

# Save PM2 configuration
pm2 save

# Configure PM2 to start on system boot
pm2 startup

Step 6: Installing and Configuring Nginx

Install and configure Nginx as a reverse proxy:

# Install Nginx
sudo apt install nginx -y

# Edit Nginx configuration
sudo nano /etc/nginx/sites-available/default

Add the following configuration:

# HTTPS server block for securing API communication
server {
    listen 80; 
    server_name your-domain.com;

    # API 1 routing
    location /api1 {
        proxy_pass http://localhost:3001;   # Route to API 1 running on port 3001
        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;
    }

    # API 2 routing
    location /api2 {
        proxy_pass http://localhost:3002;   # Route to API 2 running on port 3002
        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;
        rewrite ^/api2(/.*)$ $1 break; # Strips the /api2 prefix before forwarding
    }
}

Test and reload Nginx configuration:

# Test Nginx configuration
sudo nginx -t

# Reload Nginx
sudo systemctl reload nginx

Step 7: Setting Up SSL/HTTPS

Install Certbot and obtain SSL certificates:

# Install Certbot and its Nginx plugin
sudo apt install certbot python3-certbot-nginx -y

# Obtain and install SSL certificate
sudo certbot --nginx -d your-domain.com

Key Features of This Setup

  1. Multiple API Support

    • Different endpoints for each API (/api1, /api2)
    • Independent port numbers for each service
    • Clean URL routing
  2. Security Features

    • HTTPS encryption
    • Automatic SSL certificate renewal
    • Proxy headers for security
  3. Maintenance Benefits

    • Easy to add more APIs
    • Independent scaling
    • Simplified SSL management

Best Practices

  1. API Management

    • Use meaningful names for PM2 processes
    • Keep track of port numbers
    • Document API endpoints
  2. Security

    • Regularly update SSL certificates
    • Monitor access logs
    • Implement rate limiting
  3. Monitoring

    • Use PM2 monitoring
    • Check Nginx access logs
    • Monitor SSL certificate expiry

Troubleshooting

  1. Common Issues

    • Port conflicts
    • SSL certificate renewal failures
    • Nginx configuration errors
  2. Debug Commands

# Check Nginx errors
sudo nginx -t
sudo tail -f /var/log/nginx/error.log

# Check PM2 logs
pm2 logs
pm2 status

# Check SSL certificate
sudo certbot certificates

Conclusion

This setup provides a robust and secure way to host multiple Node.js APIs on a single server. Remember to:

  • Regularly update your SSL certificates
  • Monitor your applications
  • Keep your system updated
  • Backup your configurations

For more advanced configurations or specific use cases, refer to the official documentation of Nginx, PM2, and Certbot.