Node.js and Apache – Is It Bad Practice to Run Node.js and Apache in Parallel?

node.jsPHP

I have an idea in mind and would like to know if that's the way to go for my end application.

Think of my application as a social networking system in which I want to implement chat functionality. For that, I'd like to push data from the server to the client. I have heard I could use Node.js for that.

In the meanwhile, I want a 'regular' system for posting status updates and such, for which I'd like to use PHP and an apache server.

The only way I can think of is having Node.js and apache running parallel. But is that the way to go here? I'd think there would be a somewhat neater solution for this.

Best Answer

Running several web server processes in parallel is not a problem at all, people do it all the time.

However, since only one process can ever listen on any given port, they can't both run on port 80 directly, so you'll have to dispatch somehow. An easy solution is to run the Node.js application on a custom port (anything above 1024 should be fair game, but do a bit of research first, and make it configurable), then set up a reverse proxy in apache to pass it through on port 80 (or 443 if you need SSL) for the URLs that should go to Node.

Reverse proxying from Node into apache might also be possible, and if the Node application handles the most performance-critical calls, it may or may not be a better solution.

A thirds solution is to serve them both on different ports and do the reverse proxying in a separate process, or even a separate machine. The advantage of this is that splitting the setup over more application servers (e.g. dedicated boxes for the Node and PHP parts) later is relatively trivial (but then, it isn't much harder for the other two solutions either).

The nice part about having Apache on the public-facing side is that you can use its various mature features such as HTTP authentication, rewriting, SSL, virtual hosts, static file serving, etc., so the Node part can focus on the essentials. No need to reinvent the wheel there.

Related Topic