During the publication of our Let's Make a Web
App tutorial series, people have
been posting comments about hosting Node apps. In particular, the
comments on Node Tutorial
13 are quite detailed.
In this article I've summarised Node hosting approaches so you can pick
the approach that suits you.
In the wider web development community there are some amazing
plug-and-play solutions for hosting web apps. Hosting Node apps isn't
quite as easy, yet, but there are some excellent solutions that are both
fast and easy to understand.
Upstart is a replacement for the
/sbin/init daemon. You can use it to start, stop, and
restart Node processes. If you're using Ubuntu you should already have
it, if you're using Debian or another Linux distribution it's fairly
easy to install. It actually comes with replacements for the sysvinit scripts that come with Debian. The Upstart
Getting Started guide
has more details.
This is an upstart script in /etc/init/myapp.conf that I
knocked up to manage a Node app on EC2:
Cluster has many interesting features, from zero-downtime reloading to a
commnad-line console for real-time administration.
I asked TJ Holowaychuk how he manages production Node apps, and
apparently he's using
Cluster evolved from an older project called Spark, so it seems like
it's got some pedigree.
The age old problem of hosting web software is port 80: binding to this
port requires root. There are ways around this: some firewalls can
redirect traffic from port 80 to a non-privileged port. An alternative
to this is authbind, which can be easily installed in many
distributions. In Debian it's just a case of apt-get install
Once it's installed, create a file in /etc/authbind/byport/80 and chown it to the
user you want to run your Node app as. Then, if you're using Monit or
Upstart, make sure your Node process is launched with authbind and run it as that user.
The previous Upstart example would have to be adapted to work with
start on runlevel 
su -c 'authbind /usr/local/bin/node /data/apps/myapp/app.js 2>&1 >> /var/log/node.log' www-data
Almost a year ago I wrote a tutorial on using nginx to host Node
apps. Servers like Apache and nginx can sit in front of your app and the
web. This means you don't need to run on a privileged port, and the web
server can do jobs it's good at like serving static files.
This may fit in better with your current hosting set up. You could still
use a service monitor with Cluster to manage the Node processes.
I deployed my Upstart/Authbind/Cluster combo to EC2 without any trouble
at all. I used the AWS Console to
create EBS storage devices to keep my Mongo database, application, and
static assets. After that it's basic Linux sysadmin.
Just remember to add your IP address and port 22 to the security policy,
else you won't be able to connect!