Jump to content
MakeWebGames

Running Node.js alongside Apache2.x


Recommended Posts

NOTE: This tutorial is for *nix variants only!

With my first encounters with Node.js I was tempted not to run Node.js applications on a live server.

I really couldn't afford to rewrite all my script to node. I didn't want to sacrifice my Apache.

After reading through many of setups of Node.js and Apache2.x, I began to get frustrated with what most were suggesting.

Some even went as far as to say you need to setup a reverse proxy with Node.js to redirect requests to Apache.

That's quite ludicrous.

So let me give you possible setup.

Requirements: 2 static IP addresses and an additional Address record.

Assumptions: basic sysadmin knowledge of your distro, and a working installation of Apache2.x.


Step 1

Getting Node.js

To get started, get Node.js.

There are are many ways to get it, but I would not recommend installing from the source if you aren't familiar with it.

A package manager would be your best bet, see https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager.

Next, test that Node.js is installed and working on your system:

# node -v
v0.6.12

 


Step 2

Configuring Apache2.x

Now that you have Node.js and Apache2.x installed, it's time to make them work together separately.

Firstly, edit your Apache2.x configuration to listen only on one IP as default configuration is to listen on all available interfaces.

Sample:

/etc/apache2/ports.conf

Listen 123.45.67.111:80

# this should be in a <IfModule /> wrapper
Listen 123.45.67.111:443

Apache will need to be restarted to take effect, so run:

(As for those asking, Apache needs to be restarted for the IP addresses to be released)

# service apache2 restart
* Restarting web server apache2                                         [ OK ]

Confirm that Apache is now running only on a specific IP address:

# netstat -ln | grep 80
tcp        0      0 123.45.67.111:80       0.0.0.0:*               LISTEN

 

If everything works, then you are still good.

If not, please retrace your steps carefully.


Step 3

Setting up DNS

Now that we have Apache only using one of our two IP addresses, we can go one step further and begin preparing for a Node.js application.

At this point, you have two choices:

a. Use a domain name specifically for a Node.js application, or

b. Use a sub-domain specifically for a Node.js application.

I will definitely suggest the latter.

You will need access to your DNS records for this, but luckily it's a simple addition.

What you need to do, is create a sub-domain with an Adress(A) record which you point to the second IP address of your server.

I'm not going into much detail here, as DNS is a whole other subject.

But after you have done that, confirm that the alterations are correct:

# dig sub.domain.com | grep IN
;sub.domain.com.               IN      A
sub.domain.com.        13744   IN      A       123.45.67.222

...Other results...

If everything is in order, you are good to go and create an application, if not, retrace your steps carefully and make sure that you set up the A record correctly.


Step 4

Your Node.js Application

Well then, that was quite simple wasn't it?

But now let's actually get something running.

Any Node.js application will work, as long as you specify the IP it should listen on.

Defining which IP Node.js should listen on can be read about here: http://nodejs.org/api/http.html#http_server_listen_port_hostname_callback

Here's a simple application for you to use, just to test everything.

server.js

var http = require('http');
http.createServer(function (req, res) {
 res.writeHead(200, {'Content-Type': 'text/plain'});
 res.end('Hello World\n');
}).listen(80, '123.45.67.222');
console.log('Server started.');

And then, simply run it:

node server.js

Or, if you want to run it in the background:

nohup node server.js &

 


Additional FAQ

Where should I put my Node.js application?

I would suggest alongside your website's Apache web-directory.

On Debian systems this is located at /var/www/domain.com, so similarly create a directory /var/www/sub.domain.com to contain the files of your Node.js application.

 

Any questions and comments are welcome.

Edited by Spudinski
Link to post
Share on other sites

In theory that sounds wonderful, but in reality isn't not very plausible unless you use Node specifically for web services or a daemon.

Node JS has a lot of functionality for low level development, and thus one would be robbing it of it's really raw power when tunnelling requests through a reverse proxy.

Both methods mentioned are plausible though, thanks for the mention.

Link to post
Share on other sites
Nice share spud! Definitely going to read up on these posts and such.

Thanks man.

After our little discussion about what to write with regards to Node, I think I'll do something basic still: MySQL.

Might do SQLite, but the basis for both is the same in Node.js.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...