Configure Express + MongoDB + Nginx on AWS EC2

Recently I found it’s super fast and simple to build your own website with Node.js + MongoDB.

And it is fun and pleasure to deploy your work to EC2 with Nginx as a reverse proxy.

Here I’m going to brief you how to make those amazing happen.

EC2

First thing first, you have to launch your own EC2 instance on AWS, feel free to search for tutorials in the internet if you don’t know how to do that, or you can simply take a look at this article, but remember to come back when you got your EC2 instance ready!

http://iconof.com/blog/how-to-install-setup-node-js-on-amazon-aws-ec2-complete-guide/#updateEC2

After you finished everything inside the article with EC2, there is one more step you need to do which is INSTALL git

sudo yum install gcc-c++ make
sudo yum install openssl-devel
sudo yum install build-essential
sudo yum install libssl-dev
sudo yum install git-core

Node.js

Other than the method of installing Node.js in the above tutorial, I choose to install my own Node.js env with nvm(https://github.com/creationix/nvm)

If you’ve already installed git, you can simply run the commands below.

git clone git://github.com/creationix/nvm.git ~/nvm
cd ~/nvm
./install.sh

I’m using the newest Node.js to do all the development, you can choose your prefered version.
You can switch between different Node.js version by using nvm commands.

nvm install 0.8.15
nvm use 0.8.15

or set default

nvm alias default 0.8.15

MongoDB

Next important thing is to set up MongoDB for your web application.

You can download the latest stable version of MongoDB from http://www.mongodb.org/downloads

P.S. You can download the source code and compile it by yourself, but here I’m only introducing you how to use the compiled version of MongoDB.

After you get the MongoDB binary, you can create your own dbpath for mongod to store data and your own logpath to record the behavior of mongod by typing,

 $ ./mongod --fork --logpath /var/log/mongodb.log --logappend --dbpath /var/mnt

Nginx

You can refer to this page http://wiki.nginx.org/Install to install Nginx on EC2.

And here is the configuration file for configure your Nginx with your Express web server.

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    gzip  on;

    upstream zouzhe {
    	server 0.0.0.0:3000;
        server 0.0.0.0:3001;
    }

    server {
        listen       80;
        server_name  www.mysite.com;

        #charset koi8-r;

        access_log  logs/host.access.log;

    	location / {
    		 proxy_set_header X-Real-IP $remote_addr;
    		 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    		 proxy_set_header Host $http_host;
    		 proxy_set_header X-NginX-Proxy true;
    		 proxy_pass http://zouzhe/;
    		 proxy_redirect off;
    	}
    }
}

 

Web program

Above is basically all the stuff you need to do if you want to deploying a Node.js web application on EC2.

But I didn’t tell you how to write a proper Node.js program to make it running on the platform we build.

So, I set up a sample node.js program for you to try on your own server.

https://github.com/reterVision/node-express-startup

 

You can simply git clone it to your EC2 instance then run it by node or you can choose to use forever(https://github.com/nodejitsu/forever).

 

I will post a detailed tutorial about how to write your own node.js program in the future 😛

If you have any questiones related to this blog, please tweet me @reterclose

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s