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.
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/
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
8) Next you probably want to install modules like express.js so I suggest you create a folder /home/ubuntu/web and type:
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.
- 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
- 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 - Then you create a folder (anywhere, but i suggest under /home/ubuntu). /home/ubuntu$ mkdir db
- Then mount it: /home/ubuntu$ sudo mount <device name> <folder>
e.g. /home/ubuntu$ sudo mount /dev/xvdc db - 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/
- 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 - After you install, mongo is running.. you may want to start your own mongo and not use their service:
$ sudo service mongodb stop - 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
- 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 - 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 - 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 - Also install NPM if not exist
$ sudo apt-get install npm
8) Next you probably want to install modules like express.js so I suggest you create a folder /home/ubuntu/web and type:
- $ sudo npm install express
or other modules.