Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache and Node.js on the Same Server

Tags:

node.js

apache

I want to use Node because it's swift, uses the same language I am using on the client side, and it's non-blocking by definition. But the guy who I hired to write the program for file handling (saving, editing, renaming, downloading, uploading files, etc.), he wants to use apache. So, I must:

  1. Convince him to use Node (he's giving up little ground on that)

  2. Figure out how to upload, download, rename, save, etc. files in node or

  3. I must install apache and node on the same server.

Which is the most favorable situation, and how do I implement that?

like image 918
Matt Avatar asked Mar 22 '12 22:03

Matt


People also ask

Can you run NodeJS and Apache on the same server?

Since we cannot run both node. js server and the Apache server to listen on same port, we need to config Apache to act like a reverse proxy and pass the request to node. js application for a specific URL. For example, if you have the Apache server running on localhost and want to run node.

Can we host NodeJS in Apache?

To deploy an app to an Apache virtual host's root path, the following steps must be taken: Add a virtual host entry to your Apache configuration file. The virtual host's document root must point to your application's public subdirectory. The Apache per-directory permissions must allow access to this directory.

Can Apache run JavaScript?

While Javascript is a client-side executing coding language, it still has to have permission from the Apache Web server in order to run properly. If Apache is configured incorrectly, the Javascript on your Web server may not work properly.


1 Answers

Great question!

There are many websites and free web apps implemented in PHP that run on Apache, lots of people use it so you can mash up something pretty easy and besides, its a no-brainer way of serving static content. Node is fast, powerful, elegant, and a sexy tool with the raw power of V8 and a flat stack with no in-built dependencies.

I also want the ease/flexibility of Apache and yet the grunt and elegance of Node.JS, why can't I have both?

Fortunately with the ProxyPass directive in the Apache httpd.conf its not too hard to pipe all requests on a particular URL to your Node.JS application.

ProxyPass /node http://localhost:8000 

Also, make sure the following lines are NOT commented out so you get the right proxy and submodule to reroute http requests:

LoadModule proxy_module modules/mod_proxy.so LoadModule proxy_http_module modules/mod_proxy_http.so 

Then run your Node app on port 8000!

var http = require('http'); http.createServer(function (req, res) {   res.writeHead(200, {'Content-Type': 'text/plain'});   res.end('Hello Apache!\n'); }).listen(8000, '127.0.0.1'); 

Then you can access all Node.JS logic using the /node/ path on your url, the rest of the website can be left to Apache to host your existing PHP pages:

enter image description here

Now the only thing left is convincing your hosting company let your run with this configuration!!!

like image 126
Steven de Salas Avatar answered Sep 17 '22 14:09

Steven de Salas