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:
Select a region for your sever from top right corner of the page and click on 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.
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.
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.
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.
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.
From left menu in putty expand SSH and select Auth. Now select brows on the right side and find your .ppk file.
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.
wget http://nodejs.org/dist/node-latest.tar.gz
tar xzf node-latest.tar.gz
cd node-v0.10.22
./configure --prefix=/usr
make
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 http://github.com/isaacs/npm.git
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 / {“.
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
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, "127.0.0.1");
console.log('Server running at http://127.0.0.1:3000/');
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:
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 rcconf
sudo rcconf
This will open the rcconf window as:
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:
[program:node]
command=node app.js
directory=/home/ubuntu/www
environment=NODE_ENV=production
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.
Thanx for this guide..
I have some query please guide me.. How to stop supervisor.. and add mongodb instance to run parallel with node
Hi Jay,
you can stop supervisor using:
/etc/init.d/supervisord stop
OR
/etc/init.d/supervisord stop all
commands. I’m not sure about mongodb but you can try this:
http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/
what is rcconf for ?
why supervisor ? why not forever js ? how is it different ?
How do i check supervisor is running or not ?
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
Thanks Very helpful.
Just one question. When you type the URL in the browser the request is sent on port 80.
How does it get redirected to port 3000 where our server is running?
I had to add an inbound rule to allow HTTP port 80 so that the public DNS would show “Hello World” on browser
I followed your guidelines step by step however, my node.js app do not start once the ec2 is rebooted 🙁
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 🙂
im having the same problem with it not starting node on boot. running on ec2 ubuntu 14.4.
The same issue.
Hey. This was an awesome article. Thanks dudes, you saved me some heartache.
Thank you very much! Pretty much the only working tutortial to setup from scratch.
Thanks, it was very helpful! 🙂