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

translatoryazooInternet and Web Development

Nov 12, 2013 (3 years and 8 months ago)


Fresh Blurbs
Install Node.js and Express.js with Nginx on Debian Lenny
Sat, 05/07/2011 - 18:57.
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:
$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install build-essential
$ gcc -v
$ make -v
$ sudo apt-get install python-software-properties
$ sudo apt-get install libssl-dev libreadline-dev
$ sudo apt-get install git-core curl
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
(highly recommended in most cases):
Method 1: Installing Node.js with NVM
Attention: this is the recommended way.
$ git clone git:// ~/.nvm
$ . ~/.nvm/
$ nvm ls
$ nvm install v0.4.12
$ nvm alias default v0.4.12
$ nvm ls
$ nvm help
Please make sure to add following line to your (and by "your" we mean the user which node apps will b
executed as) ~/.bashrc or ~/.profile:
. ~/.nvm/
You may want to log out and log back in, to test that login scripts work.
Method 2: Compiling Node.js from sources yourself
Attention: this is NOT recommended. You should use NVM or Nave instead.
$ cd /usr/local/src/
Install Node.js and Express.js with Nginx on Debian Lenny | Fre...
1 of 6
12/14/11 10:28 AM
$ sudo wget
$ sudo tar xzvf node-v0.4.8.tar.gz
$ cd node-v0.4.8
$ sudo ./configure
$ sudo make
$ sudo make install
$ whereis node
$ node -v
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"
$ cd
$ curl | 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
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,
makes it better for many others." I think they are both pretty great, so it's a matter of taste, mayb
Nave allows us to run multiple versions of node in parallel and switch between them, similar to how r
and virtualenv do it for Ruby and Python respectively:
$ cd /usr/local/src/npm/
$ sudo npm install -g nave
$ nave -v
First Express App
Let's create our first Express app now
$ sudo mkdir /opt/apps
$ sudo chmod -R 775 /opt/apps/
$ sudo chown ${USER}:webmaster -R /opt/apps/
$ mkdir /opt/apps/firstapp
Let's install
web framework for Node.js under our project:
$ cd /opt/apps/firstapp
$ npm install express
create /opt/apps/firstapp/app.js with the following source code:
/* Module dependencies. */
var express = require('express');
var app = express.createServer();
Install Node.js and Express.js with Nginx on Debian Lenny | Fre...
2 of 6
12/14/11 10:28 AM
app.get('/', function(req, res){
res.send('Hello World');
and save.
You can start your Node/Express.js app from shell with:
$ node /opt/apps/firstapp/app.js
After which, if you go to 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
node.js is its non-blocking architecture, it makes sense to front-end it with a non-blocking web-serv
er as
well. Let's see how we can route our node.js app through Nginx.
Installing Nginx
Disclaimer: instructions adapted from a
SliceHost Tutorial
$ sudo apt-get install libc6 libpcre3 libpcre3-dev libpcrecpp0 libssl0.9.8 libssl-dev zlib1g zlib1g-d
ev lsb-base
$ cd /usr/local/src
$ cd /usr/local/src/
$ sudo wget
$ sudo tar xzvf nginx-1.0.4.tar.gz
$ cd nginx-1.0.4
$ sudo ./configure --sbin-path=/usr/local/sbin --with-http_ssl_module
$ sudo make && sudo make install
$ sudo ln -s /usr/local/nginx/conf /etc/nginx
Configure Nginx
Edit file: /etc/nginx/nginx.conf and insert the following line at the end of the "http" section (typi
before the very last closing curly brace "}" in the file, unless Nginx changes the default config fil
include sites-enabled/*;
Create folder "/etc/nginx/sites-enabled/" and edit /etc/nginx/sites-enabled/firstapp.conf file with c
similar to:
server {
listen 80;
# Let's put all static files like images, js and css in sub-folder: public
root /opt/apps/firstapp/public;
# static content
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
# access_log off;
Install Node.js and Express.js with Nginx on Debian Lenny | Fre...
3 of 6
12/14/11 10:28 AM
expires 15d;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_redirect off;
gzip on;
gzip_comp_level 2;
gzip_proxied any;
gzip_min_length 1000;
gzip_disable "MSIE [1-6]\."
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml a
pplication/xml+rss text/jav
: 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 (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 se
static content (images, css, javascript etc.) directly from Nginx (subfolder "public"), bypassing Nod
Bundling Dependencies
Instead of manually installing required modules, you can define dependencies of your app declarativel
and let npm manage it for you. If you are familiar with Ruby On Rails, this is a process similar to u
Gemfiles and Bundler.
First, you need to create a package.json in the same folder where your app.js lives:
{ "name" : "app"
, "version" : "1.0"
, "description" : "First Steps With Node.js"
, "author" : "Node Noob"
, "main" : "./app.js"
, "dependencies" :
Install Node.js and Express.js with Nginx on Debian Lenny | Fre...
4 of 6
12/14/11 10:28 AM
"express" : ">= 2.4"
, "jade" : "0.15.13"
, "expresso"
, "should"
after which you can run either:
$ npm bundle
or in newer versions of npm, just:
$ npm install
to get all the required dependencies installed.
$npm help json
for more details about the format of the package file.
Startup and Management
One last step is to create and configure Nginx startup scripts, so that if the server restarts, Nginx
back up automatically. Slicehost has a great article for the detailed steps of achieving just that, s
o you
should refer to it:
For daemonizing Node process itself, you may want to review this code snippet:
, but you are probably much better off using Upstart (especially on
Debian Squeeze which comes pre-installed with one). For more information read Tim Smart's blog post:
Multi-Process Node.js
Node.js is a single-threaded system. Yes, you heard it right. There is only one thread. Due to its ev
loop-based asynchronous and non-blocking nature it scales great even as a single thread, but if you a
running on a multi-core CPU you should want to start multiple Node.js processes so you can leverage
available cores.
Currently, the best solution is to start multiple instances of a node.js app and load-balance them wi
There're multiple projects that can aid in such setup. Most notably:
(a fork of a module in
Multi Node
. Personally I really like Cluster since it works very well
and provides hot code deployment in addition to base functionality. Please let us know if you find ot
good solutions.
You should also consider
for monitoring a node.js
process and restarting it in case it dies.
Next Steps
Now that we have written our first, tiny Express.js app, we should mention some other
resources/projects that build on top of the stack and make web-development with Node/Express easier
and more fun:
Install Node.js and Express.js with Nginx on Debian Lenny | Fre...
5 of 6
12/14/11 10:28 AM
Submitted by
(not verified) on Tue, 06/14/2011 - 08:32.
Thanks for noting these steps down. I just used most of these and worked fine. One thing to note,
though - "sudo git clone" doesn't work. It should be "sudo git clone
Thanks, corrected.
Submitted by irakli on Fri, 06/24/2011 - 13:47.
Martin, thanks for catching the typo. I updated original post to fix it.
Submitted by
(not verified) on Tue, 07/19/2011 - 07:21.
I found this post extremely helpful, thank you.
I use Stylus within express for shorthand CSS (as suggested by the express screen-cast tutorial) and
hence made one small tweak by removing css from the static file types served by Nginx within
firstapp.conf. If you don't, express will not compile the native css properly (and it will seem like
your CSS
never updates).
Great comment. Thank you,
Submitted by irakli on Mon, 09/05/2011 - 02:44.
Great comment. Thank you, Will.
Install Node.js and Express.js with Nginx on Debian Lenny | Fre...
6 of 6
12/14/11 10:28 AM