154

I have a website running on CentOS using the usual suspects (Apache, MySQL, and PHP). Since the time this website was originally launched, it has evolved quite a bit and now I'd like to do fancier things with it—namely real-time notifications. From what I've read, Apache handles this poorly. I'm wondering if I can replace just Apache with Node.js (so instead of "LAMP" it would "LNMP").

I've tried searching online for a solution, but haven't found one. If I'm correctly interpreting the things that I've read, it seems that most people are saying that Node.js can replace both Apache and PHP together. I have a lot of existing PHP code, though, so I'd prefer to keep it.

In case it's not already obvious, I'm pretty confused and could use some enlightenment. Thanks very much!

Jørn E. Angeltveit
  • 3,029
  • 3
  • 22
  • 53
Rick
  • 1,551
  • 2
  • 10
  • 4

5 Answers5

86

If you're prepared to re-write your PHP in JavaScript, then yes, Node.js can replace your Apache.

If you place an Apache or NGINX instance running in reverse-proxy mode between your servers and your clients, you could handle some requests in JavaScript on Node.js and some requests in your Apache-hosted PHP, until you can completely replace all your PHP with JavaScript code. This might be the happy medium: do your WebSockets work in Node.js, more mundane work in Apache + PHP.

iono
  • 2,575
  • 1
  • 28
  • 36
sarnold
  • 102,305
  • 22
  • 181
  • 238
  • 1
    I appreciate the quick response! Is nginx vastly superior to Apache? I already have a .htaccess file in place and it would be nice to keep it. Also, I've read that it's pointless to have Apache forward requests to Node.js because then you're losing the advantages of Node.js since you're still going through Apache. Would it be better to have Node.js listen on port 80 for anything in a subfolder named "nodejs" and then anything not in that subfolder could be passed to Apache by Node.js? Apache could listen on some other port like 8000. – Rick Mar 17 '11 at 22:55
  • 22
    @Rick, I'm pretty close to deleting my own answer; [Node can use `sendfile`](http://blog.std.in/2010/09/09/using-sendfile-with-nodejs/), and there is a [module for FastCGI](https://github.com/billywhizz/node-fastcgi-parser) support that might make it easy to serve your PHP through Node as well. As for nginx vs Apache, I've always been a sucker for async-style servers over threaded or multiprocess servers :) but I have found Apache documentation easier to find and read. I'd call that personal preference unless you need to scale to amazing numbers. :) – sarnold Mar 17 '11 at 23:09
  • Thanks for the information! I'm not sure what I'll end up doing, but the first thing I'm going to attempt is having all traffic go through Node, but anything not in the "nodejs" subfolder will be passed to Apache. If this is a bad idea, please let me know. – Rick Mar 21 '11 at 23:52
  • 3
    @Rick You do not want to migrate production stuff to Node before understanding what it is and how it works. Node is not a magic pill to make stuff faster. The event driven / async paradigm is not new, and there are reasons for why it's not used for everything. http://en.wikipedia.org/wiki/Asynchronous_I/O – Øyvind Skaar Feb 17 '12 at 08:50
  • 1
    @Rick Instead of thinking that you should replace what you have, maybe it's better to run Node in addition. Don't think there's any reason to pass everything through node (?), sounds like a bad idea. Just run node on another port or host. – Øyvind Skaar Feb 17 '12 at 09:35
  • 2
    @Øyvind Skaar i agree. Also, for most website (for example Wordpress), the real bottleneck is the database and not the file access. And, if the file access is a problem then cache is always a viable solution. Anyways, for real performance, PHP-APC is a damn cheap trick. – magallanes Dec 05 '12 at 15:54
26

Node.js may be faster than Apache thanks to it's evented/non-blocking architecture, but you may have problems finding modules/libraries which substitute some of Apache functionality.

Node.js itself is a lightweight low-level framework which enables you to relatively quickly build server-side stuff and real-time parts of your web applications, but Apache offers much broader configuration options and "classical" web server oriented features.

I would say that unless you want to replace PHP with node.js based web application framework like express.js then you should stay with Apache (or think about migrating to Nginx if you have performance problems).

Armen Michaeli
  • 8,625
  • 8
  • 58
  • 95
yojimbo87
  • 65,684
  • 25
  • 123
  • 131
11

I believe Node.js is the future in web serving, but if you have a lot of existing PHP code, Apache/MySQL are your best bet. Apache can be configured to proxy requests to Node.js, or Node.js can proxy requests to Apache, but I believe some performance is lost in both cases, especially in the first one. Not a big deal if you aren't running a very high traffic website though.

I just registered to stackoverflow, and I can't comment on the accepted answer yet, but today I created a simple Node.js script that actually uses sendfile() to serve files through the HTTP protocol. (The existing example that the accepted answer links to only uses bare TCP protocol to send the file, and I could not find an example for HTTP, so I wrote it myself.)

So I thought someone might find this useful. Serving files through the sendfile() OS call is not necessarily faster than when data is copied through "user land", but it ends up utilizing the CPU and RAM less, thus being able to handle larger number of connections than the classic way.

The link: https://gist.github.com/1350901

youurayy
  • 1,635
  • 1
  • 18
  • 11
  • 2
    Although very true, I still believe if you stripped apache down do just what you are describing, it would run as fast, and if not, faster than node.js. Apache does a lot of things people don't see or really understand and if you added all the functionality of these web servers to node.js, it would run just as slow as them. As simple example would probably be, http://mynode.js/getfile?file=/etc/shadow – Rahly Mar 28 '14 at 23:13
8

Previous SO post describing exactly what im saying (php + socket.io + node)

I think you could put up a node server on somehost:8000 with socket.io and slap the socket.io client code into tags and with minimal work get your existing app rocking with socket.io (realtime baby) without a ton of work.

While node can be your only backend server remember that node likes to live up to it's name and become a node. I checked out a talk awhile back that Ryan Dahl gave to a PHP Users's group and he mentioned the name node relating to a vision of several node processes doing work and talking with each other.

Community
  • 1
  • 1
Richard Holland
  • 2,663
  • 2
  • 21
  • 35
2

Its LAMP versus MEAN nowadays. For a direct comparison see http://tamas.io/what-is-the-mean-stack.

Of course M, E and A are somewhat variable. For example the more recent koa may replace (E)xpress.

However, just replacing Apache with Node.js is probably not the right way to modernize your web stack.

Wolfgang Kuehn
  • 12,206
  • 2
  • 33
  • 46