我可以用 Node.js 替换 Apache 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5346055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 13:57:34  来源:igfitidea点击:

Can I Replace Apache with Node.js?

apachenode.js

提问by Rick

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").

我有一个网站在 CentOS 上运行,使用常见的可疑程序(Apache、MySQL 和 PHP)。自从这个网站最初推出以来,它已经发展了很多,现在我想用它做一些更有趣的事情——即实时通知。从我读过的内容来看,Apache 处理得不好。我想知道我是否可以用 Node.js 替换 Apache(所以不是“ LAMP”而是“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.

我试过在网上搜索解决方案,但没有找到。如果我正确地解释了我读过的内容,似乎大多数人都在说 Node.js 可以同时替换 Apache 和 PHP。不过,我有很多现有的 PHP 代码,所以我更愿意保留它。

In case it's not already obvious, I'm pretty confused and could use some enlightenment. Thanks very much!

如果这还不是很明显,我很困惑,可以使用一些启示。非常感谢!

采纳答案by sarnold

If you're prepared to re-write your PHP in JavaScript, then yes, Node.js can replace your Apache.

如果您准备用 JavaScript 重新编写 PHP,那么是的,Node.js 可以替代您的 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.

如果您在服务器和客户端之间放置一个以反向代理模式运行的 Apache 或 NGINX 实例,您可以在 Node.js 上处理 JavaScript 中的一些请求和 Apache 托管的 PHP 中的一些请求,直到您可以完全替换所有 PHP使用 JavaScript 代码。这可能是一种快乐的媒介:在 Node.js 中使用您的 WebSockets,在 Apache + PHP 中进行更平凡的工作。

回答by yojimbo87

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 的事件/非阻塞架构,它可能比 Apache 更快,但您可能无法找到替代某些 Apache 功能的模块/库。

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.

Node.js 本身是一个轻量级的低级框架,它使您能够相对快速地构建服务器端内容和 Web 应用程序的实时部分,但 Apache 提供了更广泛的配置选项和“经典”面向 Web 服务器的功能。

I would say that unless you 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).

我会说,除非你想用基于 node.js 的 web 应用程序框架(比如 express.js)替换 PHP,否则你应该继续使用 Apache(或者如果你有性能问题,可以考虑迁移到 Nginx)。

回答by youurayy

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.

我相信 Node.js 是 Web 服务的未来,但如果你有很多现有的 PHP 代码,Apache/MySQL 是你最好的选择。Apache 可以配置为将请求代理到 Node.js,或者 Node.js 可以将请求代理到 Apache,但我相信在这两种情况下都会损失一些性能,尤其是在第一种情况下。不过,如果您运行的网站流量不是很高,那也没什么大不了的。

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

我刚刚注册到 stackoverflow,我还不能对接受的答案发表评论,但今天我创建了一个简单的 Node.js 脚本,它实际上使用 sendfile() 通过 HTTP 协议提供文件。(接受的答案链接到的现有示例仅使用裸TCP协议发送文件,我找不到HTTP示例,所以我自己写了。)

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.

所以我想有人可能会觉得这很有用。通过 sendfile() 操作系统调用提供文件不一定比通过“用户空间”复制数据时快,但最终会减少 CPU 和 RAM 的使用,因此能够处理比经典方式更多的连接。

The link: https://gist.github.com/1350901

链接:https: //gist.github.com/1350901

回答by Richard Holland

PreviousSO post describing exactly what im saying (php + socket.io + node)

以前的SO 帖子准确地描述了我在说什么(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.

我认为您可以在 somehost:8000 上使用 socket.io 建立一个节点服务器,并将 socket.io 客户端代码打到标签中,并且只需最少的工作就可以让您现有的应用程序与 socket.io(实时宝贝)一起摇摆,而无需大量工作。

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.

虽然节点可以是您唯一的后端服务器,但请记住,节点喜欢名副其实并成为节点。不久前,我查看了 Ryan Dahl 给 PHP 用户组的一个演讲,他提到了与几个节点进程进行工作和相互交谈的愿景相关的名称节点。

回答by Wolfgang Kuehn

Its LAMP versus MEAN nowadays. For a direct comparison see http://tamas.io/what-is-the-mean-stack.

现在的 LAMP 与 MEAN。如需直接比较,请参阅http://tamas.io/what-is-the-mean-stack

Of course M, E and A are somewhat variable. For example the more recent koamay replace (E)xpress.

当然,M、E 和 A 在某种程度上是可变的。例如,最近的koa可能会替换 (E)xpress。

However, just replacing Apache with Node.js is probably not the right way to modernize your web stack.

但是,仅用 Node.js 替换 Apache 可能不是实现 Web 堆栈现代化的正确方法。