We are using Grammarly to help us writing this article

Setup Node.js, Nginx and MongoDB on Ubuntu 16.04 for Production

by Didin J. on Jan 13, 2018 Setup Node.js, Nginx and MongoDB on Ubuntu 16.04 for Production

Step by step tutorial of setup Node.js and Nginx Web Server for Production from Scratch in Ubuntu Server 16.04

Step by step tutorial of setup Node.js and Nginx Web Server for Production from Scratch in Ubuntu Server 16.04. This tutorial can apply to unmanaged VPS or Infrastructure As a Service (IAS) which everything set up on your own. For managed VPS or Platform As a Service (PAAS) cloud you can use Google Cloud Platform, Heroku, AWS Beanstalk, Redhat Openshift, etc that no need more setup. We are using the latest common Ubuntu Server 16.04.

The following tools, software and modules are required for this tutorial:

- VPS with Ubuntu Server 16.04
- Node.js
- Nginx
- MongoDB
- Git Repository
- PM2
- SSH connection to the VPS

Server or VPS specification for this tutorial is single CPU, 1 GB RAM and 8 GB HDD. You can find cheap VPS with larger specification from GreenGeeks.com or A2 Hosting. We assume that you already purchase VPS with Ubuntu 16.04 installed. Next, we can start the steps of this tutorial.

1. Create a Swap File

For limited VPS resource (eg. 1 GB RAM), we suggest to set swap file on Ubuntu Server. To check if swap file is exists, connect to your server via SSH. On the terminal, just run this command.

ssh [email protected]

Enter the password for the user if it asking for password. If you are using key pair, type this command.

ssh -i your-keypair.pem [email protected]

After login to your VPS using SSH, type this command for checking the swap file.

sudo swapon --show

Next, check RAM and Swap space.

free -h

Before setting a swap, check your hard disk space by this command.

df -h

If there's enough available space, you can create a swap file. For example, we create 1 GB swap file.

sudo fallocate -l 1G /swapfile

Verify the amount of swap file that just created.

ls -lh /swapfile

Next, enable the swap file. But first, make /swapfile accessible to "root".

sudo chmod 600 /swapfile

Check permission changes.

ls -lh /swapfile

You should see permission like this.

-rw------- 1 root root 1.0G Jan 23 01:44 /swapfile

Next, mark the swap space by this command.

sudo mkswap /swapfile

Next, make enable the swap file.

sudo swapon /swapfile

Verify available swap.

sudo swapon --show

Check again swap space by this command.

free -h

To make swap permanent, add this file information to "/etc/fstab".

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

2. Install MongoDB for Ubuntu 16.04

To install MongoDB server on Ubuntu, we have to add the official repository first. Type this command to add the repository.

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927

Type this command to create the list file for MongoDB.

echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list

Next, update the `apt` package list.

sudo apt-get update

Now, we can install MongoDB using `apt`. Type this command to install it.

sudo apt-get install -y mongodb-org

Run the MongoDB service using this command.

sudo service mongod start

To check MongoDB status, type this command.

sudo service mongod status

You will see below result. To close it just press `ctrl`+`c` key.

Setup Node.js, Nginx and MongoDB on Ubuntu 16.04 for Production - MongoDB Status

To run MongoDB service automatically when the server starts types this command.

sudo systemctl enable mongod && sudo systemctl start mongod

3. Install Node.js

We will install latest Node.js from NodeSource https://github.com/nodesource/distributions. Type this command to get installation script from the repository.

curl -sL https://deb.nodesource.com/setup_9.x | sudo -E bash -

Now, install Node.js using this command.

sudo apt-get install -y nodejs

For testing purpose, we will use our existing MEAN (MongoDB, Express.js, Angular 5 and Node.js) web application from our GitHub https://github.com/didinj/mean-stack-angular5-crud. Clone the source code from the repository.

git clone https://github.com/didinj/mean-stack-angular5-crud.git mean-crud

Next, go to the cloned directory.

cd ./mean-crud

Install all required modules by type this command.

npm install

To start the app type this command.

npm start

That will run MEAN stack app using port 3000. To close it just press CTRL+C button.

4. Install PM2

PM2 is tools for manage Node.js process. To install it, type this command.

sudo npm install -g pm2

To start the app using PM2 you should first build the Angular 5 using this command.

npm run build

Then run the MEAN app using PM2.

pm2 start bin/www

You should see this PM2 status.

Setup Node.js, Nginx and MongoDB on Ubuntu 16.04 for Production - PM2 Status

To stop the PM2 type this command.

pm2 stop 0

The `0` parameter is app id that you can see in above PM2 status table. Next, to make the PM2 start and run the app every time server starting type this command.


sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u ubuntu --hp /home/ubuntu

`ubuntu` is your server username. To check the status of startup system, type this command.

systemctl status pm2-ubuntu

You can see this log or output console of PM2 service.

Setup Node.js, Nginx and MongoDB on Ubuntu 16.04 for Production - PM2 Service status

Now, the MEAN app is running and managed by PM2.

5. Install and Configure Nginx as the Reverse Proxy Server

Now, install Nginx as the reverse proxy server for MEAN app. Type this command to install it.

sudo apt-get install nginx

To redirect all request to MEAN app, open and edit Nginx configuration file.

sudo nano /etc/nginx/sites-available/default

Replace location body with this.

location / {
    proxy_pass http://localhost:3000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;

Save it by press CTRL+O then exit the Nano editor by press CTRL+X. To test Nginx configuration, type this command.

sudo nginx -t

If you see below response, then you can restart Nginx server.

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

To restart Nginx type this command.

sudo service nginx restart

Don't forget to start the MEAN app if not running yet.

pm2 start 0

Finally, you can see your MEAN stack app on the live cloud server publicly by accessing your VPS URL (eg. http://ec2-52-90-182-190.compute-1.amazonaws.com)


That's it, this tutorial step using Amazon AWS EC2 T2.micro Instance with Ubuntu 16.04 LTS amd64 Image. You can find more cheap VPS on GreenGeeks.com or A2 Hosting.


The following resources might be useful for you: