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

START

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
# [ENTER]
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- https://raw.githubusercontent.com/creationix/nvm/v0.32.0/install.sh | bash

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

. ~/.nvm/nvm.sh

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/id_rsa.pub
pbcopy < ~/.ssh/id_rsa.pub

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.

ls

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://keyserver.ubuntu.com:80 — recv 0C49F3730359A14518585931BC711F9BA15703C6

2. Create a list file for MongoDB.

echo “deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu 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.

top

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

./END


You pass command line arguments with --

sudo pm2 start server.js -- production

NO PM2

sudo node server production

riow

Publications are personal

Rio Weber

Written by

Rio Weber

It doesn’t get better than this.

riow

riow

Publications are personal