Installing Node.js on MediaTemple (dv) 4

I have the need—no, the desire—to install Node.js alongside my Apache installation so I can play with clever real-time web applications. The problem is that my server is a (dv) 4 which has Python version 2.4.3, while the current version of Node.js requires Python 2.7. Installation notes follow.

Installing Python

Here’s the problem: the MediaTemple (dv) 4 uses CentOS 5.0. This is old. Installing the latest versions of Node requires a newer version of Python than is available.

The key information is in this article installing python 2.7 on Centos 6: install python as a separate binary, and keep the old one as python. Instead of replacing the existing Python on my (dv) 4.0, which would break the system, the alternative python install leaves the original alone. It is then possible to configure Node.js to use this different interpreter and all theoretically is well.

I followed the instructions for the Python portion, and confirmed that I could run the python27 command.

NOTE: The next part of the instructions, admittedly for a different application than Node.js, describing installing a “Distribute” package. This package seemed to be deprecated in favor of “SetupTools”. Apparently this is a package manager for Python. These SetupTool instructions describe installing them for a target version of python. I’m not sure this is even necessary since I’m not actually planning on distributing Python programs (the apparent purpose of SetupTools); I just want python 2.7 available for running Node.js. So I’ll skip this for now and see if I can get away with it.

Installing Node

Next, I looked for instructions on installing node.js from source. Following the node.js installation guide on Joyent combined with this comment about configuring node to run with a different version of python I issued the following commands to install node.js in my /opt/node directory:

wget http://nodejs.org/dist/v0.10.15/node-v0.10.15.tar.gz
tar -zxf node-v0.6.18.tar.gz
cd node-v0.10.15
PYTHON=/usr/local/bin/python2.7
export PYTHON
python2.7 configure --prefix=/opt/node && make && make install

Then to double-check that the installation succeeded:

cd /opt/node
./node --version

This prints v0.10.15 to the command line.

Then to add node to the path for all users, I followed this stackexchange profile.d discussion to properly add node (living in /opt/node/bin):

echo 'pathmunge /opt/node/bin' > /etc/profile.d/node.sh
chmod +x /etc/profile.d/node.sh
. /etc/profile
node --version

I also logged out and logged back in to make sure that node still could be called from anywhere by any user.

Updating Node

A few years later I wanted to try installing Ghost, so I needed to update my Node installation. The approach is pretty much the same: build from scratch! That’s because I am still on the old (dv) 4.0 CentOS 5 install. There are all kinds of dire warnings from Plesk about screwing around with the default yum repos and installing different versions of Python, so building from scratch with the custom Python2.7 build is still the way to go.

However, there’s a bug in the Node 0.12.x release where the PYTHON environment value isn’t used in some of the installation scripts, which causes the build process to die with an Except Error as e error. The easiest workaround was to make a temporary bin directory with a symbolic link to python 2.7, and temporarily prepend it to my $PATH, as described in this StackExchange article.

mkdir /tmp/py27 
ln -s /usr/local/bin/python2.7 /tmp/py27/python 
export PATH=/tmp/py27:${PATH}
python configure
make
make install

That seemed to do the trick. Once I logout, the temporary path change should go away, and Plesk should not die.

Confirming It Works

Next, confirming that it actually works! I created an “application” directory in my /var/www/vhosts/davidseah.net directory, copied the server version of this simple node.js hello world example, ran it with node hello.js from the command line, and confirmed that my web browser was able to reach it on port 8000. Success!

Next step: set-up Supervisor, which monitors node.js scripts and recompiles them, apparently.

npm install supervisor -g

Instead of running node application.js, I instead do supervisor application.js.

At this point, it’s a little foggy what to do next. If I have some permanent apps set up, I figure I can configure Apache to redirect certain directory names to the node.js server instance that is running at whatever port I happen to set up using the httpd.include file for a particular vhost. Then I can make a particular node app start on server startup with this script that goes into /etc/rc.d/init.d/.