NodeJS NGINX MongoDB Git CentOS

NodeJS, NGINX, MongoDB and Git Installation on CentOS 6.4

First of all, I'm gonna start by saying that I am not a linux pro, so bare with me if something here is wrong and let me know in a comment. This is just the way I do the setup for my NodeJS server, which will need access to github(since I want the app to auto pull from github whenever I push something from my dev), NGINX to handle the static content, MongoDB, and a couple other things I use. Here is a link to my shell script that will do the whole setup automatically (you're gonna have to run it as root tho), if you don't feel like following this post.

I'm going to use the smallest droplet(512MB ram, 1 core processor, 20GBSSD Disk, and 1TB of transfer) from Digital Ocean with CentOS 6.5 x64. I think this will be enough to handle the amazing amount of traffic that my apps will have. :)

Server Setup

The first thing to do, when you get a fresh new server, will be to update all installed software to the latest versions. I’m going to use YUM package manager. Aditionally, I use wget to download my packages and binaries in linux, so we install it here. This could take a couple of seconds...

yum update -y  
yum install wget  

Now, we are going to need a place to save whatever we download and a place for our sites or apps, so... let’s create them.

mkdir -p /data/tmp/  
mkdir -p /webroot/  
cd /data/tmp/  

Git Installation

I always use github hooks to maintain my server with the latest code in my repos. And installing git is as easy as the following command:

yum -y install git  

MongoDB Installing

For MongoDB, we need to create a configurations file for YUM to know where to find the latest MongoDB's packages. The file needs to be at /etc/yum.repos.d/mongodb.repo and it should contain something like:

name=MongoDB Repository  

Note that I’m using the 64-bit repo here, but you can easily change the baseurl to to get the 32-bit version.

Now that we have the configuration file in place, let’s do the actual installation via YUM. By default, Mongo stores data in /data/db/ directory, so we need to create it. Then we are going to start mongod service and make it start automatically when the server starts.

yum -y install mongo-10gen mongo-10gen-server  
mkdir -p /data/db/  
service mongod start  
chkconfig mongod on  

Installing NodeJS

In order to install NodeJS, we are going to need some tools to build the NodeJS source, so let’s install them via YUM.

yum -y groupinstall "Development Tools"  

Now, we need to download NodeJS source, which is a compressed file. We need to uncompressed it and prepare the compiler by reading the properties of our system. At the time of this writing, NodeJS latest stable version is v0.10.26.

tar zxf node-v0.10.26.tar.gz  
cd node-v0.10.26  

After having done this, the next step is to actually compile the NodeJS source. This process takes somewhere around ~4 minutes, depending on your connection and system.


The last step for NodeJS installation is to make it available system-wide, we do this with the following command:

make install  

After having NodeJS installed, we have NPM available to us. I recommend downloading the following packages that are pretty usefull to node: Supervisor and Forever. Supervisor can watch file changes to restart the server automatically. Forever lets you run node apps as a daemon and restarts the server if something goes wrong and stops the app.

npm install supervisor -g  
npm install forever -g  

Install NginX and Start the service

Lastly, NGINX. we are going to access the /data/tmp/ directory again and download the repo and add it to YUM. After having the repo, we are going to install it, start the NGINX service and make it start automatically whenever the server's system starts.

wget -P /data/tmp/  
rpm -ivh $TEMP_DIR/nginx-release-centos-6-0.el6.ngx.noarch.rpm  
yum -y install nginx  
service nginx start  
chkconfig nginx on  

NGINX needs a connector to run node on port 80. By default, NGINX will add all files with .conf in the directory /etc/nginx/conf.d/. Create a file at /etc/nginx/conf.d/virtual.conf and add the following:

server {  
    server_name  [your_domain];
    root /webroot/[your_domain or webapp name];
    access_log  /var/log/nginx/[your_domain or webapp name].log;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header HOST $http_host;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass[port assigned to node];
        proxy_redirect off;

There are a couple of things to note here. server_name is your domain name. root is the directory to the node app. Then we add a proxy to the node URI with the port assigned in the node app.

So there you have it, a server running node apps with NGINX as a front webserver, a MongoDB local instance and git. Still needs some minor tweaks to be production ready, but this will give you the starting point. Leave a comment if you get an error or something doesn't work to see how we can solve it.

comments powered by Disqus