Run nodejs server continuously using forever

By last Friday morning the open bugs count raised above 150 mark and we managed to take it down to under 25 by the end of the day, thanks to the dedicated effort by the team. Among them, one was to make the Nodejs server run continuously. In our application we are using the Nodejs to implement a near-realtime notification module, which involved pushing notifications and updates to the Web application as well to the Mobile application. Faye module was used to implement the identified channel patterns required to identify different user types and their respective activities each user type executes.

Nodejs is a powerful, Event-driven, I/O based or JavaScript based server that can be used to develop interactive applications. It was developed on top of the V8 JavaScript engine.

Installing Nodejs on Ubuntu

I came across an easier approach to install Nodejs via the Ubuntu Package Manager.

$ sudo apt-get install python-software-properties
$ sudo add-apt-repository ppa:chris-lea/node.js
$ sudo apt-get update
$ sudo apt-get install nodejs npm

Install Forever

In the last command it installed the Node Package Manager(npm). The npm provides the facility to install any module to Nodejs in a manner similar to installing application on Ubuntu via apt-get.

$ sudo npm install forever --global

Setting the –global parameter makes the module accessible globally

Updating Nodejs & the modules to latest stable release

Nodejs comes with a module called n aka node version manager provides the facility to update the module to its latest or stable release.

Install n

$ sudo npm install --global n

Updating Nodejs using n

$ sudo n --global latest/stable
$ sudo n --global custom v0.x.x

Updating Nodejs modules using n

$ sudo n --global npm
$ sudo n --global forever

Connect To Amazon EC2 via Putty

In this post I’m going to show how to establish a SSH connection to an Amazon EC2 instance using Putty using a Windows box. First of all we need to download following tool. It can be find under the section called For Windows on Intel x86.

Step 1: Generating the Private Key using Puttygen

In Amazon AWS environment each instance (EC2, RDS, ElasticCache, etc…) is attached to a permission/security group, access to each service is provided through Private Key/Public Key authentication. Each Amazon EC2 instance will holds the Public Key of the permission/security group it belongs. To connecting to a specific Amazon EC2 instance need to use the corresponding Private Key (xxx.pem). Puttygen can be used to generate the local Private key out from the Private Key obtained from Amazon AWS required for establishing the connecting to Amazon EC2 instance via Putty.

Open Puttygen as shown in Figure 1 and click Load to select the Private Key obtained form the Amazon AWS.

 Generate private key

Figure 1: Selecting the Amazon AWS Private Key

Next click Save private key, next click OK on the appearing dialog box as shown in Figure 2 and 3

Saving generate private key

Figure 2: Saving the generated Private Key

Provide a suitable name (eg: aws_putty_private_key.ppk) for the newly created Private key, make sure to store all these keys in a well secure place.

Saving generate private key

Figure 3: Saving the generated Private Key

Step 2: Pointing the generated Private key to Putty

Now we have finish creating the private key required by Putty. Open Putty and navigates to Connection -> SSH -> Auth from the left pane of the Putty window as shown in Figure 4. Select the newly created private key (aws_putty_private_key.ppk) from the Options controlling SSH authentication pane as shown in Figure 5

Options controlling SSH authentication

Figure 4: Options controlling SSH authentication

Selecting the generated private key

Figure 5: Selecting the generated Private key

Step 3: Providing Amazon AWS EC2 instance information

Navigates to Session from the left pane of the Putty window as shown in Figure 6 and Provide the Host Name or the IP of the Amazon EC2 instance Connection Type as SSH.

Providing Amazon AWS EC2 instance information

Figure 6: Providing Amazon AWS EC2 instance information

Step 4: Connecting to Amazon AWS EC2 instance

Provides the User name of the EC2 instance.

Connecting to Amazon AWS EC2 instance

Upon successful authentication connection is established. Happy hacking.

Connecting to Amazon AWS EC2 instance

Workarounds found for Huawei e220 on ubuntu 11.10

Once upgrading form Ubuntu 11.04 to 11.10 the Huawei E200 modem failed to work, unfortunately I switched back to Windows 7 for browsing the Internet. I didn’t realized that I would have search for this issue in the first place and today only I just did it, viola I managed to find some workarounds to get my good old Huawei E200 modem back to work 🙂

  • Adding the “blacklist usb_storage” to /etc/modprobe.d/blacklist.conf
  • sudo usb_modeswitch -v 0x12d1 -p 1003 -V 0x12d1 -P 1003 -R

As usual pluagged in the modem and ran the command illustrated under second point and now I’m happily surfing the Internet.

Following was the message got printed on the terminal.

hayesha@gnu-user:~$ sudo usb_modeswitch -v 0x12d1 -p 1003 -V 0x12d1 -P 1003 -R
[sudo] password for hayesha:

Looking for target devices ...
Found devices in target mode or class (1)
Looking for default devices ...
Found devices in default mode, class or configuration (1)
Accessing device 003 on bus 001 ...
Getting the current device configuration ...
OK, got current device configuration (1)
Using endpoints 0x02 (out) and 0x82 (in)
Not a storage device, skipping SCSI inquiry

USB description data (for identification)
Manufacturer: HUAWEI Technologies
Product: HUAWEI Mobile
Serial No.: not provided
Warning: no switching method given.
Resetting usb device .
OK, device was reset
-> Run lsusb to note any changes. Bye.

