Apache and Node.js on the Same Server

前端 未结 10 1535
青春惊慌失措
青春惊慌失措 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:49

    I am assuming that you are making a web app because you refer to Apache and Node. Quick answer - Is it possible - YES. Is it recommended - NO. Node bundles it's own webserver and most websites run on port 80. I am also assuming that there is currently no Apache plugin which is supported by Nodejs and I am not sure if creating a virtual host is the best way to implement this. These are the questions that should be answered by developers who maintain Nodejs like the good folks at Joyent.

    Instead of ports, it would be better to evaluate Node's tech stack which is completely different from most others and which is why I love it but it also involves a few compromises that you should be aware of in advance.

    Your example looks similar to a CMS or a sharing web app and there are hundreds of out of the box apps available that will run just fine on Apache. Even if you do not like any readymade solution, you could write a webapp in PHP / Java / Python or mix n match it with a couple of ready made apps and they are all designed and supported to run behind a single instance of Apache.

    It's time to pause and think about what I just said.

    Now you are ready to decide on which techstack you are going to use. If your website will never use any out of the thousands of ready made apps that require Apache, then go for Node otherwise you must first eliminate the assumptions that I have stated earlier.

    In the end, your choice of techstack is way more important than any individual component.

    I completely agree with @Straseus that it is relatively trivial to use node.js file system api for handling uploads and downloads but think more about what you want from your website in the long run and then choose your techstack.

    Learning Node's framework is easier than learning other frameworks but it is not a panacea. With a slightly more effort (which may be a worthwhile endeavor in itself), you can learn any other framework too. We all learn from each other and you will be more productive if you are working as a small team than if you are working alone and your backend technical skills will also develop faster. Therefore, do not discount the skills of other members of your team so cheaply.

    This post is about a year old and chances are that you have already decided but I hope that my rant will help the next person who is going through a similar decision.

    Thanks for reading.

    0 讨论(0)
  • 2020-11-22 06:51

    I recently ran into this kinda issue, where I need to communicate between client and server using websocket in a PHP based codeigniter project.

    I resolved this issue by adding my port(node app running on) into Allow incoming TCP ports & Allow outgoing TCP ports lists.

    You can find these configurations in Firewall Configurations in your server's WHM panel.

    0 讨论(0)
  • 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!!!

    0 讨论(0)
  • 2020-11-22 06:58

    You can use a different approach such as writing a reverse proxy server with nodejs to proxy both apache and all other nodejs apps.

    First you need to make apache run on a different port other than port 80. ex: port 8080

    Then you can write a reverse proxy script with nodejs as:

    var proxy = require('redbird')({port: 80, xfwd: false);
    
    proxy.register("mydomain.me/blog", "http://mydomain.me:8080/blog");
    proxy.register("mydomain.me", "http://mydomain.me:3000");
    

    Following article describes the whole process of making this.

    RUN APACHE WITH NODE JS REVERSE PROXY – USING REDBIRD

    0 讨论(0)
  • 2020-11-22 06:59

    I was looking for the same information. Finally found the answer from the link on the answer above by @Straseus

    http://arguments.callee.info/2010/04/20/running-apache-and-node-js-together/

    Here is the final solution to run apache website on port 80, node js service on port 8080 and use .htaccess RewriteRule

    In the DocumentRoot of the apache website, add the following:

    Options +FollowSymLinks -MultiViews
    
    <IfModule mod_rewrite.c>
    
    RewriteEngine on
    
    # Simple URL redirect:
    RewriteRule ^test.html$ http://arguments.callee.info:8000/test/ [P]
    
    # More complicated (the user sees only "benchmark.html" in their address bar)
    RewriteRule ^benchmark.html$ http://arguments.callee.info:8000/node?action=benchmark [P]
    
    # Redirect a whole subdirectory:
    RewriteRule ^node/(.*) http://arguments.callee.info:8000/$1 [P]
    

    For the directory level redirect, the link above suggested (.+) rule, which requires one or more character after the 'node/'. I had to convert it to (.*) which is zero or more for my stuff to work.

    Thanks a lot for the link @Straseus

    0 讨论(0)
  • 2020-11-22 07:02

    This question belongs more on Server Fault but FWIW I'd say running Apache in front of Node.js is not a good approach in most cases.

    Apache's ProxyPass is awesome for lots of things (like exposing Tomcat based services as part of a site) and if your Node.js app is just doing a specific, small role or is an internal tool that's only likely to have a limited number of users then it might be easier just to use it so you can get it working and move on, but that doesn't sound like the case here.

    If you want to take advantage of the performance and scale you'll get from using Node.js - and especially if you want to use something that involves maintaining a persistent connection like web sockets - you are better off running both Apache and your Node.js on other ports (e.g. Apache on localhost:8080, Node.js on localhost:3000) and then running something like nginx, Varnish or HA proxy in front - and routing traffic that way.

    With something like varnish or nginx you can route traffic based on path and/or host. They both use much less system resources and is much more scalable that using Apache to do the same thing.

    0 讨论(0)
提交回复
热议问题