Simple Installation of Node.js and MongoDB

So you have got your AWS EC2 Amazon Linux instance running. You connected with ssh and everything looks like the old familiar territory. Now you have heard about all these wonderful technologies called Node.js, MongoDB, real-time web applications, Express.js, and what have you. There are gazillions of installation scripts and Stack Overflows in Google, but go figure.

I have distilled installation scripts that were tested to work.

Note that Amazon Linux is essentially the CentOS Linux distribution.

Node.js

Run the installation script install_node_npm.sh with:
sudo ./install_node_npm.sh

install_node_npm.sh

 # required gcc and g++
 yum install gcc
 yum install gcc-c++ compat-gcc-32 compat-gcc-32-c++

 echo 'export PATH=/local/bin:$PATH' >> ~/.bashrc
 . ~/.bashrc
 mkdir node-latest
 cd node-latest
 curl http://nodejs.org/dist/node-latest.tar.gz | tar xz --strip-components=1
 ./configure
 make install

This also installs NPM - the Node.js package manager.

MongoDB

To install MongoDB you first need to add a repository to yum, the Linux package manager. Create the file:

10gen.repo

[10gen]
   name=10gen Repository
   baseurl=http://downloads-    
  distro.mongodb.org/repo/redhat/os/x86_64
   gpgcheck=0
   enabled=1

Run the installation script install_mongodb.sh with:
sudo ./install_mongodb.sh

install_mongodb.sh

cat 10gen.repo > /etc/yum.repos.d/10gen.repo
yum install mongo-10gen mongo-10gen-server

MongoDB Configuration

The configuration is in /etc/mongod.conf. To edit,

 sudo vi /etc/mongod.conf

The defaults are alright for the start. You should consider changing two important things:

  • dbpath - where the database is stored
  • logpath - where the log file is stored

If you change any of these, you need to change the file owner of the chosen folder. So if your new dbpath is /home/johndoe/data, do:

 chown -R mongod:mongod /home/johndoe/data

Running MongoDB

Run:

 sudo service mongod start

Stop:

 sudo service mongod stop