Published on Jul 31 2012 in Control Panels PHP/Perl/Python

See how to install node.js on a cPanel account alongside your JVM and appserver.

Node.js is event-driven, asynchronous I/O server-side JavaScript environment based on V8 engine._

For Centos 6 version scroll to the bottom.

On request you will be assigned a dedicted port or ports to use with node.js by your hosting provider. If you already have a dedicated IP you will be able to request any port number not already bound for this IP to use with node.js sockets. On a shared server you are required to install node.js and dependencies in your home directory. The procedure will be similar in case of a VPS where you are not restricted to a home directory but running node.js as an uprivileged user is still highly recommended.

Step 1. Install python 2.6 or 2.7 in your home directory

On a shared server you may need to request compiler access enabled from support. node.js will require bz2 python module so make sure bzip2-devel package (or similar for your distro) is available. Request it from support if missing. They will need to run yum install bzip2-devel as root. Another development libraries may be installed system-wide too (e.g. sqlite-devel) if required. Without bz2 module you will see "ImportError: No module named bz2" when installing node.js.

[~]# rpm -qa | grep bzip2-devel 
[~]# which python
/usr/bin/python
[~]# python -V
Python 2.4.3
[~]# wget http://www.python.org/ftp/python/2.6.7/Python-2.6.7.tgz
[~]# tar xzf Python-2.6.7.tgz 
[~]# cd Python-2.6.7
[~]# ./configure --prefix=$HOME --with-threads --enable-shared
[~]# make 
[~]# make install

At the end of make you may see information about what development headers were missing and module build skipped for. For example:

Failed to find the necessary bits to build these modules:
_tkinter bsddb185 dl
imageop sunaudiodevTo find the necessary bits, look in setup.py in detect_modules() for the module's name.

Python is still served from /usr/bin so we need to modify $PATH in .bashrc by inserting

# User specific aliases and functions
export PATH=~/bin:$PATH

Run the below code to have new $PATH setting in effect and test python:

[~]# source .bashrc
[~]# which python
/home/nodetest/bin/python
[~]# python -V
python: error while loading shared libraries: libpython2.6.so.1.0: cannot open shared object file: No such file or directory

Python 2.6 cannot find the libs in home directory so let's modify $LD_LIBRARY_PATH by adding another line into .bashrc and importing it into current environment:

# User specific aliases and functions
export PATH=~/bin:$PATH
export LD_LIBRARY_PATH=~/lib:$LD_LIBRARY_PATH
 
[~]# source ~/.bashrc
[~]# python -V
Python 2.6.7

Step 2. Install node.js in your home directory

[~]# wget http://nodejs.org/dist/v0.8.2/node-v0.8.2.tar.gz
[~]# tar xzf node-v0.8.2.tar.gz 
[~]# cd node-v0.8.2
[~]# ./configure --prefix=$HOME
[~]# make 
[~]# make install

Optionally, install setuptools to make python module management easier later.

[~]# wget 'http://pypi.python.org/packages/2.6/s/setuptools/setuptools-0.6c11-py2.6.egg#md5=bfa92100bd772d5a213eedd356d64086'
[~]# sh setuptools-0.6c11-py2.6.egg

Here is an example of a simple TCP server which listens on port 12345 (port number assigned by support and open in firewall, shared or dedicated IP) and echoes whatever you send it:

[~]# cat > socket_server.js<<EOF
var net = require('net');
var server = net.createServer(function (socket) {
 socket.write('Echo server\r\n');
 socket.pipe(socket);
});
server.listen(12345, '10.10.10.10');
console.log('Server running at http://10.10.10.10:12345/');
EOF
    
[~]# node socket_server.js 
Server running at http://10.10.10.10:14243/

Then you may test it from another host:

anotherhost:~$ telnet 10.10.10.10 12345
Trying 10.10.10.10...
Connected to 10.10.10.10.
Escape character is '^]'.
Echo server
abcd
abcd
Connection closed by foreign host.

Static memory usage as reported by top for the socket_server.js and chat-server.js

VIRT RES  SHR  S %CPU %MEM TIME+   CODE SWAP DATA COMMAND
581m 8620 4868 S 0.0  0.0  0:00.04 8564 572m 555m node example.js
579m 12m  5984 S 0.0  0.0  0:00.04 8564 566m 551m node chat-server.js

NodeJS on Centos 6 tutorial

For Centos 6 based hosts the procedure is much simplier as Python 2.6.6+ is already available system-wide. We can also skip compilation and use binary nodejs instead:

wget http://nodejs.org/dist/v0.10.31/node-v0.10.31-linux-x64.tar.gz
tar xzf node-v0.10.31-linux-x64.tar.gz
mv node-v0.10.31-linux-x64 node
./node/bin/node socket_server.js