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 7 tutorial

For Centos 7 based hosts the procedure is much simplier as Python 2.6.6+ (2.7.5) is already available system-wide. We can also skip compilation and use binary nodejs instead. In this example we are using http server not the socket one.

$ wget http://nodejs.org/dist/latest-v12.x/node-v12.9.0-linux-x64.tar.gz
$ tar xzf node-v12.9.0-linux-x64.tar.gz
$ mv node-v12.9.0-linux-x64 node
$ echo -e '\nexport NODEJS_HOME=~/node\nexport PATH=$NODEJS_HOME/bin:$PATH\n' >> ~/.bashrc
$. .bashrc
$ node -v
v12.9.0

$ cat > server.js<<EOF
const argv = require('minimist')(process.argv.slice(2));
const http = require('http')
var port = argv.port || 80;
console.log("Will listen on port "+port);
http.createServer(function (req, res) {
    res.end('Hello from Node.js server on port '+port+'!')
}).listen(port || 80);
EOF

$ node server.js --port 9392

remote_host$ curl server_ip:9392
Hello from Node.js server on port 9392!