Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
August 22, 2022 07:23 pm GMT

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 App2cd 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 oneconst server = http.createServer((req, res) => {  res.statusCode = 200;  res.setHeader('Content-Type', 'text/plain');  res.end('Hello Everyone from APP 2 !
');});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 configsudo nginx -t# Restart NGINXsudo 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.


Original Link: https://dev.to/ranjan/deploy-multiple-nodejs-apps-on-single-server-with-ssl-nginx-pm2-part-2-f48

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To