且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

同一服务器上的 Apache 和 Node.js

更新时间:2023-11-22 17:57:52

好问题!

有许多使用 PHP 实现的网站和免费网络应用程序在 Apache 上运行,很多人都使用它,因此您可以将一些非常简单的东西混搭在一起,此外,它是一种提供静态内容的简单方法.Node 是一种快速、强大、优雅且性感的工具,具有 V8 的原始功能和没有内置依赖项的扁平堆栈.

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.

我还想要 Apache 的易用性/灵活性以及 Node.JS 的笨拙和优雅,为什么我不能两者兼得?

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

幸运的是 Apache ProxyPass 指令>httpd.conf 将特定 URL 上的所有请求通过管道传输到 Node.JS 应用程序并不难.

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

另外,请确保以下几行没有被注释掉,以便您获得正确的代理和子模块来重新路由 http 请求:

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

然后在端口 8000 上运行您的 Node 应用程序!

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');

然后您可以使用 url 上的 /node/ 路径访问所有 Node.JS 逻辑,网站的其余部分可以留给 Apache 来托管您现有的 PHP 页面:

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:

现在唯一剩下的就是说服您的托管公司让您使用此配置运行!!!

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