hayesha@gnu-user:~$ lsusb
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 002: ID 8087:0020 Intel Corp. Integrated Rate Matching Hub
Bus 002 Device 002: ID 8087:0020 Intel Corp. Integrated Rate Matching Hub
Bus 001 Device 003: ID 12d1:1003 Huawei Technologies Co., Ltd. E220 HSDPA Modem / E230/E270 HSDPA/HSUPA Modem
Bus 001 Device 004: ID 03f0:231d Hewlett-Packard
Bus 002 Device 003: ID 064e:f209 Suyin Corp.


Useful GNU Linux commands

During the past week I spend most of my time and effort on installing and configuring Ubuntu Server 10.04.3 and required applications, so following are some of the useful commands I came across.

Manual Partitioning

As a practice, when installing GNU Linux I partition the harddisk as follows by selecting the option of manual partition.

  • / – Root partition
  • /boot – Boot partition, that keep the grub loader, especially important when multiple operating systems are installed.
  • /home – Home partitions where the user data will be stored
  • swap – Swap partitions, recommends to allocate double the size of RAM attached to the machine.

Once the partitioning is completed the OS installation will take few minutes to complete. Next step will be to configure the server to establish a connection to the Internet via the local network.

Network Configuration – Static IP approach

Let us see the steps involve in assigning a static IP and the DNS information.

  • Check whether the network card is properly working and the cable is properly connected as follows:
    $ sudo mii-tool eth0
    should give the details of the network card instead of no link message.
  • Backup the existing interfaces configuration file as follows:
    $ sudo cp /etc/network/interfaces /etc/network/interfaces.bk
    and open the interfaces file as following
    $ sudo vim /etc/network/interfaces
  • Replace the values for address, netmask, gateway, and broadcast with values specific to your desired IP address and network.
    # The loopback network interface
    auto lo
    iface lo inet loopback

    auto eth0
    iface eth0 inet static

  • Setting the IP’s for the DNS Server(s), backup the resolv.conf as follows:
    $ sudo cp /etc/resolv.conf /etc/resolv.conf.bk
    and update the nameservers as follows:
    $ sudo vim /etc/resolv.conf
  • Next, restart the network interfaces as follows:
    $ sudo /etc/init.d/networking restart
  • Finally, test the configuration first by pinging to the Gateway IP and then to another external IP or site like
    $ ping
    PING ( 56(84) bytes of data.
    64 bytes from icmp_seq=1 ttl=64 time=0.051 ms
    64 bytes from icmp_seq=2 ttl=64 time=0.061 ms
    64 bytes from icmp_seq=3 ttl=64 time=0.068 ms

Mount/unmount a filesystem – External hard disk with a ntfs filesystem

  • Find the device name (eg: /dev/sdb1) required to mount using the following command.
    $ sudo fdisk -l
  • Create a mount point and mount the external hard disk as follows:
    $ sudo mkdir /media/external_hard
    $ sudo mount -t ntfs-3g /dev/sdb1 /media/external_hard
  • Unmount the hard disk as follows:
    $ sudo umount /media/external_hard


Virtual Hosting

Last month I got a chance to play with an Ubuntu Linux server to set up Virtual Hosting for couple of sites in the office. Most of the time this is achieved through cPanel interface, this server was not supported with GUI tools but I got the root access :-).

Unfortunately I had only heard and read little about Virtual Hosting. So this was sort of a challenge for me. So first of all I did a bit of searching to get to know the basics of Virtual Hosting. With couple of hours effort I managed to figure out what should be done.

Following is the folder structure I created in the server to hold the site related resources. Sometimes I might have missed some of the important aspect, so strongly recommends you to refer here for a complete explanation.

                                                |—- html
                                                |—- logs
                                                |—- etc

  • html – place all the resources required for the site.
  • logs – place the access log and error log files.
  • etc – place other resources that are not directly required for the site but you feel like useful for future activities.

Once above is completed next step is to update the apache server about the new Virtual Host. Following block illustrates the necessary configuration for the new Virtual Host. Opens up your favorite text editor and place the below content block and replace the ‘‘ with the sites actual name. Save the file as in the following path: /etc/apache2/sites-available/ and create a symbolic link to /etc/apache2/sites-enabled/.

How to

  • Opens the terminal window
  • Creates a new file having a name like ‘‘ (replace this with your actual site details) using vim editor: sudo vim /etc/apache2/sites-available/
  • Enter into insert mode by pressing i key on your keyboard and place the below code block and replace respective location to reflect to your site’s settings.
  • Next click Esc key on your keyboard and type :wq
  • Finally create a symbolic link to /etc/apache2/sites-enabled/ using following command: ln -s /etc/apache2/sites-available/ /etc/apache2/sites-enabled/

<VirtualHost *:80>

ServerAdmin webmaster@localhost

DocumentRoot /home/websites/

Options Indexes FollowSymLinks
DirectoryIndex index.html, index.php

ErrorLog /home/websites/

# Possible values include: debug, info, notice, warn, error, crit,alert, emerg.

LogLevel warn

CustomLog /home/websites/ combined
ServerSignature On


« Older entries