Posted in Amazon Web Services, NodeJS

Running NodeJS in Amazon EC2

Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript engine. It is a server-side programming to build network applications using JavaScript. For a machine to run NodeJS, it should be configured. Configuring it in Amazon EC2 instance is as similar as configuring it in our local machine.

Read this article to know how to launch Ubuntu 16.04 using Amazon EC2.

Following steps can be performed using any SSH terminal like PuTTY.

Step 1:
Install NodeJS using advanced packaging tool

sudo apt-get install nodejs

You will be prompted to download 3 MB archive. Enter to continue.

Step 2:
Install Node Package Manager (npm)

sudo apt-get install npm

You will be prompted to download an archive. Enter to continue.

Step 3:
Verify installation

  1. Type node in the terminal. If it displays >, you are good to proceed.
  2. If it says, “The program ‘node’ is currently not installed. You can install it by typing: sudo apt install nodejs-legacy”, you need to debug further:
  3. Type which nodejs you will be provided with the install location as /usr/bin/nodejs.
  4. Run sudo ln -s /usr/bin/nodejs /usr/bin/node
  5. Now, when you run node, it should display >. This means that node terminal is running successfully. Just type 1+2, node gives 3.

Step 4:
Run NodeJS application

  1. Type vi app.js to open vi editor to create file named app.js.
  2. Type i to insert text into the editor. (See this link to know basic vi commands).
  3. Add your NodeJS code or the basic one:
    // Load the http module to create an http server.
    var http = require('http');
    // Configure our HTTP server to respond with Hello World to all requests.
    var server = http.createServer(function (request, response) {
     response.writeHead(200, {"Content-Type": "text/plain"});
     response.end("Hello World\n");
    // Listen on port 8000, IP defaults to
    // Put a friendly message on the terminal
    console.log("Server running at");
  4. Press Esc and then :wq to save and quit the file.
  5. Now, you will be in terminal window, start node app.js.
  6. Server is now listening to the port 8000.
  7. You can launch your browser and enter the IP of your EC2 instance with port 8000. Example: If your IP is then URL should be
  8. You will see the text Hello World!!! printed on the browser document.

Debug note: In case browser doesn’t open up the URL, be sure that you have added a Custom TCP Rule with port 8000 in Inbound rule of the security group which your instance uses.


Bonus Tip:
You can access the application in the browser until the PuTTY terminal is closed. Once it is closed, the node server stops and the application fails to execute/run. To keep the command from running, make use of screen command:

screen node app.js

Now, though the terminal is closed, the command continues to run in background. Later, to stop the command, open the terminal again and enter:

screen -r

to bring background running command to foreground. From here, execution can be stopped.


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