DEV Community

Cover image for Deploy Multiple NodeJS Apps on single Server with SSL, Nginx, PM2 - Part 2
Ranjan Singh
Ranjan Singh

Posted on • Updated on

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
Enter fullscreen mode Exit fullscreen mode

now just create a new file called index.js

nano index.js
Enter fullscreen mode Exit fullscreen mode

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}/`);
});
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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

pm2 list
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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;
    }
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

or for Subdomain

sudo certbot --nginx -d api2.yourdomain.com
Enter fullscreen mode Exit fullscreen mode

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

Thanks for reading Cheers.

Top comments (0)