Installing Node.js and Python 2.7 on cPanel with a unique IP address

This Tutorial has been updated for the current version of cPanel over here.

This is a small tutorial or how-to on how to install node.js on a cpanel box. Note to all that setting up an EC2 instance on Amazon and getting Node.js up and running was WAY easier than doing this on a cPanel box.

First lets install bzip2-devel with yum:

yum install bzip2-devel

First we need to install an alternate version of Python. We'll install Python 2.7 and put it in /opt.

cd /usr/local/src
wget http://python.org/ftp/python/2.7.3/Python-2.7.3.tgz
tar -zxvf Python-2.7.3.tgz
cd Python-2.7.3
./configure --prefix=/opt/python2.7 --with-threads --enable-shared
make
make install
ln -s /opt/python2.7/bin/python /usr/bin/python2.7
echo '/opt/python2.7/lib'>> /etc/ld.so.conf.d/opt-python2.7.conf
/sbin/ldconfig

(Thanks to this site for giving this part of the tutorial).

Best to test it actually worked and that bz2 can be found, before we continue:

cd /opt/python2.7/bin
./python -c "import bz2; print bz2.__doc__"
The python bz2 module provides a comprehensive interface for
the bz2 compression library. It implements a complete file
interface, one shot (de)compression functions, and types for
sequential (de)compression.

If you see what is above, then python works and bz2 can be found! Now let's set our PATH environment so that we can see the new python from our shell. Add the follow line to the end of your ~/.bashrc file:

export PATH=/opt/python2.7/bin:$PATH

Now run source ~/.bashrc to load up the change:

source ~/.bashrc

You should be able to find the new python now with this:

# which python
/opt/python2.7/bin/python

Now, let's get node.js installed:

cd /usr/local/src
git clone git@github.com:joyent/node.git
cd node

More than likely you will need to edit the ./configure file to change the environment from the old crusty cpanel 2.4 to your shiny new Python 2.7 version in the first line. Change:

#!/usr/bin/env python
to
#!/usr/bin/env python2.7

Now we can build node:

./configure
make
make install

If you are installing a newish version of Git have an older version of GCC, you may need to apply this patch. The error I got from this little bug was:

unrecognized command line option "-Wno-old-style-declaration"

Next up, lets make sure we have a unique IP on our box to run node from, so we don't have to use alternate ports nor create a proxy. You do this by using the Apache Configuration -> Reserved IPs Editor in cPanel. Set one of your IP's to reserved so that Apache won't listen to it anymore. Let's say for example you have IP address 111.111.111.111 in your list, and you want Node.js to use this instead of Apache. Simply check the box beside 111.111.111.111 Reserved and click on Save. You will be asked to rebuild apache, which is fine and only takes a few seconds.

Now we can setup a Node.js server and see if it works from our browser! Create a folder somewhere on your server where you can create your Node server and files. Then create a file called server.js and put the following into it, making sure to change the IP address 111.111.111.111 to the one you reserved in cPanel:

var http = require("http");
function onRequest(request, response) {
  console.log("Request received.");
  response.writeHead(200, {"Content-Type": "text/plain"});
  response.write("Hello World");
  response.end();
}
http.createServer(onRequest).listen(80, '111.111.111.111');
console.log("Server has started.");

(Thanks to this site for an excellent tutorial on learning Node.js).

Now launch the node.js server:

# node server.js 
Server has started.

If you see the above, then you can test the server by going to your IP address in your browser. When you load the page in your browser you should see "Request received." in your shell. You'll note that you see it twice, this is due to the fact that one is when your browser looks for a favicon. This is all explained and a lot more in the Node.js tutorial linked just above.

There you have it, Python 2.7 and Node.js should now be running on a unique IP address on a cPanel box on port 80. Let me know if you have questions, but due to a lack of time I might not be of much help. All of the above was found simply by using Google.

Comments