Deploying MEAN.js app to Ubuntu

In this short tutorial we will explain how we deployed our MEAN.js app on ubuntu. We used EC2 for hosting our app and the Ubuntu 12.04 as the OS

EC2

We are assuming you are familiar with how to spin up servers in Amazon EC2. In this tutorial we will not be explaining the basic server spin up and server configuration. If you need help with server setup — please refer to this article which gives an excellent step by step guidance.

http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-launch-instance_linux.html

MEAN JS

Let’s install the individual components one by one.

Installing MongoDB

  1. Import the public key used by the package management system.
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10

2. Create a list file for MongoDB.

echo "deb http://repo.mongodb.org/apt/ubuntu precise/mongodb-org/3.0 multiverse"

3. Reload local package database.

sudo apt-get update

4. Install the MongoDB packages.

sudo apt-get install -y mongodb-org

5. Start the service

sudo service mongod start

And in 5 easy steps, you have your mongo db server running and accepting connections.

For more detailed steps, read the guide here

https://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/

Installing NodeJS

Install NodeJS using the apt-get package by executing the following command

sudo apt-get install nodejs

It’s also a good idea to install Node Package Manager — npm. Just execute this command on linux shell

sudo apt-get install npm

Install Bower

Once you have installed npm, installing any node package is relatively straighforward. For instance, execute this command to install bower

npm install bower -g

Please note that you may have to use sudo if you get Permission Denied error.

Deploy the app

Note that we have 2 components of MEAN stack already installed i.e Node and MongoDB. The other two components are typically listed as dependencies in your package.json file and would be installed during the app deploy process itself.

In order to deploy code, either you can package your application as a npm package or to keep it simple, you can simply do git clone / checkout.

git clone <path to your git repository>

Next, install all the node dependencies

cd <application directory>
npm install

Install bower packages

bower install

Prepare your MEAN app to run in production mode

grunt build

Note that for Mean v.0.3, grunt build without NODE_ENV set to production would produce the minified assets required to run the app in production mode. You may see a warning that says NODE_ENV not set. Using default development, but you should ignore that.

Check public/dist/application.min.js and public/dist/application.min.css. These files should be populated after the build

Now our application is ready for deployment!

Install Forever

Before I show how to run your node app in behind nginx proxy, let me quickly add a note about forever.

Generally you never know when your web service is going to crash and die. Hence it’s prudent to use softwares like monit, supervisor or forever to ensure that the service is continuously running. If the process crashes for some reason then forever detects that and restarts a new process. The service keeps running until you explicitly tell forever to stop it.

To start the node server, execute this command

NODE_ENV=production PORT=8000 forever start server.js &

Now we are setting the NODE_ENV=production because we want node to serve the minified application JS and CSS files. Typically I would start 4 of these processes separately on ports 8000, 8001, 8002 and 8003. Now you have 4 node servers ready to serve traffic.

Installing NGINX

We will be using NGINX to load balance the requests between our node servers started in the previous step.

When a user comes to www.foo.com, nginx would accept the requests on port 80 and just proxy the request to the node servers in round robin fashion there by distributing the load equally (somewhat) to all the node servers.

The way of enabling that is just add the following lines in your nginx.conf file found in /etc/nginx/nginx.conf. This configuration goes into the http block of the config file.

upstream nodes {
server 127.0.0.1:8000;
server 127.0.0.1:8001;
server 127.0.0.1:8002;
server 127.0.0.1:8003;
keepalive 64;
}

Just restart nginx and we are ready to start serving traffic on our app.

Chat Applications

After starting nginx, we could access my app over the internet. However we noticed one problem. The application we had deployed had a web sockets based real time chat module. The socket connection was getting established but whenever the XHR request was sent to the URL corresponding to the socket id, it would fail with 404 status.

The reason is nginx. Remember we added a configuration to load balance the requests between node servers. So my application establishes a web socket connection with one of backend node processes but the next request goes to a new node process and hence we get back a 404.

The fix was simple once the problem was clear. Just had to enable sticky sessions in nginx. Now the configuration becomes, just add ip_hash to the upstream servers block.

upstream nodes {
ip_hash;
server 127.0.0.1:8000;
server 127.0.0.1:8001;
server 127.0.0.1:8002;
server 127.0.0.1:8003;
keepalive 64;
}

And that’s it folks. It’s ridiculously simple to deploy a MEAN.js app in production and get the application running thanks to awesome folks at meanjs.org

If you have any questions, please post it to our twitter handle — @PatronousTeam

If you are looking for a kick ass team to develop some mean applications for you, hit us up!

www.patronous.com

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.