Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save KtanPatel/d7f070897db59b308f50f9662dadabfe to your computer and use it in GitHub Desktop.
Save KtanPatel/d7f070897db59b308f50f9662dadabfe to your computer and use it in GitHub Desktop.
Setup Node.js + MongoDB + PM2 Production Server on Ubuntu 20.04
#!/usr/bin/env bash
echo "
----------------------
NODE & NPM
----------------------
"
# add nodejs LTS ppa (personal package archive) from nodesource
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
# install nodejs and npm
sudo apt-get install -y nodejs
# install yarn
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt-get update && sudo apt-get install --no-install-recommends -y yarn
# install build essentials
sudo apt-get install -y build-essential wget gnupg
echo "
----------------------
MONGODB
----------------------
"
# import mongodb 7.0 public gpg key
curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | \
sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg \
--dearmor
apt-key list
# create the /etc/apt/sources.list.d/mongodb-org-7.0.list file for mongodb
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.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
# set mongodb to start automatically on system startup
sudo systemctl enable mongod
# status mongodb
# sudo systemctl status mongod
echo "
----------------------
PM2
----------------------
"
# install pm2 with npm
sudo npm install -g pm2
# set pm2 to start automatically on system startup
sudo pm2 startup systemd
echo "
----------------------
NGINX
----------------------
"
# install nginx
sudo apt-get install -y nginx
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment