Javascript 部署生产 Node.js 服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8386455/
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
Deploying a production Node.js server
提问by David Chouinard
I've written a Node.js app, I'm looking to get it running on one of our production machines. This seems like a pretty common request yet I can't find an adequate solution. Is there not established solutions for deploying production Node.js apps?
我已经编写了一个 Node.js 应用程序,我希望让它在我们的一台生产机器上运行。这似乎是一个很常见的请求,但我找不到合适的解决方案。是否没有用于部署生产 Node.js 应用程序的既定解决方案?
The app is simple (<100 LOC), but needs to be very efficient, reliable and could run continuously for years without restarting. It's going to be run on a large site, with dozens of connections/second. (the app is not used as a webserver, it only has a JSON API)
该应用程序很简单(<100 LOC),但需要非常高效、可靠,并且可以连续运行多年而无需重新启动。它将在一个大型站点上运行,每秒有几十个连接。(该应用程序不用作网络服务器,它只有一个 JSON API)
Here are the approaches I've considered but I'm still not sure about:
以下是我考虑过的方法,但我仍然不确定:
Using a framework (eg. Express)
使用框架(例如 Express)
Because the app needs to be high performance and is so simple, adding bloat in the form of a framework is something I want to avoid.
由于应用程序需要高性能且非常简单,因此以框架的形式添加膨胀是我想要避免的。
Starting the server with nohup
启动服务器 nohup
The main problem here is with exception handling, we (obviously) don't want the entire server to crash because of an exception. From what I understand, wrapping the entire app in a try {} catch {}
loop won't help because the Javascript interpreter is left in an unpredictable state after an exception. Is that correct?
这里的主要问题是异常处理,我们(显然)不希望整个服务器因为异常而崩溃。据我所知,将整个应用程序包装在一个try {} catch {}
循环中无济于事,因为 Javascript 解释器在发生异常后处于不可预测的状态。那是对的吗?
Using something like Forever
使用 Forever 之类的东西
I've installed Forever in a FreeBSD machine of ours and it was very buggy. It ended up spawning endless processes that couldn't be killed from Forever. I had to run kill -9
to get my machine back and I don't feel too confident about running a production app on Forever. It also seems that Upstart (similar tool, but more generic) won't run on FreeBSD.
我已经在我们的 FreeBSD 机器上安装了 Forever,它有很多问题。它最终产生了无法从 Forever 杀死的无尽进程。我不得不运行kill -9
才能恢复我的机器,而且我对在 Forever 上运行生产应用程序不太有信心。Upstart(类似的工具,但更通用)似乎也不会在 FreeBSD 上运行。
Hosted solutions (eg. Heroku, Rackspace, Amazon EC2, etc.)
托管解决方案(例如 Heroku、Rackspace、Amazon EC2 等)
This is probably the simplest solution, but we already have a the serious hardware for the rest of our webservers. For financial considerations, it doesn't make sense.
这可能是最简单的解决方案,但我们已经为其余的网络服务器配备了重要的硬件。出于财务考虑,这没有意义。
Surely there must be some established solution to this? Am I missing something?
当然,必须有一些既定的解决方案吗?我错过了什么吗?
采纳答案by alessioalex
- You should really really use a framework (I recommend something like Express since it was battle-tested) unless you want to deal with sessions, cookies, middleware etc by yourself. Express is really light.
- Starting the server with nohup: you shouldn't do that, just start it with the regular "node" command. Also Express wraps the routes in a try-catch, so your server won't crash in a route. However if your server does have a serious problem, you shouldn't fear restarting it (besides, if you have 2-3 processes at least, only one will die, so there will be at least 1-2 remaining and the user won't feel a thing).
- For monitoring I personally prefer something more at the OS-level such as Upstartand Monit.
- Hosting solution: since you already have your own serious hardware stuff, no need to invest money in something else. Just use a load-balancer (maybe nginx or node-http-proxy) to proxy stuff.
- 你真的应该使用一个框架(我推荐像 Express 这样的框架,因为它经过了实战测试),除非你想自己处理会话、cookie、中间件等。快递真的很轻。
- 使用 nohup 启动服务器:您不应该这样做,只需使用常规的“node”命令启动它。此外,Express 将路由封装在 try-catch 中,因此您的服务器不会在路由中崩溃。但是,如果您的服务器确实存在严重问题,则不必担心重新启动它(此外,如果您至少有 2-3 个进程,则只有一个会死掉,因此至少会剩下 1-2 个,用户不会感觉不到什么)。
- 对于监控,我个人更喜欢操作系统级别的东西,例如Upstart和Monit。
- 托管解决方案:由于您已经拥有自己的重要硬件,因此无需在其他方面投入资金。只需使用负载平衡器(可能是 nginx 或 node-http-proxy)来代理内容。
回答by helpermethod
See Hosting Node Apps.
请参阅托管节点应用程序。
This tutorial walks you through setting up a server that can host node.js apps for server-side JavaScript applications. Right now, the node.js hosting options boil down to running node daemon processes that talk to a web server. Most web servers can proxy connections to a different port, so you'll be able to use Apache or nginx to do this.
本教程将引导您设置一个服务器,该服务器可以为服务器端 JavaScript 应用程序托管 node.js 应用程序。现在,node.js 托管选项归结为运行与 Web 服务器通信的节点守护进程。大多数 Web 服务器可以代理到不同端口的连接,因此您可以使用 Apache 或 nginx 来执行此操作。
回答by Sunil Hirole
Try using pm2 it is simple and intuitive CLI, installable via NPM. Just start your application with PM2 and your application is ready to handle a ton of traffic
尝试使用 pm2,它是简单直观的 CLI,可通过 NPM 安装。只需使用 PM2 启动您的应用程序,您的应用程序就可以处理大量流量
How to set up a node js application for production using pm2
回答by RyanWilcox
There are three questions here, I think.
我认为这里有三个问题。
Question 0: "Should I use a framework for my node app?"
问题 0:“我应该为我的节点应用程序使用框架吗?”
Question 1: "How do I run node servers on production machines?"
问题 1:“如何在生产机器上运行节点服务器?”
Question 2: "How do I deploy node apps to production".
问题 2:“如何将节点应用程序部署到生产环境”。
For Question 1, I really like Cluster(although the latest Node version has something like that built in, so you might check that out). I've had good success with something like Monit/Upstart to monitor OS level events and make sure your servers are in good health. (This was monitoring N clusters of Ruby Thin servers, but same thing).
对于问题 1,我真的很喜欢Cluster(尽管最新的 Node 版本内置了类似的功能,因此您可以查看一下)。我使用 Monit/Upstart 之类的工具来监控操作系统级别的事件并确保您的服务器运行状况良好,这已经取得了很大的成功。(这是监控 N 个 Ruby Thin 服务器集群,但同样的事情)。
Depending on the traffic you may want to run cluster on multiple machines, then putting a load balancer in front of that. This depends on your traffic, how long requests take to complete / how long you block the event loop, and how many processors/node instances you launch per machine.
根据您可能希望在多台机器上运行集群的流量,然后在其前面放置一个负载均衡器。这取决于您的流量、完成请求所需的时间/阻塞事件循环的时间以及每台机器启动的处理器/节点实例数。
A framework gives you better error handling, and catches errors that would exit normal node.js apps. If you do it without a framework, make sure you read up on error handling in node.js.
框架为您提供更好的错误处理,并捕获会退出普通 node.js 应用程序的错误。如果您在没有框架的情况下执行此操作,请确保您阅读了 node.js 中的错误处理。
For Question 2, I don't think the node community has a good deploy standard yet. You could try using Ruby's Capistrano tool (and here's a blog entry talking about deploying cluster with Capinstrano).
对于问题 2,我认为节点社区还没有一个好的部署标准。您可以尝试使用 Ruby 的 Capistrano 工具(这里有一篇关于使用 Capinstrano 部署集群的博客文章)。
The bad thing about Capistrano is that it makes some assumptions that might not be true (ie: that you're deploying a Rails project), so you may end up fighting with the framework a lot.
Capistrano 的坏处是它做出了一些可能不正确的假设(即:您正在部署一个 Rails 项目),因此您最终可能会与框架发生很多冲突。
My goto deployment solution in general is Python's Fabrictool, which gives you deployment tools and lets you do what you need to do.
我的 goto 部署解决方案通常是 Python 的Fabric工具,它为您提供部署工具并让您执行需要执行的操作。
Another deployment option is "the cloud", with things like Nodester: let them take care of it.
另一个部署选项是“云”,使用Nodester 之类的东西:让他们来处理它。
回答by Wyatt Anderson
You might get better answers over on ServerFault, but there's a description of one user's experience hereusing supervisord
. You're going to need to use some sort of process watcher to keep the node
process alive, and another common recommendation seems to be to reverse-proxy connections to the node
process somehow. I'd probably vote for nginx
(this way you can have nginx
handle the logging, authentication, or any other higher-level HTTP features you need as opposed to somehow baking them into node), but the aforementioned article mentions haproxy
in the comments here and there which may be more lightweight. Your choice of reverse-proxy will probably depend largely on whether or not you need WebSocket support.
您可能会在 ServerFault 上得到更好的答案,但这里有一个用户使用supervisord
. 您将需要使用某种进程观察器来保持node
进程处于活动状态,另一个常见的建议似乎是以node
某种方式反向代理到进程的连接。我可能会投票支持nginx
(通过这种方式,您可以nginx
处理日志记录、身份验证或您需要的任何其他更高级别的 HTTP 功能,而不是以某种方式将它们烘焙到节点中),但上述文章haproxy
在这里和那里的评论中提到可能更轻。您对反向代理的选择可能在很大程度上取决于您是否需要 WebSocket 支持。
I'm not sure any more "standard" workflow exists for node just yet; it's not quite as mature as something like Rails that has a myriad of ways to keep a webapp running.
我还不确定节点是否存在更多“标准”工作流程;它不像 Rails 那样成熟,它有无数种方法来保持 web 应用程序运行。
回答by Ryan Olds
The guys at Cloudkick wrote an an excellent solution to this. It's called Cast
, http://cast-project.org/.
Cloudkick 的人为此写了一个很好的解决方案。它被称为Cast
, http://cast-project.org/。
Install cast on your server and on your workstation. You start the cast-agent on the server and have your workstation sign with the servers cast instance. You can then create "bundles", upload them to the server, create/upgrade/destroy from them as well as start/stop the your instances. Cast will automatically restart your services when they crash. You can also tail the stdout/strerr remotely as well as get a list of running instances and PID#s and manage your instances/servers from your workstation (no SSHing required). The docs are slightly out of date, but the results are worth the little bit of extra work. All of the interactions/commands are over HTTPS and a RESTful API.
在您的服务器和工作站上安装 cast。您在服务器上启动 cast-agent 并让您的工作站与服务器 cast 实例进行签名。然后,您可以创建“捆绑包”,将它们上传到服务器,创建/升级/销毁它们以及启动/停止您的实例。Cast 会在您的服务崩溃时自动重新启动它们。您还可以远程跟踪 stdout/strerr 以及获取正在运行的实例和 PID#s 的列表,并从您的工作站管理您的实例/服务器(不需要 SSH)。文档有点过时,但结果值得做一点额外的工作。所有交互/命令都通过 HTTPS 和 RESTful API。
Prior to this I was doing all the upgrades by hand with SCP/SSH. We has supervise
keeping things up. We haven't looked back.
在此之前,我使用 SCP/SSH 手动完成所有升级。我们一直在坚持supervise
。我们没有回头。