Node.js setup for easy deployment and updating Node.js setup for easy deployment and updating node.js node.js

Node.js setup for easy deployment and updating


Combining all knowledge gathered (Big thanks to Julian Knight for the ideas) and methods tested in the past week, I've decided to settle for the deployment solution described below (I thought I'd be nice to share to help others with comparable questions):

Auto-restarting on script errors and automatic reloading on script changes is handled by forever, as it also includes a script watch, as long as Forever is spawned from within a node.js script.

To do so, I've added a server.js to launch the app.js script we actually want to run:

server.js

var forever = require('forever'),    child = new(forever.Monitor)('app.js', {        'silent': false,        'pidFile': 'pids/app.pid',        'watch': true,        'watchDirectory': '.',      // Top-level directory to watch from.        'watchIgnoreDotFiles': true, // whether to ignore dot files        'watchIgnorePatterns': [], // array of glob patterns to ignore, merged with contents of watchDirectory + '/.foreverignore' file        'logFile': 'logs/forever.log', // Path to log output from forever process (when daemonized)        'outFile': 'logs/forever.out', // Path to log output from child stdout        'errFile': 'logs/forever.err'    });child.start();forever.startServer(child);

This watches all files in the application directory for changes and restarts the script running in foreveras soon as one changes. Because the logs and pidfile are in subdirectories of the application, those have to be ignored from the file watch, or the script will loop restarts:

.foreverignore

pids/**logs/**

To make this all start on system boot and enabling us to easily control the service using start node-appand stop node-app we use Ubuntu's Upstart.I've combined two examples (this and this one) into one that does the job quite well:

/etc/init/node-app.conf

# This is an upstart (http://upstart.ubuntu.com/) script# to run the node.js server on system boot and make it# manageable with commands such as# 'start node-app' and 'stop node-app'## This script is to be placed in /etc/init to work with upstart.## Internally the 'initctl' command is used to manage:# initctl help# initctl status node-app# initctl reload node-app# initctl start node-appdescription "node.js forever server for node-app"author      "Remco Overdijk <remco@maxserv.nl>"version "1.0"expect fork# used to be: start on startup# until we found some mounts weren't ready yet while booting:start on started mountallstop on shutdown# Automatically Respawn:respawnrespawn limit 99 5env HOME=/home/user/node-app-dirscript    # Not sure why $HOME is needed, but we found that it is:    export HOME=$HOME    chdir $HOME    exec /usr/local/bin/node server.js > logs/node.log &end script#post-start script#   # Optionally put a script here that will notifiy you node has (re)started#   # /root/bin/hoptoad.sh "node.js has started!"#end script

As Kevin wisely mentions in his article it's unwise to run node as root, so we'll change that to exec sudo -u www-data /usr/local/bin/node when we move to new servers next week.

So, forever gets started automatically by node server.js which gets launched by upstart, and monitors for crashes and file changes, keeping the entire setup running as long as we want.

I hope this helps anyone.


Since my last answer is for the future! Here are some other links to assist:

There doesn't yet seem to be a perfect answer but there are plenty of people running production Node instances. Hopefully this will point you in the right direction.


You might be better off, for production use, to look at something like Cluster. You might not want the cluster features but it also includes other production features such as zero downtime restarts, logging, workers, etc.

As you say, Forever is OK for testing but doesn't really have what it takes for production use.

I seem to vaguely remember that Cluster or something similar may be adopted into Node itself come v0.7