Tuesday, May 29, 2012

Can I Replace Apache with Node.js?


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!


Source: Tips4all

4 comments:

  1. 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.

    You will probably always want an Apache or nginx running as a boring web server to handle serving all the static files: the sendfile(2) system call makes serving static pages from a real web server very fast (it removes the 'double-copy' of copying data into the process memory from disk, only to copy the memory into the kernel for the network card -- by telling the kernel which filedescriptor's contents you want sent over which socket, the kernel can arrange to copy data directly from disk to kernel memory for the network card! woot.) I don't know if node.js has easy access to the sendfile(2) syscall, but I would be surprised, so I would assume running an nginx to handle static files would be worth having two complete servers loaded into memory simultaneously.

    ReplyDelete
  2. 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 don't 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).

    ReplyDelete
  3. 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

    ReplyDelete
  4. 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.

    ReplyDelete