Apache and Node.js on an Ubuntu Serer

Node JS & Apache

I’ve been pretty excited to pick up nodejs for awhile now and I sat down today to get the ball rolling. I’ve installed node before and ran the basic ‘hello world’ example at work so I wanted to get right into it. Since I was anxious to at least get something out there on hour zero so I jumped straight into Heroku and created an app. While the process worked great and I got something up I was so excited about node that I committed to picking it up and putting own server.

And that’s the first blocker. Sure - I want to put it on own server… but I run a lot of other things on my boxes. One of those things is a preexisting LAMP stack that hosts some tools that it would be tough to replace or go without. So that’s when my googling started.

I knew going into this that I was going to need to either need a second IP address or use a proxy. I opted for the path of least resistence and most practical: Proxy.

Not surprisingly there are a lot of brain dumps out there on this very subject, but I found them fairly tough to follow. Everyone has a stackoverflow question or forum post on how to tackle this problem and even a few “is this even ok?” questions.

Here’s what I discovered as my own solution:

Install node and npm

This one is pretty straight forward.

Follow these steps and you’ll be set. https://gist.github.com/isaacs/579814#file-node-and-npm-in-30-seconds-sh

My only one add here is that I actually needed to add the local bin folder ~/local/bin to my ~/.bash_profile on MacOSX. I did not have this problem when installing on my Ubuntu box though.

Edit local server, /etc/hosts and open port

I knew I was about to start redirecting from localhost to localhost, so I wanted to ensure that my Ubuntu server knew that my domain.com was the same as 127.0.0.1 to prevent any type of 503’s. Along this line I also went ahead and opened the 5000 port on my network. You can choose any port of your own, anything in that range should work.

Edit Apache

And now for the important stuff… the proxy.

1
2
sudo a2enmod proxy
sudo a2enmod proxy_http

This will install the Apache2 mod_proxy to /etc/apache2/mods-available/ and /etc/apache2/mods-enabled/

Then, I needed to edit my default conf:

1
2
3
4
5
6
7
8
9
ProxyRequests Off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
<Location /node>
ProxyPass http://myawesomedomain.com:5000
ProxyPassReverse http://myawesomedomain.com:5000
</Location>

Then restart apache:
sudo service apache2 restart

Spawn a node instance & Hello World!

Next I went ahead and created two files: web.js and package.json

web.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
var express = require("express");
var app = express();
app.use(express.logger());
app.get('/', function(request, response) {
response.send('Hello World!');
});
app.get('/test', function(request, response) {
response.send('This was only a test');
});
app.use(function(err, req, res, next){
console.error(err.stack);
res.send(500, 'Something broke!');
});
var port = process.env.PORT || 8080;
app.listen(port, function() {
console.log("Listening on " + port);
});

package.json:

1
2
3
4
5
6
7
8
9
10
11
{
"name": "node-example",
"version": "0.0.1",
"dependencies": {
"express": "3.1.x"
},
"engines": {
"node": "0.10.x",
"npm": "1.2.x"
}
}

After this we can then run npm install from our node app directory, then:
sudo node web.js

Point our browsers to myawesomedomain.com/node and… success. :)