6

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.

3
  • Combine how? You can have node.js and apache running in parallel, and there's a variety of ways to pass data between them. What exactly are you trying to do?
    – yannis
    May 21, 2013 at 15:59
  • @YannisRizos sorry, in parallel, yes.
    – user76821
    May 21, 2013 at 16:24
  • Also: Technical aspects of running Node.js and apache in parallel
    – user76821
    May 21, 2013 at 16:35

3 Answers 3

13

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.

1
  • Actually anything above 1024 is fair game -- or at least out of the potential range of IANA reserved ports. Nov 8, 2013 at 18:47
10

There's no technical reason you couldn't do this. I did something very similar for a simple game I wrote to play with Node. The pages were served by Apache and written in PHP, but then I had an HTML5 canvas and I initialized a socket to my Node server. Both Apache and Node can run happily side by side on the same machine, just different ports.

1
  • I am a novice to node. But, if you use different port except 80, you must have to append that port with the url, am I right?
    – ssi-anik
    Mar 13, 2016 at 1:27
1

You could use node.js as the server:

you can set up a server in node, adding the following modules:

  • express: static html server
  • ws: websocket support
  • sphp: PHP support

You can make a simple and fast webserver, with websockets binding to port 80 and using PHP as server side scripting language. You can almost use the example server in sphp, as is.

I should mention that I'm the author of sphp. There are other options for PHP integration into node.

You install npm (Node package manager) to your system and then install the node modules you need:
In project directory

npm install express ws sphp 

then you copy the example.js and modify it to needs.

See examples her: https://github.com/paragi/sphp
For the basics see also: https://github.com/paragi/was

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy