I am Hack Sparrow
Captain of the Internets.

How to install Node.js on Webfaction

Node.js installation instructions for Webfaction

I just got Node.js running on my Webfaction account 🙂 Installation was pretty straightforward and Node completely blew my mind. The greatness of Node.js deserves a post of its own; we will get there another day, for now let's install Node.js on your Webfaction account.

Even though Node.js is currently not in Webfaction's App list, installing and getting it running was a breeze, so worry not if you wanna install Node.js on your account.

First I got the Node.js source archive using wget; there is a git way too, but I preferred the wget way as it was more straightforward. Then I unarchived the source, built, and installed Node.js. Next, I installed the Node Package Manager (npm), and installed some Node packages. Then, I created a Node http server and had it running online successfully. The whole thing is illustrated below.

$ wget http://nodejs.org/dist/node-v0.4.10.tar.gz
$ tar -zxvf node-v0.4.10.tar.gz
$ cd node-v0.4.10
$ ./configure --jobs=1 --prefix=$HOME
$ make
...
$ make install
...

Confirm Node.js is installed.

$ node -v
v0.4.10

With that we are done installing Node.js. Now we need to install npm for managing Node packages (libraries) which can be used with Node.js. We will install the Express Web framework as an example.

$ curl http://npmjs.org/install.sh | sh
$ npm install express

Now that we have installed Node.js and npm, let's create an application and get your Node.js website running.

Create a new app from your Webfaction control panel, let's call it node_test. Select App category as Custom, and App type as Custom app (listening on port). You should get a port number for this app, make a note of it. Set this app for your website, let's say node.hacksparrow.com.

Create a file called test.js with the content below. Replace 31337 with the port number Webfaction generated for you.

var http = require('http');
http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World\n');
}).listen(31337, '127.0.0.1');
console.log('Server running ...');

Now FTP to your account and upload test.js to ~/webapps/node_test. cd to ~/webapps/node_test and run it.

$ cd webapps/node_test/
$ node test.js
Server running ...

Fire up your browser and load http://node.hacksparrow.com (the website you set up, not the literal node.hacksparrow.com), and there you go!

Even though your app listens on a custom port, Webfaction proxies it to port 80. What it means is that you don't have to specify the custom port number to access your website. All browsers make their HTTP requests to port 80 (HTTP port number) by default.

I am very pleased with how easily I could get Node.js running on my Webfaction account, my next step is to explore the Express web framework.

Exercise

  1. How do you keep your scripts running even after you log out?
  2. How do you kill a specific Node script?
  3. What is the default FTP port number? SSH? POP? SMTP? IRC? HTTPS?

References and Links
  1. Node.js
  2. NodeJS 0.4.10 API docs
  3. npm
  4. Webfaction Custom Applications

4 Responses to “How to install Node.js on Webfaction”

  1. drozzy says:

    Everyone keeps saying “node app.js” – but HOW do you keep it running after you log out? That would be great help!

  2. Captain says:

    @drozzy, here is the answer – http://www.hacksparrow.com/keep-node-js-script-running-after-logging-out-from-shell.html 🙂

  3. Rajat says:

    I have been using your articles to setup the nodejs + expressjs stack on webfaction and they are incredibly helpful. Many thanks for the information.

    With sessions enabled, we get the warning of Memoryleak by express and a good solution is to use redis store. However, it is not clear to me how to go about setting up a redis server on webfaction. Any help would be appreciated.

    P.S. I am a UI Engineer and this is the first time I am getting my hands dirty with server configuration setup. Hence, the limited understanding and knowledge. Your articles are very well written though and I am catching up on all this stuff thanks to you.

  4. Captain says:

    @Rajat http://www.hacksparrow.com/use-redisstore-instead-of-memorystore-express-js-in-production.html addresses this issue specifically. All the best!

Make a Comment