How to Setup Nodejs Mongodb in Production server on Ubuntu 18.04 in AWS, GCP, Azure, Digital Ocean Cloud Instance or Locally
Tags: cloud, deploy, backend, how
Published: 2022-06-27 10:09:29

Talking of DevOps practices like infrastructure automation,there are lots of great tools out there for large enterprise applications. However, for small applications, it would be an overkill like using a sledge hammer for a fly. So why would I use Infrastructure as Code System such as [Terraform](https://learn.hashicorp.com/collections/terraform/aws-get-started), or Configuration Management System [Chef](https://docs.chef.io/chef_overview/), [Ansible](https://www.ansible.com/integrations/infrastructure), [Puppet](https://puppet.com/open-source/#osp); when I can simply fly with this 5mins installation guide 😜  (just kidding, I am learning them 📚)

I regularly update the installation steps so get the [Github gist](https://gist.github.com/Nditah/6d619b2370d20f34949a7e249f4d6d94) for the most recent. 

📝 _still drafting the article_

```
#!/usr/bin/env bash

# Steps to write and execute a script
# Open the terminal. Go to the directory where you want to create your script.
# Create a file with . sh extension.
# Write the script in the file using an editor.
# Make the script executable with command chmod +x <fileName>.
# Run the script using ./<fileName>.

echo "
----------------------
  Adding a New User to the System 'Sammy'
----------------------
"
adduser sammy

# enter all the prompted info

# Step 3 — Adding the User to the sudo Group

usermod -aG sudo sammy

# Testing sudo Access
su - sammy
sudo ls -la /root


echo "
----------------------
  GIT
----------------------
"
# install curl
sudo apt install curl -y

# install git
sudo apt-get install -y git


echo "
----------------------
  NODE & NPM
----------------------
"
## You may also need development tools to build native addons:
sudo apt-get install gcc g++ make -y

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash

nvm ls-remote

nvm install 14

nvm alias default  14.15.0

# add nodejs 14 ppa (personal package archive) from nodesource
# curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -

# install nodejs and npm
# sudo apt-get install -y nodejs


echo "
----------------------
  MONGODB
----------------------
"

# import mongodb 4.0 public gpg key
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4

# create the /etc/apt/sources.list.d/mongodb-org-4.0.list file for mongodb
echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list

# reload local package database
sudo apt-get update

# install the latest version of mongodb
sudo apt-get install -y mongodb-org

# start mongodb
sudo systemctl start mongod

# stop mongodb
sudo systemctl stop mongod

# Make a directory as root user
sudo mkdir -p /data/db

# Provide access to the directory
sudo chown -R $USER /data/db


# set mongodb to start automatically on system startup
sudo systemctl enable mongod

# stop mongodb to start automatically on system startup
sudo systemctl disable mongod

# install local replication-set driver for nodejs
sudo npm install --unsafe-perm --verbose -g run-rs -f

# start mongodb replica set
# run-rs --mongod --keep --shell --dbpath /home/user/data"

# start mongod as a background process
 mongod  --fork  --syslog

echo "
----------------------
  PM2
----------------------
"

# install pm2 with npm
npm install -g pm2

# set pm2 to start automatically on system startup
pm2 startup systemd

# make current user the owner of the pm2 log home dir
sudo chown -R $(whoami):$(whoami) /home/ubuntu/.pm2

# create a shell script replica.sh
$ nano replica.sh
    #!/bin/bash
    run-rs --mongod --keep --shell --dbpath /data/db

$ pm2 run replica.sh


echo "
----------------------
  NGINX
----------------------
"

# install nginx
sudo apt-get install -y nginx

# You can make the currrent $USER the owner of that directory
sudo chown -R $(whoami):$(whoami) /var/www

# set the appropriate permissions
chmod 755 -R /var/www


echo "
----------------------
  UFW (FIREWALL)
----------------------
"

# allow ssh connections through firewall
# sudo ufw allow OpenSSH

# allow http & https through firewall
# sudo ufw allow 'Nginx Full'

# enable firewall
# sudo ufw --force enable

echo "
----------------------
  NETWORK TESTING TOOL
----------------------
"

# curl tool
sudo apt  install httpie -y
sudo apt update
sudo apt install redis-server -y


# # comment out `supervised no` and set `supervised systemd`
sudo nano /etc/redis/redis.conf
# > supervised systemd


# restart redis server
sudo systemctl restart redis.service


echo "
----------------------
  SET UP LETS-ENCRYPT
----------------------
"
# Instal CertBot
curl -o- https://raw.githubusercontent.com/vinyll/certbot-install/master/install.sh | bash

# Open the server block file for your domain using nano or your favorite text editor:
sudo nano /etc/nginx/sites-available/example.com
#server_name example.com www.example.com;

# test and restart nginx
sudo nginx -t
sudo systemctl reload nginx

# create the nginx default configuration

nano  default 
# paste the content below

## start 📥 

# website server
server {
    server_name example.com www.example.com;
    root /var/www/html/web/build;
    index index.html;
    location / {
        try_files $uri$args $uri$args/ /index.html;
    }
}
# admin console server
server {
    server_name admin.example.com;
    root /var/www/html/admin/dist;
    index index.html;
    location / {
        try_files $uri$args $uri$args/ /index.html;
    }
}
# demo or documentation server
server {
    server_name developers.example.com;
    root /var/www/html/backend/doc;
    index index.html;
    location / {
        try_files $uri$args $uri$args/ /index.html;
    }
}
# backend api server
server {
    server_name api.example.com;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $remote_addr;
    location / {
        proxy_pass http://localhost:5000;
        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;
        proxy_connect_timeout       600;
        proxy_send_timeout          600;
        proxy_read_timeout          600;
        send_timeout                600;
    }
}

## end 📤

sudo rm  /etc/nginx/sites-available/default
sudo mv default  /etc/nginx/sites-available/default


# Set up Certbot to obtain SSL certificates
sudo certbot --nginx -d example.com -d www.example.com  -d api.example.com  -d dev.example.com   -d developers.example.com -d admin.example.com

# To test the renewal process, you can do a dry run with certbot:
sudo certbot renew --dry-run

```