September 26 2018

Setup Node.js + MongoDB Production Server on Ubuntu 18.04

The below scripts can be used to setup and configure a production Node.js + MongoDB Web Server from scratch on Ubuntu 18.04 with the following technologies:

  • Node.js 10.x & NPM
  • MongoDB 4.0
  • PM2
  • NGINX
  • UFW (Firewall)


Whole Script for Ubuntu 18.04 Production Web Server Setup

Here's the whole script for setting up a production Ubuntu 18.04 web server with Node, NPM, Mongo, PM2, NGINX and UFW.

Each section is broken up with a big echo label so it's easy to find in the console, and each line has a small comment to describe what it's for.

The script is also available on GitHub Gist at https://gist.github.com/cornflourblue/f0abd30f47d96d6ff127fe8a9e5bbd9f.

You can copy the script to your server or run the gist directly with the following command: curl https://gist.githubusercontent.com/cornflourblue/f0abd30f47d96d6ff127fe8a9e5bbd9f/raw/e3047c9dc3ce8b796e7354c92d2c47ce61981d2f/setup-nodejs-mongodb-production-server-on-ubuntu-1804.sh | sudo bash

#!/usr/bin/env bash

echo "
----------------------
  NODE & NPM
----------------------
"

# add nodejs 10 ppa (personal package archive) from nodesource
curl -sL https://deb.nodesource.com/setup_10.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

# set mongodb to start automatically on system startup
sudo systemctl enable 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

 

Separate Web Server Setup Scripts

Below are standalone scripts for installing each of the above pieces of the production web server separately.


Install Node 10.x & NPM on Ubuntu 18.04

This script adds Node.js 10.x to the local package database from NodeSource and then installs it with apt-get.

Available on GitHub Gist at https://gist.github.com/cornflourblue/b792ace8470d9b4c35fc72947cc32393.

Command to run gist directly: curl https://gist.githubusercontent.com/cornflourblue/b792ace8470d9b4c35fc72947cc32393/raw/c1a412e2e61f31d5edabc6accb3798e2548b845a/install-node-10x-npm-on-ubuntu-1804.sh | sudo bash

#!/usr/bin/env bash

echo "
----------------------
  NODE & NPM
----------------------
"

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

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


Install MongoDB 4.0 on Ubuntu 18.04

This script installs MongoDB 4.0 and configures it to start automatically on system startup and reboot.

Available on GitHub Gist at https://gist.github.com/cornflourblue/34e2e2b14979198151493cf893b0697d.

Command to run gist directly: curl https://gist.githubusercontent.com/cornflourblue/34e2e2b14979198151493cf893b0697d/raw/a5a1fff60c9d1c3f4386909b64955186b56790f8/install-mongodb-40-on-ubuntu-1804.sh | sudo bash

#!/usr/bin/env bash

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

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


Install PM2 on Ubuntu 18.04

This script installs PM2 from NPM and configures it to start automatically on system startup and reboot. PM2 is a production process manager for Node.js.

NOTE: NPM is required before installing PM2, see the above script for installing Node & NPM if you haven't got them installed yet.

Available on GitHub Gist at https://gist.github.com/cornflourblue/91fe14bf4803f92eef662d7ded89e916.

Command to run gist directly: curl https://gist.githubusercontent.com/cornflourblue/91fe14bf4803f92eef662d7ded89e916/raw/ce16f128fbc01746f65bd48cb41c00233d1aafef/install-pm2-on-ubuntu-1804.sh | sudo bash

#!/usr/bin/env bash

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

# install pm2 with npm
sudo npm install -g pm2

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


Install NGINX on Ubuntu 18.04

This script installs the NGINX web server / reverse proxy server. By default NGINX is configured to start at system startup and reboot.

Available on GitHub Gist at https://gist.github.com/cornflourblue/d1b48e0ddc66d6c69f9115aabe46d818.

Command to run gist directly: curl https://gist.githubusercontent.com/cornflourblue/d1b48e0ddc66d6c69f9115aabe46d818/raw/5c69b716d4530f7f4aaf8c38eca2b31bb1060aed/install-nginx-on-ubuntu-1804.sh | sudo bash

#!/usr/bin/env bash

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

# install nginx
sudo apt-get install -y nginx


Configure UFW (Firewall) on Ubuntu 18.04

This script configures UFW (Uncomplicated Firewall) to allow SSH and web traffic (HTTP & HTTPS) to the server, then enables the firewall.

Available on GitHub Gist at https://gist.github.com/cornflourblue/d7d1b42552287ad4afa3f579597e54d5.

Command to run gist directly: curl https://gist.githubusercontent.com/cornflourblue/d7d1b42552287ad4afa3f579597e54d5/raw/59363792ad2e0a4b075cdbb9f509902939ac92a8/configure-ufw-on-ubuntu-1804.sh | sudo bash

#!/usr/bin/env bash

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


(Optional) Install MEANie CMS and Blog on Ubuntu 18.04

This script installs MEANie, the MEAN Stack CMS and blogging platform that I built to run this blog. It's a good option for testing your new server because it uses all of the pieces that you installed and configured above (Node, Mongo, PM2 & NGINX).

Once MEANie is installed, open your web browser and go to the IP address or domain name of your server, enter a username and password to create the admin user and then login to access the CMS. To access the blog front end go to the root IP address or domain name of your server again.

Available on GitHub Gist at https://gist.github.com/cornflourblue/e8c070fe4a88c2360db11a8e5d5568d1.

Command to run gist directly: curl https://gist.githubusercontent.com/cornflourblue/e8c070fe4a88c2360db11a8e5d5568d1/raw/b7cd4ea19f001aad5028120a538919e3a1793ade/install-meanie-cms-blog-on-ubuntu-1804.sh | sudo bash

echo "
----------------------
  MEANIE
----------------------
"

# clone meanie project from github into /opt/meanie folder
sudo git clone https://github.com/cornflourblue/meanie /opt/meanie

# install npm packages for meanie
cd /opt/meanie/server && npm install

# start meanie with pm2
sudo pm2 start server.js

# configure nginx reverse proxy
sudo cat << EOF > /etc/nginx/sites-available/default
server {
  listen 80 default_server;
  server_name _;

  location / {
    proxy_pass http://localhost:3000;
  }
}
EOF

# restart nginx
sudo systemctl restart nginx

 

Web Development Sydney

Feel free to contact me if you're looking for a web developer in Sydney, I also provide remote contracting services for clients outside Sydney.

 


Sponsored by