简体   繁体   English

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

[英]Can I Replace Apache with Node.js?

I have a website running on CentOS using the usual suspects (Apache, MySQL, and PHP). 我使用常见的嫌疑人(Apache,MySQL和PHP)在CentOS上运行一个网站。 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. 根据我的阅读,Apache处理这个问题很糟糕。 I'm wondering if I can replace just Apache with Node.js (so instead of " LAMP " it would "LNMP"). 我想知道我是否可以用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. 如果我正确地解释了我读过的东西,似乎大多数人都说Node.js可以同时替换Apache和PHP。 I have a lot of existing PHP code, though, so I'd prefer to keep it. 我有很多现有的PHP代码,所以我宁愿保留它。

In case it's not already obvious, I'm pretty confused and could use some enlightenment. 如果它还不是很明显,我很困惑,可以使用一些启示。 Thanks very much! 非常感谢!

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. 如果在服务器和客户端之间放置以反向代理模式运行的Apache或NGINX实例,则可以在Node.js上处理JavaScript中的一些请求,并在Apache托管的PHP中处理一些请求,直到您可以完全替换所有PHP使用JavaScript代码。 This might be the happy medium: do your WebSockets work in Node.js, more mundane work in Apache + PHP. 这可能是一个快乐的媒介:你的WebSockets是否在Node.js中工作,在Apache + PHP中更平凡的工作。

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

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. 我相信Node.js是Web服务的未来,但如果你有很多现有的PHP代码,Apache / MySQL是你最好的选择。 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. Apache可以配置为代理对Node.js的请求,或者Node.js可以代理对Apache的请求,但我相信在这两种情况下都会丢失一些性能,尤其是在第一种情况下。 Not a big deal if you aren't running a very high traffic website though. 如果你没有运行一个非常高流量的网站,这没什么大不了的。

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. 我刚刚注册到stackoverflow,我还不能评论已接受的答案,但今天我创建了一个简单的Node.js脚本,它实际上使用sendfile()通过HTTP协议提供文件。 (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.) (接受的答案链接到的现有示例仅使用裸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()OS调用提供文件不一定比通过“用户域”复制数据更快,但它最终利用CPU和RAM更少,因此能够处理比传统方式更多的连接。

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

Previous SO 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用户组的一段时间的谈话,他提到了名称节点,该节点与几个节点进程的工作和相互交谈的愿景有关。

Its LAMP versus MEAN nowadays. 它的LAMP与MEAN相比如今。 For a direct comparison see http://tamas.io/what-is-the-mean-stack . 有关直接比较,请参阅http://tamas.io/what-is-the-mean-stack

Of course M, E and A are somewhat variable. 当然,M,E和A有些变化。 For example the more recent koa may replace (E)xpress. 例如,最近的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堆栈现代化的正确方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM