Deploy Multiple NodeJS Apps on single Server with SSL, Nginx, PM2 – Part 2

deploy-multiple-nodejs-apps-on-single-server-with-ssl,-nginx,-pm2-–-part-2

Prerequisites

Please Read Part 1 of the series here.

We have already setup NGINX, PM2 and SSL for the First APP and we are going to setup a new NodeJS APP.
There is very few steps we need to do to get the new app up and running

Step 08 – Clone New Project Or Create New with file

It’s quite similar to the step 02 we are just going to create a new folder and create new app and run it on different Port No.

mkdir App2
cd App2

now just create a new file called index.js

nano index.js

now paste following code in index.js

const http = require('http');

const hostname = 'localhost';
const port = 4000; //make sure this port no is different from the first one

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello Everyone from APP 2 !n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

Step 09 – Start the App With PM2

we are going start the app with PM2 and give it a Name that is optional

pm2 start index.js --name "APP 2"

this will start the app at port no 4000 we can also see all running app with PM2 by following

pm2 list

this will give you list of all running apps you can start/stop/restart using id or Name aswell.

Step 10 – Adding Reverse Proxy with NGINX

we need to add server config one more time for this app aswell.
To update server First open the config

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

and add this new block in location part of the server block

    server_name yourdomain2.com www.yourdomain2.com;

    location / {
        proxy_pass http://localhost:4000; 
        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 are planing to host secound app on subdomain just replace yourdomain2.com with subdomain like api2.yourdomain.com

then check and restart the NGINX

# Check NGINX config
sudo nginx -t

# Restart NGINX
sudo service nginx restart

if domain is pointed you should see your app live on port 80 there is one more step to go adding SSL.

Step 11 – Adding SSL for Second APP

we already have certbot installed so adding additional domains are not an issue

sudo certbot --nginx -d yourdomain2.com -d www.yourdomain2.com

or for Subdomain

sudo certbot --nginx -d api2.yourdomain.com

that’s all your New app also should be Live on New domain with SSL.

Thanks for reading Cheers.

Total
1
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
magic-of-window.open-in-browsers

Magic of window.open in browsers

Next Post
using-classes-inside-react’s-functional-components

Using Classes Inside React’s Functional Components

Related Posts