Apache and Node.js on the Same Server

前端 未结 10 1793
青春惊慌失措
青春惊慌失措 2020-11-22 06:11

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 pro

10条回答
  •  感动是毒
    2020-11-22 06:53

    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!!!

提交回复
热议问题