Install Node.js and Express.js with Nginx on Debian Lenny

Posted on: May 07, 2011

Following instructions were tested on a RackSpace Cloud Server. It
should work for any other reasonably configured, mostly bare-bones
Debian Lenny and latest Ubuntu (e.g. on other popular hosting options
like: SliceHost, Linode etc.). First, let's install some essentials:

Now we can proceed with Node.js installation. There are two ways to
install Node.js, directly (not recommended due to maintenance cost) or
with a version manager like nvm
(highly recommended in most cases):

Installing NPM Package Manager

You do not need this if you installed Node using NVM, because NVM
installs NPM as well. Let's install Node's package manager (make sure
your non-root user is also part of the "webmaster" group)::

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

Next, you can run "npm search" (takes a while) to see the abundance of
Node.js packages available or "npm ls" to see packages already
installed.

Another Node Version Manager: Nave

Nave is different from NVM in that it
does not require sourcing a shells script. Comparing Nave and NVM,
Nave's author wrote: "Nvm is also really nice, but has to be sourced
rather than being run, and thus is a little bit wonky for some use
cases. But it doesn't involve subshells, which makes it better for many
others." I think they are both pretty great, so it's a matter of taste,
maybe. Nave allows us to run multiple versions of node in parallel and
switch between them, similar to how rvm and virtualenv do it for Ruby
and Python respectively:

After which, if you go to http://yourdomain.com:3000/ you should see a
nice "Hello World" message. Voila! We made our first Node.js app! Of
course, in the real world you will probably want to put a web-server in
front of the node.js app. If for no other reason: to serve static
content faster and to securely attach to port 80. Since the big deal
with node.js is its non-blocking architecture, it makes sense to
front-end it with a non-blocking web-server as well. Let's see how we
can route our node.js app through Nginx.

Configure Nginx

Edit file: /etc/nginx/nginx.conf and insert the following line at the
end of the "http" section (typically: before the very last closing curly
brace "}" in the file, unless Nginx changes the default config file):

Attention: If you use some CSS shorthand frameworks, as Will
explains in his
comment
you will want to remove css files from the list of static content that
you'd like NginX to handle for you. You can start nginx with:

$ sudo /usr/local/sbin/nginx

If you later need to edit Nginx configuration and reload it you can run:

$ /usr/local/sbin/nginx -s reload

You can test Nginx configuration for errors with:

$ sudo /usr/local/sbin/nginx -t

Once Nginx is properly configured, you should be able to see the same
express.js response page with http://yourdomain.com (without the port
3000 and assuming that you are still running the node.js application).
At that point request is routed through Nginx and we have also added
instructions to serve static content (images, css, javascript etc.)
directly from Nginx (subfolder "public"), bypassing Node.js

Bundling Dependencies

Instead of manually installing required modules, you can define
dependencies of your app declaratively and let npm manage it for you. If
you are familiar with Ruby On Rails, this is a process similar to using
Gemfiles and Bundler. First, you need to create a package.json in the
same folder where your app.js lives:

Multi-Process Node.js

Node.js is a single-threaded system. Yes, you heard it right. There is
only one thread. Due to its event-loop-based asynchronous and
non-blocking nature it scales great even as a single thread, but if you
are running on a multi-core CPU you should want to start multiple
Node.js processes so you can leverage available cores. To utilize
multiple Node processes you should use Clustering, which is now part of
Node.js core.

Next Steps

To understand clustering, hot reloading and take a look at a sample
Node/Express.js project, dive into the code of
NodeBootstrap on Github.