Strictly Programming

Deploy Nodejs app in Centos 6.2

Posted in nodejs, upstart by sqllyw on 02/19/2012

Many options to deploy nodejs apps, but after some research I found the solution that works and yet simple. First I tried Forever, it works perfectly until you need your app to be started during boot, i put a line in the /etc/rc.d/rc.local, this might or might not work, it still confuses me up to now, why there are times working and times failed.

next is the Upstart and Monit, upstart has made the writing of starting up script as simple as autoexec.bat in the DOS time, and monit will do the checking of your app to be sure it is working all the time.

Then again, I found out, my need is simple, 1. app should be started during the boot, and 2. restarted if it crashes (that happens in nodejs app), and upstart alone is enough for the two jobs.

Centos 6.2 comes with upstart installed, so what you need to do is create a text file in /etc/init, say, myapp.conf:

#!upstart
description "testjs.js "
author      "sample"

start on runlevel [2345]
stop on shutdown
respawn 

script
    export HOME="/root"

    echo $$ > /var/run/testjs.pid
    exec /usr/local/bin/node /root/test.js >> /var/log/testjs.log 2>&1

end script

pre-start script
    # Date format same as (new Date()).toISOString() for consistency
    echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Starting" >> /var/log/testjs.log
end script

pre-stop script
    rm /var/run/testjs.pid
    echo "[`date -u +%Y-%m-%dT%T.%3NZ`] (sys) Stopping" >> /var/log/testjs.log
end script

once it is finished, you can do:

/sbin/start myapp
/sbin/stop myapp
/sbin/status myapp

and app will be started during booting, you don’t need any other utility in Centos 6.2 to start and keep your app running. I found Centos requires start on runlevel [2345], other options seem not working.

Some useful links as follow:

3 Responses

Subscribe to comments with RSS.

  1. Muneeb Samuels (@muneebsamuels) said, on 06/16/2012 at 7:23 pm

    Thank you this was very helpful.

  2. simonshepard said, on 03/30/2013 at 6:12 pm

    wow, that’s great, the most simple way of doing it explained right there, non of the over convolution of all the other node blogs trying to get you to install their xyz node monitor platform lock-in crap. Just centos, just what I needed! thanks!

  3. Slava Fomenko said, on 06/19/2013 at 10:19 pm

    Thanks, All works !!!


Leave a comment