Install and Setup Node.js to Run Forever

Install and Setup Node.js to Run Forever

Obsoleted!

This article is pretty old and was never very good to start with. A much better solution is Deploying Node.js With Upstart and Monit

Install Node.js

$ sudo apt-get update
$ sudo apt-get install git-core curl build-essential openssl libssl-dev
$ git clone https://github.com/joyent/node.git && cd node

I had trouble using the latest (head) verson of node so I used an older version which is optional.

git checkout origin/v0.4

If you don't want the older version, skip the previous command.

$ ./configure
$ make
$ sudo make install
$ node -v

Install Node Package Manager (NPM)

$ curl http://npmjs.org/install.sh | sudo sh
$ npm -v

Install Forever

Next, lets make it persistent using Forever, so once we logoff it still runs and will restart even if node throws an error.

sudo npm install forever --global

 

Make a Test Script

Create a file called test.js and paste this into it.

var util = require('util'),
http = require('http');

http.createServer(function (req, res) {
   res.writeHead(200, {'Content-Type': 'text/plain'});
   res.write('hello, i know nodejitsu.')
   res.end();
}).listen(8000);
/* server started */
util.puts('hello world running on port 8000');

Run it Forever (until you kill it)

forever start test.js

You should be able to see it here

ps axl | grep node

And here

forever list

You can kill it using

forever stop 0

Where 0 is the index of your code in the forever list.

Time to start javascripting.

References

Installing Node Wiki https://github.com/joyent/node/wiki/Installation

Installing Node.js and NPM on Ubuntu 11.04 http://www.giantflyingsaucer.com/blog/?p=2775

Keep A Node.js Server Up with Forever http://blog.nodejitsu.com/keep-a-nodejs-server-up-with-forever