At Agira, Technology Simplified, Innovation Delivered, and Empowering Business is what we are passionate about. We always strive to build solutions that boost your productivity.

Setup Node.js, Nginx And MongoDB On Ubuntu 16.04

  • By Saravana
  • June 14, 2018
  • 2863 Views

This blog will show you how to install Node.js, nginx and mongodb on ubuntu 16.04 operating system including the configuration for production deployment.
Before setup, These following needs to be installed,
– Node.js
– MongoDB
– Nginx

Preparing Ubuntu Server

To start the setup process, we need to install software build tools on the server.

$ sudo apt-get update
$ sudo apt-get install build-essential libssl-dev

 
 

1. Install Node.js

You can install the latest version of Node.js using this link https://github.com/nodesource/distributions.

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

 

Run the below command to install Node.js

 

$ sudo apt-get install -y nodejs

 
Nodejs will have both nodejs and npm package, So we need not to install it separately. It will install both packages on the server.
And, check the versions of nodejs and npm to confirm the installation

$ nodejs -v

 
So the output screen will show you the version of Nodejs installed on your server.

v10.3.0

So the output screen will show you the version of Nodejs installed on your server.

$ npm -v

 
Now the output will show you the version of NPM installed on your server.

6.1.0

 
 

2. Install MongoDB

Add apt repo to install mongodb on ubuntu.

$ apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10

 
 

Create the Package list file for MongoDB

 

$ echo "deb http://repo.mongodb.org/apt/ubuntu "$(lsb_release -sc)"/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list

 

Update the Ubuntu packages

$ sudo apt-get update

 

Now Install MongoDB

 

$ sudo apt-get install -y mongodb-org
Freeze MongoDB version (Optional Step)
$ echo "mongodb-org hold" | sudo dpkg --set-selections
$ echo "mongodb-org-server hold" | sudo dpkg --set-selections
$ echo "mongodb-org-shell hold" | sudo dpkg --set-selections
$ echo "mongodb-org-mongos hold" | sudo dpkg --set-selections
$ echo "mongodb-org-tools hold" | sudo dpkg --set-selections

 
 

To start the mongo service

$ sudo service mongod start

 

To stop the mongo service

$ sudo service mongod stop

 
 

To check the status of mongo service

$ sudo service mongod status

Related: How to setup MongoDB sharding environment

 

3. Install Nginx

By default nginx apt repository is available in Ubuntu. So run below command to install nginx service.

$ sudo apt-get install nginx

After the installation check nginx service using,

$ sudo service nginx status

Configure Nodejs application into nginx

Now, Configure Nginx as a reverse proxy server for nodejs application.

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

 
Then, replace the below code in the nginx config file to set up Nodejs application

server {
listen 80 default_server;
listen [::]:80 default_server;
server_name www.agiratech.com.com;
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;
}
}

 
After adding configuration restart the nginx service to apply the configuration changes

$ sudo service nginx restart

 
Finally, We can see your nodejs application on the Server URL which is configured in nginx.
Similarly, Want to explore more about Technologies? Join us on the excursion of exploring Technologies, Agira Technologies one of the fastest growing Web and Mobile Development Company in India. Precisely, extends its research on Blockchain, Chatbot, Artificial Intelligence, IoT and lot more on the queue. Explore more about us on www.agiratech.com.
For more details reach us at info@agiratech.com

Saravana

An enthusiastic Tech Lead with 7 plus years of experience in Web development arena. Owns legitimate experience in Ruby, Ruby On Rails, AngularJs, DevOps. Golang, Another add on, This young tech freak never miss a chance to get his hands on planting and Gardening even in his busy weekends.