Murvin Lai
  • Me
  • Career
  • Tech Blog
  • My Projects
  • Open Source
    • Install Node.js
    • Node.JS + MongoDB on AWS
    • Node.js + MongoDB + Mongoose
    • Node Crypto Performance
    • NPM
    • My Node Tips
    • Dev NoTe
    • Req and Res in Node.js
    • Remote HTTP Request
  • Foodies
    • My Menu
  • lO_Ok

Node.js + MongoDB on AWS EC2

Here are the steps to have your Node.js & MongoDB running on AWS EC2 with Ubuntu server. 

Steps for AWS:

1) Start an instance from an Ubuntu AMI. 
2) Add EBS volume in the popup options before creating the instance.
3) Make sure you have the right security group that opens up port 80 and 22. 
4) After the instance is created, you want to get the EBS mounted properly.  That's for storing the MongoDB data.  You don't want to store the data in the instance.   So, if anything wrong with the instance, you still have the data in EBS protected and mount to another instance later.
  1. First, in AWS panel, it will say the EBS is created on device /dev/sdc  or sd whatever.   The actual device name in Ubuntu will be xvd whatever, e.g. xvdc
  2. If it is a new EBS with no data,  you need to create the file system:  $ sudo mkfs -t ext4 <device name>
    <device name> is your EBS path, e.g. /dev/xvdc 
  3. Then you create a folder (anywhere, but i suggest under /home/ubuntu).  /home/ubuntu$ mkdir db
  4. Then mount it:  /home/ubuntu$ sudo mount <device name> <folder>   
    e.g. /home/ubuntu$ sudo mount /dev/xvdc db
  5. Now you have the db folder.  Then go inside the folder to create a subfolder "data" for holding the actual mongo Data
    /home/ubuntu$ cd db
    /home/ubuntu$ mkdir data

5) After that, let's install mongo before we do anything with node.  Follow this instruction: http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/
  1. In brief, you need to do the followings:
    $ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
    $ echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/10gen.list
    $ sudo apt-get update
    $ sudo apt-get install mongodb-10gen


  2. After you install, mongo is running.. you may want to start your own mongo and not use their service:
    $ sudo service mongodb stop

  3. Then run mongo from the db/data folder you just created:
    $ sudo /usr/bin/mongod --dbpath /home/ubuntu/db/data


6) Now, you have mongo running.  Next, we want to install node.  See: 
https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager

  1. In brief, you need to 
    $sudo apt-get install nodejs
    Or do that with older version of ubuntu
    $ sudo apt-get update
    $ sudo apt-get install python-software-properties python g++ make
    $ sudo add-apt-repository ppa:chris-lea/node.js
    $ sudo apt-get update
    $ sudo apt-get install nodejs

  2. If you have previous version of node, then it is quite troublesome, you need to uninstall the previous node first:
    $ sudo  rm -r bin/node bin/node-waf include/node lib/node lib/pkgconfig/nodejs.pc share/man/man1/node.1
  3. Node.js package may have naming conflict.  (you run as nodejs not node.).  in order to run as "node" in command line, do this:
    $ sudo ln -s /usr/bin/nodejs /usr/bin/node
  4. Also install NPM if not exist
    $ sudo apt-get install npm 

7) After that, you should have the latest node.js and npm installed.  
8) Next you probably want to install modules like express.js   so I suggest you create a folder /home/ubuntu/web  and type:
  1. $ sudo npm install express
    or other modules.
9) Now what? you want to use git to get your own project running on your new EC2, right?


Copyright @2012 Murvinlai.com