Friday, 18 December 2015

Install node.js on Ubuntu 14.04 linux server and configure a hello world node.js application using pm2 and varnish

Node.js is an open-source, cross-platform runtime environment for developing server-side web applications. Node.js applications are written in JavaScript and can be run within the Node.js runtime.

Node.js is primarily used to build network programs such as web servers, making it similar to PHP.The biggest difference between PHP and Node.js is that PHP is a blocking language, where commands execute only after the previous command has completed, while Node.js is a non-blocking language where commands execute in parallel, and use callbacks to signal completion.

Below given are the steps to install node.js on Ubuntu server 14.04 LTS

$apt-get install nodejs

$apt-get install npm

This will install node.js on Ubuntu. Now add a symlink to 'node' so that nodejs will be called with 'node' command.

$ln -s /usr/bin/nodejs /usr/bin/node

To confirm the node.js installation, type the below command:

$node -v

Now we have successfully installed and configured node.js on Ubuntu 14.04 LTS server.

Let's see how to configure a simple "hello world" node.js application.

Create a directory called hello-node.js in Ubuntu server and change to that directory.

$mkdir /var/www/sites/hello-node.js

$cd /var/www/sites/hello-node.js

Now create a file called 'hello.js' and copy paste the below content:

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(9000, '');
console.log('Server running at');

Here we have configured the hello world application to listen on the port 9000. We can run the application by using below command :

$node hello.js

But this method is not suitable for running production work loads. So let's see how to make node.js application running as a service.

To make node.js running as a service, we can use "pm2" .

Install pm2 using below command :

$npm install pm2 -g

Now start the node.js hello world application using below command:

$pm2 start hello.js

To make the hello world application automatically start on Ubuntu server reboot, use below command :

$pm2 startup ubuntu

This will create a start up script at the location : /etc/init.d/

You can use below commands to manage the node.js application :

$pm2 list

$pm2 stop hello

$pm2 start hello

$pm2 info hello

To remove the hello world application from pm2, you can use below command:

$pm2 delete hello

Now let's configure node.js application with Varnish. In this setup, node.js will listen on port 9000 and varnish will listen on port 80. Varnish will then forward the traffic to node.js app on port 9000 based on the URL.

Open varnish default configuration and add below lines:

$vim /etc/varnish/default.vcl

backend nodejs {
    .host = "";
    .port = "9000";

sub vcl_recv {
 if ( ==  "") {
        set = "";
        set req.backend = nodejs;
        return (pass);
    } else {
        set req.backend = default;

If it is Varnish V4.0, use the below configuration:

You can find the varnish version using below command:

$ varnishd -V
varnishd (varnish-4.0.3 revision b8c4a34)
Copyright (c) 2006 Verdens Gang AS
Copyright (c) 2006-2014 Varnish Software AS

$vim /etc/varnish/default.vcl

vcl 4.0;

backend nodejs {
    .host = "";
    .port = "9000";

sub vcl_recv {
 if ( ==  "") {
        set = "";
        set req.backend_hint = nodejs;
        return (pass);
    } else {
        set req.backend_hint = default;

Restart varnish to make the changes in to effect.

Now go to in the browser and you can access the node.js hello world application (running on port 9000) from your browser.

1 comment:

  1. Im no expert, but I believe you just made an excellent You certainly understand what youre speaking about, and I can truly get behind that.
    Node JS training in chennai