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.
*This was tested and worked on a AWS EC2 server.
Log into the server. (ONLY FOR AWS)
Set permissions for AWS.
chmod 400 AWS_devKey.pem
Always start with an update:
sudo apt update
Create folder for project:
Git (/ɡɪt/) is a version control system for tracking changes in computer files and coordinating work on those files among multiple people.
sudo apt-add-repository ppa:git-core/ppa2.14.1 or higher
sudo apt update
sudo apt install git
# Check GIT version
# Should be
1. Install node version manager (nvm) by typing the following at the command line.
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.
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.
X sudo apt install npm
# IGNORE ERROR 'Some packages could not be installed. Thi...'
npm install npm@latest -g
# Check version.
# Should be 5.5.1 or higher.
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.
This is the Process Manager you should use. There are others (nodemon), this one is for production.
npm install pm2@latest -g# Check version.
# Should be 2.7.2 or higher.
# Follow Instructions
# Free Node Monitoring Service
Generating a new SSH key
ssh-keygen -t rsa -b 4096 -C "
Hit enter through the prompts.
Get SSH Public Key.
pbcopy < ~/.ssh/id_rsa.pub
Paste key into repo host Deploy Keys.
Clone the Production!
# 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
See if the folder
node_modules has been created.
Make sure Express is installed.
npm ls | grep express
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.
Due to a lock elision bug present in older versions of the package on Ubuntu 16.04 for POWER, you must upgrade the…docs.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
The MongoDB 3.4 log messages you have highlighted are startup warnings, not errors. These warnings are intended to…askubuntu.com
See if mongod is running.
If it is, find PID for mongod from
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.
Might be worth starting and stopping it a few times.
Check output from Mongo log file.
sudo service mongod start
# To stop Mongod
# sudo service mongod stop
PM2 Set Up
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
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
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
sudo node server production