HOW TO HOST NodeJS APP IN UBUNTU 18.4

Install nodejs:

#apt update

#curl -sL https://deb.nodesource.com/setup_10.x -o nodesource_setup.sh

#bash nodesource_setup.sh

#apt install nodejs

Verify installation using below commands:

#node -v

#npm -v

Install pm2 process manager:

#npm i -g pm2 (Install globally)

Install mongoDB:

curl -fsSL https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add –

echo “deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.4 multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org-4.4.list

#apt update

#apt install mongodb-org

Verify mongo installation:

#Mongo –version

Start mongodb service:

#systemctl start mongod

#Enable mongodb service:

#systemctl enable mongod

Enter into mongo shell using below command:

mongo

Create a database:

use dbname

Create a db user as db owner:

db.createUser({user:”username”, pwd:”pa$$word”, roles:[{role:”dbOwner”, db:”dbname”}]})

Restore database from dump:

mongorestore –port=27017 –username=user –password=”pa$$word” –db dbname  backup_file_name

Change the new db credentials in the files appropriately.

Now we can start the application components using pm2:

This application (afrocamgist.com) has 4 components (Desktop and Mobile frontend, API and CDN). The commands to start these components are given below:

#cd /frontend/desktop/react-seo-server

pm2 –log-date-format ‘DD-MM HH:mm:ss.SSS’ start npm –name AFRO_DT_FRONT — run start server.js

#cd /frontend/mobile

pm2 –log-date-format ‘DD-MM HH:mm:ss.SSS’ start npm –name AFRO_MOB_FRONT — run start server.js

#cd /api/afrou-api

pm2 –log-date-format ‘DD-MM HH:mm:ss.SSS’ start npm –name AFRO_API_SERVER — run start

pm2 –log-date-format ‘DD-MM HH:mm:ss.SSS’  start –name AFRO_CDN cdn.js

To check the running services:

#pm2 list

Now we can setup reverse proxy for proxy passing port 80 into corresponding node ports:

For desktop frontend:

#vi /etc/nginx/sites-enabled/afro.conf

server {

    listen 80;

    server_name afro.spotfixcrew.com;

    location / {

              proxy_pass http://localhost:8081;

    }

}

Save and close the file.

For mobile frontend:

vi /etc/nginx/sites-enabled/m.afro.conf

server {

           listen 80;

server_name m.afro.spotfixcrew.com;

    location / {

              proxy_pass http://localhost:8080;

    }

}

Then restart the nginx server:

#systemctl restart nginx

Call the website form the browser:

Now we can install ssl certificates on the domains:

First add the repository:

#add-apt-repository ppa:certbot/certbot

Install Certbot’s Nginx package with apt:

#apt install python-certbot-nginx

Now we can obtain ssl certificates using the below command:

#certbot –nginx

You need to provide an email address and agree to terms of service. Select which domain you want to enable ssl. Leave the space blank for selecting all the domains hosted in the server.

Then select anyone of ‘No redirect’ or ‘Redirect’.

That’s it! Now the domains are ssl encrypted.

You May Also Like

About the Author: admin

Leave a Reply

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