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

Part one: Setup an EC2 Instance

 First you need to login into your Amazon AWS account and go to AWS Management Console.

click on EC2:


EC2 Option

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

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

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 InatanceTypes

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

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

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.



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

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

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 update
sudo apt-get -y upgrade

Now we have to install three essential tools for Node.js
sudo apt-get install build-essential
sudo apt-get install libssl-dev
sudo 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.
tar xzf node-latest.tar.gz
cd node-v0.10.22
./configure --prefix=/usr
sudo 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
cd npm
sudo 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

nginx configuration

and add the following after try_files line:

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

Now we are gonna write a simple Node.js app to test our server.

make a new directory for our app.
mkdir ~/www
cd ~/www
Open a new js file:
sudo vim app.js
add the following code:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n'); }).listen(3000, "");
console.log('Server running at');

Now run the Node.js app:
node app.js

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-setuptools
sudo easy_install supervisor
Adding supervisor to the system services:
88d0d68c4af22a7474ad1d011659ea2d27e35b8d/ > supervisord

giving it proper rights:
chmod +x supervisord
sudo mv supervisord /etc/init.d/supervisord

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


rcconf Interface

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.conf
sudo 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:



Move further down the file and find ;[program:theprogramname] section and add the following before it:
command=node app.js

Restart supervisord:
/etc/init.d/supervisord restart
Then reboot your EC2 via the management console.

You can replace the content of ~/www directory with any existing Node.js application you have. However, after any changes to Node.js app you need to restart the supervisord.

That’s it! congratulation, you have your Node.js app running on Amazon EC2 and It will be running as long as your server is up. I hope this was useful. Comment or subscribe for any questions.