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.


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!

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


Other than the method of installing Node.js in the above tutorial, I choose to install my own Node.js env with nvm(

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

git clone git:// ~/nvm
cd ~/nvm

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


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

You can download the latest stable version of MongoDB from

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


You can refer to this page 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/;

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 {
        listen       80;

        #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.


You can simply git clone it to your EC2 instance then run it by node or you can choose to use 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


Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s