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.

riow
Published in
5 min readNov 15, 2016

--

*This was tested and worked on an AWS EC2 server.

(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

Git (/ɡɪt/) is a version control system for tracking changes in computer files and coordinating work on those files among multiple people.
https://git-scm.com/

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

Node.js® is a JavaScript runtime built on Chrome’s V8 JavaScript engine.
https://nodejs.org/en/

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

We will use nvm to install Node.js because nvm can install multiple versions of Node.js and allow you to switch between them.

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 X.X.X

Go to https://nodejs.org/en/ to find the latest version of Node.

Installing Node.js also installs the Node Package Manager (npm) so you can install additional modules as needed.

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)"

This should display the following message that confirms the installed version of Node.js running.

Update NPM

npm is the package manager for JavaScript and the world’s largest software registry. Discover packages of reusable code — and assemble them in powerful new ways.
https://www.npmjs.com/

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

Install PM2

PM2 is a production process manager for Node.js applications with a built-in load balancer. It allows you to keep applications alive forever, to reload them without downtime and to facilitate common system admin tasks.
http://pm2.keymetrics.io/

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 "YOUR@EMAIL.COM"

Hit enter through the prompts.

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@GITLAB.COM:GROUP/PROJECT.GIT
# For a specific branch
git clone -b production GIT@GITLAB.COM:GROUP/PROJECT.GIT

You may need to run the command twice.

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

MongoDB is a free and open-source cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with schemas.
https://www.mongodb.com/

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 PID

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

Might be worth starting and stopping it a few times.

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

This next part is a reminder for myself.
I use a different port and IP address on development vs production server.
So I pass the command line argument “real” to set port to 3000 and IP to be requested by the server.

You pass command line arguments with --

sudo pm2 start server.js -- production

NO PM2

sudo node server production

--

--