Set up a new Node.js Production Server on Ubuntu

This is my attempt to detail every step in setting up a new Production Server running Node.js using Express with MongoDB on Ubuntu.

Rio Weber
Rio Weber in riow
Nov 15, 2016 · 5 min read

(ONLY FOR AWS) Log into the server. Set permissions for AWS.

chmod 400 AWS_devKey.pem


Always start with an update:

sudo apt update

Create folder for project:

mkdir ~/production
cd ~/production

Install GIT

sudo apt-add-repository ppa:git-core/ppa
sudo apt update
sudo apt install git
# Check GIT version
git --version
# Should be 2.14.1 or higher

Install Node

1. Install node version manager (nvm) by typing the following at the command line.

curl -o- | bash

2. Activate nvm by typing the following at the command line.

. ~/.nvm/

3. Use nvm to install the version of Node.js you intend to use by typing the following at the command line.

nvm install 

4. Test that Node.js is installed and running correctly by typing the following at the command line.

node -e "console.log('Running Node.js '+ process.version)"

Update NPM

npm install npm@latest -g
# Check version.
npm -v
# Should be 5.5.1 or higher.

Install PM2

This is the Process Manager you should use. There are others (nodemon), this one is for production.

npm install pm2@latest -g
# Check version.
pm2 -v
# Should be 2.7.2 or higher.
pm2 startup
# Follow Instructions
pm2 monitor
# Free Node Monitoring Service

Generating a new SSH key

ssh-keygen -t rsa -b 4096 -C ""

Get SSH Public Key.

cat ~/.ssh/
pbcopy < ~/.ssh/

Paste key into repo host Deploy Keys.

Clone the Production!

git clone git clone -b production 

Install all Project dependencies

Install Modules

npm install

See if the folder node_modules has been created.


Make sure Express is installed.

npm ls | grep express

Install MongoDB

1. Import the public key used by the package management system.

sudo apt-key adv — keyserver hkp:// — recv 0C49F3730359A14518585931BC711F9BA15703C6

2. Create a list file for MongoDB.

echo “deb [ arch=amd64,arm64 ] xenial/mongodb-org/3.4 multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list

3. Reload local package database.

sudo apt update

4. Install the MongoDB packages.

sudo apt-get install -y mongodb-org

*MongoDB Start Up Warnings

Start MongoDB

See if mongod is running.


If it is, find PID for mongod from top.

sudo kill -9 

X Create required folder for MongoDB — (where the database is stored).

X sudo mkdir -p /data/db
X sudo chown -R $USER /data/db

Start MongoDB so it can set up.

sudo mongod

Check output from Mongo log file.

cat /var/log/mongodb/mongod.log

Run MongoDB

sudo service mongod start
# To stop Mongod
# sudo service mongod stop

PM2 Set Up

Start Server

pm2 start server.js

Ubuntu Security Updates

Install important Security Updates.

X sudo unattended-upgrades -d

Port Opening and Accepting

X sudo iptables -I INPUT 1 -p tcp --dport 8080 -j ACCEPT
X sudo iptables -I INPUT 1 -p tcp --dport 8443 -j ACCEPT

Port Forwarding

This is important because port 80 is a “reserved” port. This script forwards port 80 to port 8080. You can make it port 8080.

sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080

Forward 443 for HTTPS.

sudo iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 443 -j REDIRECT --to-port 8443


You pass command line arguments with --

sudo pm2 start server.js -- production


sudo node server production


Publications are personal

Rio Weber

Written by

Rio Weber

It doesn’t get better than this.



Publications are personal