step-by-step tutorial to setup and run your Node.js server on Amazon EC2 instances

Part one: Setup an EC2 Instance

Select a region for your sever from top right corner of the page and click on Lunch Instance:

Lunch Instance

* please note that there are different pricing for different regions; for full details visit here.

From the list of Amazon Machine Images select one of the Ubuntu Images.

Select Ubuntu

Ubuntu LTS stands for Long Term Support but you can choose the latest version instead.

Next step is to select a type for your instance. The Instance types start from Micro to Extra large instance. Each type is optimized with different computing power and memory allocations to suit different needs. However for a simple Node.js application a micro instance is more than sufficient.

Amazon Instance Types

Click on review and lunch button. Check out the final details then click on lunch button. you will be prompted to select a key pair with a pem format.

Select a Key pair

The key pair is later used to authenticate and access the EC2 instance.You can choose an existing key or create a new key. You must make sure to save the key in a known and confidential place for your later use.

After a few minutes the instance will be in running status. You should be able to see your new instance under instances list. Select your new instance and view its public DNS. you can use the public DNS to connect to your server via ssh.

place of public DNS

Part two: Connect to your EC2 server with putty

For windows users, download putty and puttygen from here. We gonna use the puttygen to convert Amazon’s .pem key to standard .ppk file. Start the puttygen and click on load.

puttygen

Select your .pem key obtained from previous steps and click on OK. Now click on save private key and save the new file somewhere.

Open putty and enter your public DNS in Host Name field and leave the port as default 22.

configuring putty session

From left menu in putty expand SSH and select Auth. Now select brows on the right side and find your .ppk file.

Adding private key

Finally click open. When the connection establishes the putty will ask to Log in as a user. For a Ubuntu Instance the default username is ubuntu. Enter ubuntu and press enter.

Congratulation, you have now connected to your EC2 instance and ready to configure your server or install new software.

Part three: Installing Node.js on your EC2 instance

After connecting to your EC2 server for the first time, it is best practice to update your Ubuntu system before use. Enter following commands using Putty Shell. The upgrade might take some time.sudo apt-get updatesudo apt-get -y upgrade

Now we have to install three essential tools for Node.jssudo apt-get install build-essentialsudo apt-get install libssl-devsudo apt-get install git-core

Install rcconf service utility. This will help us to manage services more easier.sudo apt-get install rcconf

Finally we get to install Node.js.wget http://nodejs.org/dist/node-latest.tar.gztar xzf node-latest.tar.gzcd node-v0.10.22./configure --prefix=/usrmakesudo make install
Note the make command will take long time to finish.

It is time to install node package manager NPM from github.cd ~git clone http://github.com/isaacs/npm.gitcd npmsudo make install

Now you can install any Node package that you need for your project. I choose some of the good ones here. Each package has different functionality, if you are not familiar with them just Google them for more info.cd ~sudo npm install express ejs express-resource connect futures emailjs

It is best practice to have your Node.js running behind a proxy server such as nginx for extra security and usability. I choose nginx because it is open source and fairly mature and used by many famous companies. Enter following command to install nginx.sudo apt-get install nginx

We need to edit the default nginx configuration to forward any incoming request to your Node.js server.sudo vi /etc/nginx/sites-enabled/default
Using your arrow keys scroll down to “location / {“.

nginx configuration

and add the following after try_files line:
proxy_pass http://127.0.0.1:3000/;
Please note that above port number must be whatever port you are using for Node.js server which in my case is 3000. And restart nginx:sudo /etc/init.d/nginx restart

We can test our app by entering the public DNS is a browser and we should see the “Hello World” message. 🙂

Part four: Running your Node.js forever

The last part of this tutorial is to make sure our Node.js app runs even after we close the putty connection, reboot or any unexpected crashes. For this we gonna use ‘supervisor’ application service. First stop the previous running Node.js app. Then to install supervisor use following commands:sudo apt-get install python-setuptoolssudo easy_install supervisor
Adding supervisor to the system services:curl https://gist.github.com/howthebodyworks/176149/raw/
88d0d68c4af22a7474ad1d011659ea2d27e35b8d/supervisord.sh > supervisord
giving it proper rights: chmod +x supervisord sudo mv supervisord /etc/init.d/supervisord

checking if supervisor is part of services using rcconfsudo rcconf
This will open the rcconf window as:

rcconf Interface

Make sure the box next to supervisord has a start, i.e, selected and running, then press tab to select OK and enter.

We need to configure the supervisord service.sudo echo_supervisord_conf > supervisord.confsudo mv supervisord.conf /etc/supervisord.conf
open the new configuration file:sudo vi /etc/supervisord.conf

under [unix_http_server] uncomment the chmod and change it to 0777 and uncomment user and change the value to ubuntu:

supervisorConfig

Move further down the file and find ;[program:theprogramname] section and add the following before it:[program:node]
command=node app.js
directory=/home/ubuntu/www
environment=NODE_ENV=production

Supervisor is basically a Linux script that run as a service and always checks that forever. Js is running to run the server . also rcconf is a way to configure supervisor to monitor any program that need to be running, in our case is the forever. js which runs the server

hey, first make sure the supervisord is running as part of services using command:
sudo rcconf
secondly check everything is correct in supervisord.conf file, such as app name.
third you must restart supervisord after any changes to the config file.
and finally you can look into /tmp/supervisord.log file to check for any errors.
Happy Debugging 🙂