Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
June 27, 2022 10:09 am GMT

How to Setup Nodejs Mongodb in Production server on Ubuntu 18.04 in AWS, GCP, Azure, Digital Ocean Cloud Instance or Locally

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 have to use Infrastructure as Code System such as Terraform, or Configuration Management System Chef, Ansible, Puppet; 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 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 Groupusermod -aG sudo sammy# Testing sudo Accesssu - sammysudo ls -la /rootecho "----------------------  GIT----------------------"# install curlsudo apt install curl -y# install gitsudo apt-get install -y gitecho "----------------------  NODE & NPM----------------------"## You may also need development tools to build native addons:sudo apt-get install gcc g++ make -ywget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bashnvm ls-remotenvm install 14nvm 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 nodejsecho "----------------------  MONGODB----------------------"# import mongodb 4.0 public gpg keysudo 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 mongodbecho "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 databasesudo apt-get update# install the latest version of mongodbsudo apt-get install -y mongodb-org# start mongodbsudo systemctl start mongod# stop mongodbsudo systemctl stop mongod# Make a directory as root usersudo mkdir -p /data/db# Provide access to the directorysudo chown -R $USER /data/db# set mongodb to start automatically on system startupsudo systemctl enable mongod# stop mongodb to start automatically on system startupsudo systemctl disable mongod# install local replication-set driver for nodejssudo 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  --syslogecho "----------------------  PM2----------------------"# install pm2 with npmnpm install -g pm2# set pm2 to start automatically on system startuppm2 startup systemd# make current user the owner of the pm2 log home dirsudo 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.shecho "----------------------  NGINX----------------------"# install nginxsudo apt-get install -y nginx# You can make the currrent $USER the owner of that directorysudo chown -R $(whoami):$(whoami) /var/www# set the appropriate permissionschmod 755 -R /var/wwwecho "----------------------  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 enableecho "----------------------  NETWORK TESTING TOOL----------------------"# curl toolsudo apt  install httpie -ysudo apt updatesudo apt install redis-server -y# # comment out `supervised no` and set `supervised systemd`sudo nano /etc/redis/redis.conf# > supervised systemd# restart redis serversudo systemctl restart redis.serviceecho "----------------------  SET UP LETS-ENCRYPT----------------------"# Instal CertBotcurl -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 nginxsudo nginx -tsudo systemctl reload nginx# create the nginx default configurationnano  default # paste the content below## start  # website serverserver {    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 serverserver {    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 serverserver {    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 serverserver {    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/defaultsudo mv default  /etc/nginx/sites-available/default# Set up Certbot to obtain SSL certificatessudo 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

Original Link: https://dev.to/nditah/how-to-setup-nodejs-mongodb-in-production-server-on-ubuntu-1804-in-aws-gcp-azure-digital-ocean-cloud-instance-or-locally-16hk

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