Skip to content

Instantly share code, notes, and snippets.

  • Save learneriq/213a37ed9024c1405f916a4c81ad35d0 to your computer and use it in GitHub Desktop.
Save learneriq/213a37ed9024c1405f916a4c81ad35d0 to your computer and use it in GitHub Desktop.
Run multiple nodejs app in same server with different domain and run all app concurrently

Node.js Deployment

Steps to deploy a Node.js app to DigitalOcean using PM2, NGINX as a reverse proxy and an SSL from LetsEncrypt

1. Sign up for Digital Ocean

Sometimes we need to run multiple nodejs app on same server with different domain or sub domain like admin.domain.com, api.domain.com and also need to run both nodejs or reactjs app concurrently. We can do this using 2 things , first of all we need to install nginx in our server for reverse proxy to connect different domain, and for running multiple nodejs app concurrently we can use PM2 NodeJs Process Manager

Please Make sure that you have installed these things on your server

  • NodeJS
  • Git

2. Create a droplet and log in via ssh

I will be using the root user, but would suggest creating a new user

3. Install Nginx . Run these command one after another

sudo apt-get update
sudo apt-get install nginx

If you want to installed lates nodejs version or others version visit this url and there is available all of the version of nodejs . https://github.com/nodesource/distributions#debinstall

4. Modify Nginx Virtual host and add your domain

sudo nano /etc/nginx/sites-available/default

It will open a code nano code editor on server . Now delete all of the lines from here and then copy this below code and paste there and save.

# This will connect with your domain and please make sure in which port your app is running and change the port
server {
    server_name domainname.com www.domainname.com;

    location / {
        proxy_pass http://localhost:5000; #whatever port your app runs on
        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;
    }
}

If you want to add more domain or sub domain use this code

# Main Client Website
server {
    server_name domain.xyz www.domain.xyz;

    location / {
        proxy_pass http://localhost:5000; #whatever port your app runs on
        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;
    }
}

# Backend API
server {
    server_name api.domain.xyz;

    location / {
        proxy_pass http://localhost:3000; #whatever port your app runs on
        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;
    }
}

# Admin Dashboard
server {
    server_name admin.domain.xyz;

    location / {
        proxy_pass http://localhost:5001; #whatever port your app runs on
        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;
    }
}

# Manager Dashboard
server {
    server_name manager.domain.xyz;

    location / {
        proxy_pass http://localhost:5002; #whatever port your app runs on
        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;
    }
}

Restart the Nginx server after saving the file

sudo service nginx restart
sudo service nginx reload

Setup PM2 process manager to keep your app running

Install PM2 globally in your server then in your root directory create a configuration file called process.json and copy this code and paste it on that file and then save

{
  "apps": [
    {
      "name": "Backend API",
      "cwd": "./backend", // this is directory url of your app
      "script": "npm start",
      "env": {
        "NODE_ENV": "production"
      }
    },
    {
      "name": "Admin Dashboard",
      "cwd": "./admin", // this is directory url of your app
      "script": "serve -s build -l 5001",
      "env": {
        "NODE_ENV": "production"
      }
    },
    {
      "name": "Manager Dashboard",
      "cwd": "./manager", // this is directory url of your app
      "script": "serve -s build -l 5002",
      "env": {
        "NODE_ENV": "production"
      }
    }
  ]
}

Now start all the app from pm2. Make sure you are in same direcotry that process.json file avaiable

pm2 start process.json

Now visit your domain and check. Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment