How to Install node.js on a cPanel Server

nodejs

Installation of node.js is  not yet officially supported by cPanel.However It’s still a feature request .

(Discussion at Official cPanel Forum too )

What is Node.Js?

Node.js is an application development software written in and for javascript for real-time event-driven applications,more at http://en.wikipedia.org/wiki/Node.js

The Installation Procedure:

Installing node.js on a cPanel VPS  doesn’t involve any complex tasks.  Prior beginning the installation make sure following requirements are met:

GCC 4.2 or newer
Python 2.6 or 2.7
GNU Make 3.81 or newer

Run and Update for any software in the above list (If not up to date).

Initially download latest version from nodejs.org .

wget http://nodejs.org/dist/v0.11.9/node-v0.11.9.tar.gz

Next, extract the node.js tarball and install it:

tar -xzvf  node-v0.11.9.tar.gz
cd node-v0.11.9
./configure
make && make install

The installation will take a while to complete . Now upon completion we could test it working.Check the version

[root@sh csf]# node -v
v0.11.9

[root@sh csf]# which node
/usr/local/bin/node
[root@sh csf]# /usr/local/bin/node --help

Usage: node [options] [ -e script | script.js ] [arguments] node debug script.js [arguments]

Options:
-v, --version print node's version
-e, --eval script evaluate script
-p, --print evaluate script and print result
-i, --interactive always enter the REPL even if stdin
does not appear to be a terminal
--no-deprecation silence deprecation warnings
--trace-deprecation show stack traces on deprecations
--v8-options print v8 command line options
--max-stack-size=val set max v8 stack size (bytes)
Environment variables:
NODE_PATH ':'-separated list of directories
prefixed to the module search path.
NODE_MODULE_CONTEXTS Set to 1 to load modules in their own
global contexts.
NODE_DISABLE_COLORS Set to 1 to disable colors in the REPL

Documentation can be found at http://nodejs.org/

Testing the  Installation:

Just scroll to  cd /usr/local/cpanel/htdocs

vi server.js

var http = require("http");
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello Test");
response.end();
}).listen(8080);

save and quit

Open the port 8080 in your firewall via csf conf .

vi /etc/csf/csf.conf

TCP IN, OUT ===>8080
save and quit

csf -r

(from the dir/usr/local/cpanel/htdocs execute below )
/usr/local/bin/node server.js

Now access http://ip.add.re.ss:8080

You will get a confirmation message in your browser that says “Hello World”.

Extra:
If you need to direct traffic for our domain to the node.js build, you could implement it using via the .htaccess file by following rules.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^(.*) "http\:\/\/127\.0\.0\.1\:8080\/$1" [P,L]

Your domain name replaces “domain” in the above example. Just visit your domain again!

The above installation is tested under latest cpanel .

[root@sh]# cat /usr/local/cpanel/version
11.44.1.19
[root@sh]#

Let us know whether any sort of change or modification  is needed for  above installation, suggestions are invited!