What is node.js?
Node.js is event-driven, asynchronous I/O server-side JavaScript environment based on V8 engine.
Pre-Requirements
You don’t need full root access to install node.js in a cpanel hosting account. Although, you require to have access to the following from your hosting provider:
1. Compiler Access
2. SSH Access
3. An open port
4. Automatic Process Killing Exclusion
You need to first contact your provider to ensure you can access ‘1’ & ‘2’. In case 3 and 4, mention the port you want to use for your node.js app. Provider will exclude the port and your cpanel username in the firewall.
Installation: Step 1
First, find out if you have python 2.6 or above and bzip2-devel installed in the server. You can do using the following commands:
# which python
/usr/bin/python
# python -V
Python 2.6.6
# rpm -qa|grep bzip2-devel
bzip2-devel-1.0.5-7.el6_0.x86_64
Most of the latest cPanel server will have Python 2.6.6 or 2.6.7. So, as long as your provider isn’t running some old version of cPanel. If you have Python 2.6, you don’t need to recompile python. Python in cPanel server executable by the users. You might not see bzip2-devel installed. In case, it is not, your provider would need to install it using yum.
# yum install bzip2-devel
Step 2
Once the above ‘Step 1’ things are available, you can download the latest node.js binary and compile it. While writing this tutorial, node.js is running 0.10.24. You can download the latest node.js source from here:
http://nodejs.org/download/
# wget http://nodejs.org/dist/v0.10.24/node-v0.10.24.tar.gz
# tar -xvzf node-v*
# cd node*
# ./configure --prefix=$HOME
# make
# make install
Out of all the commands above, you need to note the ‘prefix’. It should use $HOME if you are running from the user’s shell. A root user can use the path to the home directory of the cpanel user like /home/cpanelusername in place of $HOME to install node.js in a cpanel user’s account.
Step 3
If you have installed node.js from the root account, change the permission for /home/cpanelusername/bin, /home/cpanelusername/include, /home/cpanelusername/lib to the cpanelusername.
# cd /home/cpanelusername
# chown -Rf cpanelusername:cpanelusername node* bin include lib
Now you can run a node.js node. A simple script to open a port can be the following:
# nano sock.js
Insert the following:
var net = require(‘net’);
var server = net.createServer(function (socket) {
socket.write(‘Open Serverrn’);
socket.pipe(socket); });
server.listen(674, ‘203.20.20.20);
console.log(‘Server running at http://203.20.20.20:674/’);
Now you can run the node as following:
# node sock.js