Ruby on Rails 服务器选项
为我的 Ruby on Rails 应用程序设置开发服务器的整个问题让我感到困惑。我确信有 WEBrick、Mongrel、Passenger、Apache、Nginx 等等,但我不太了解它们所扮演的不同角色。
我开始使用 WEBrick,现在使用 Mongrel 进行开发。这些服务器是独立的,还是位于 Apache 前面?
我读过有关 Passenger 的内容,但我不太明白它是什么,该网站说“使 Ruby Web 应用程序的部署变得轻而易举”,它会取代 Mongrel 吗?是不是像Capistrano一样,也部署Web应用程序?
请记住,我想测试 SSL,并且我相信 mongrel 不支持这一点,最好的开发服务器设置是什么?
谢谢
The whole issue of setting up a development server for my Ruby on Rails application confuses me. There are WEBrick, Mongrel, Passenger, Apache, Nginx and many more I am sure, and I don't really understand the different roles they play.
I started off using WEBrick, and now I use Mongrel for development. Are these servers stand-alone, or do they sit in front of Apache?
I have read about Passenger and I don't really understand what it is, the site says "makes deployment of Ruby web applications a breeze", does it replace Mongrel? Is it like Capistrano, which also deploys web applications?
Bearing in mind I would like to test SSL, and I believe that's not supported by mongrel, what is the best development server setup?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据上下文,“部署”一词可以有两种含义。您还将 Apache/Nginx 的角色与其他组件的角色混淆了。
历史记录:本文最初写于 2010 年 11 月 6 日,当时 Ruby 应用服务器生态系统还很有限。我于 2013 年 3 月 15 日更新了本文,其中包含生态系统中的所有最新更新。
免责声明:我是 Phusion Passenger(应用程序服务器之一)的作者之一。
Apache 与 Nginx
它们都是 Web 服务器。它们可以提供静态文件,但通过正确的模块,也可以提供动态 Web 应用程序,例如用 PHP 编写的应用程序。 Apache 更流行,功能更多,Nginx 更小、更快,功能更少。
Apache 和 Nginx 都无法为开箱即用的 Ruby Web 应用程序提供服务,为此,您需要将 Apache/Nginx 与某种附加组件结合使用,稍后将对此进行介绍。
Apache 和 Nginx 还可以充当反向代理,这意味着它们可以接受传入的 HTTP 请求并将其转发到另一个也使用 HTTP 的服务器。当该服务器响应 HTTP 响应时,Apache/Nginx 会将响应转发回客户端;稍后您将了解为什么这是相关的。
Mongrel 和其他生产应用程序服务器与 WEBrick
Mongrel 是一个 Ruby“应用程序服务器”: 具体而言,这意味着 Mongrel 是一个应用程序:
Mongrel 侦听此套接字上的 HTTP 请求并将请求数据传递到 Ruby Web 应用程序。
然而 Mongrel 已经相当过时了,现在它不再被维护了。较新的替代应用程序服务器有:
我稍后将介绍它们并描述它们彼此之间以及与 Mongrel 的不同之处。
WEBrick 与 Mongrel 做同样的事情,但不同之处在于:
应用程序服务器和世界
所有当前的 Ruby 应用程序服务器都使用 HTTP,但是某些应用程序服务器可能会在端口 80 上直接暴露到 Internet,而其他应用程序服务器可能不会。
为什么某些应用程序服务器必须置于反向代理后面?
为什么有些应用服务器可以直接暴露在互联网上?
应用程序服务器比较
在本节中,我将比较我提到的大多数应用程序服务器,但不包括 Phusion Passenger。 Phusion Passenger 与其他产品截然不同,因此我为其专门设置了一个部分。我还省略了 Trinidad 和 TorqueBox,因为我对它们不太了解,但无论如何,它们只有在您使用 JRuby 时才相关。
Phusion Passenger
Phusion Passenger 的工作原理与其他所有产品非常不同。 Phusion Passenger 直接集成到 Apache 或 Nginx 中,因此可以与 Apache 的 mod_php 进行比较。就像 mod_php 允许 Apache 几乎神奇地为 PHP 应用程序提供服务一样,Phusion Passenger 允许 Apache(还有 Nginx!)几乎神奇地为 Ruby 应用程序提供服务。 Phusion Passenger 的目标是让一切顺利(tm),尽可能减少麻烦。
您无需为您的应用程序启动进程或集群,也无需使用 Phusion Passenger 配置 Apache/Nginx 来为进程/集群提供静态文件和/或反向代理请求,您只需:
所有配置都在 Web 服务器配置文件中完成。 Phusion Passenger 几乎可以自动化所有事情。无需启动集群和管理进程。启动/停止进程、崩溃时重新启动进程等 - 全部自动化。与其他应用程序服务器相比,Phusion Passenger 的移动部件要少得多。这种易用性是人们使用 Phusion Passenger 的主要原因之一。
与其他应用服务器不同的是,Phusion Passenger 主要用 C++ 编写,因此速度非常快。
还有 Phusion Passenger 的企业版,具有更多功能,例如自动滚动重启、多线程支持、部署 Phusion
由于上述原因,Phusion Passenger 是目前最受欢迎的 Ruby 应用服务器,为超过 150,000 个网站提供支持,其中包括纽约时报、皮克斯、Airbnb 等大型网站。
其他应用服务器比较
Passenger 与Phusion Passenger 提供的 与其他应用程序服务器相比,具有更多功能并提供许多优势,例如:
Unicorn 不擅长的工作负载有:
Phusion Passenger Enterprise 4 或更高版本中的混合 I/O 模型使其成为此类工作负载的绝佳选择。
1)它不限于GC,可以用于任意工作。
2) Phusion Passenger 的版本可以很好地与多线程应用程序配合使用,而 Unicorn 的则不能。
还有更多的功能和优点,但列表确实很长。您应该参考全面的 Phusion Passenger 手册(Apache 版本、Nginx 版本) 或 Phusion Passenger 网站 了解信息。
I/O 并发模型
最近在 Phusion 博客上发布了一篇文章,内容涉及根据工作负载优化调整进程和线程的数量。请参阅调整 Phusion Passenger 的并发设置。
卡皮斯特拉诺
卡皮斯特拉诺是完全不同的东西。在前面的所有章节中,“部署”是指在应用程序服务器中启动 Ruby 应用程序的行为,以便访问者可以访问它,但在此之前,通常需要做一些准备工作,例如
在 Capistrano 的上下文中,“部署”是指完成所有这些准备工作。 Capistrano 不是应用程序服务器。相反,它是一个用于自动化所有准备工作的工具。您告诉 Capistrano 您的服务器在哪里,以及每次部署新版本应用程序时需要运行哪些命令,Capistrano 会负责将 Rails 应用程序上传到服务器并运行您指定的命令。
Capistrano 始终与应用程序服务器结合使用。它不会取代应用程序服务器。反之亦然,应用程序服务器不会取代 Capistrano,它们可以与 Capistrano 结合使用。
当然,您不必使用 Capistrano。如果您喜欢使用 FTP 上传 Ruby 应用程序并每次手动运行相同的命令步骤,那么您可以这样做。其他人厌倦了它,所以他们在 Capistrano 中自动化这些步骤。
The word "deployment" can have two meanings depending on the context. You are also confusing the roles of Apache/Nginx with the roles of other components.
Historic note: This article was originally written on November 6, 2010, when the Ruby app server ecosystem was limited. I've updated this article on March 15 2013 with all the latest updates in the ecosystem.
Disclaimer: I am one of the authors of Phusion Passenger, one of the app servers.
Apache vs Nginx
They're both web servers. They can serve static files but - with the right modules - can also serve dynamic web apps e.g. those written in PHP. Apache is more popular and has more features, Nginx is smaller and faster and has less features.
Neither Apache nor Nginx can serve Ruby web apps out-of-the-box, to do that you need to use Apache/Nginx in combination with some kind of add-on, described later.
Apache and Nginx can also act as reverse proxies, meaning that they can take an incoming HTTP request and forward it to another server, which also speaks HTTP. When that server responds with an HTTP response, Apache/Nginx will forward the response back to the client; You will learn later why this is relevant.
Mongrel and other production app servers vs WEBrick
Mongrel is a Ruby "application server": In concrete terms this means that Mongrel is an application which:
Mongrel listens for HTTP requests on this socket and passes the request data to the Ruby web app.
However Mongrel is quite dated, nowadays it is no longer maintained. Newer alternative application servers are:
I'll cover them later and describe how they differ from each other and from Mongrel.
WEBrick does the same thing as Mongrel, but the differences are:
The app server and the world
All current Ruby app servers speak HTTP, however some app servers may be directly exposed to the Internet on port 80, while others may not.
Why must some app servers be put behind a reverse proxy?
Why can some app servers be directly exposed to the Internet?
Application servers compared
In this section I'll compare most application servers I've mentioned, but not Phusion Passenger. Phusion Passenger is such a different beast from the rest that I've given it a dedicated section. I've also omitted Trinidad and TorqueBox because I do not know them well enough, but they're only relevant anyway if you use JRuby.
Phusion Passenger
Phusion Passenger works very differently from all the other ones. Phusion Passenger integrates directly into Apache or Nginx, and so can be compared to mod_php for Apache. Just like mod_php allows Apache to serve PHP apps, almost magically, Phusion Passenger allows Apache (and also Nginx!) to serve Ruby apps, almost magically. Phusion Passenger's goal is to make everything Just Work(tm) with as little hassle as possible.
Instead of starting a process or cluster for your app, and configuring Apache/Nginx to serve static files and/or reverse proxying requests to the process/cluster with Phusion Passenger you only need to:
All configuration is done within the web server config file. Phusion Passenger automates pretty much everything. There is no need to start a cluster and manage processes. Starting/stopping processes, restarting them when they crash, etc. - all automated. Compared to other app servers, Phusion Passenger has far fewer moving parts. This ease of use is one of the primary reasons why people use Phusion Passenger.
Also unlike other app servers, Phusion Passenger is primarily written in C++, making it very fast.
There's also an Enterprise variant of Phusion Passenger with even more features, such as automated rolling restarts, multithreading support, deployment error resistance, etc.
For the above reasons, Phusion Passenger is currently the most popular Ruby app server, powering over 150,000 websites, including large ones such as New York Times, Pixar, Airbnb, etc.
Phusion Passenger vs other app servers
Phusion Passenger provides a lot more features and provides many advantages over other app servers, such as:
Workloads that Unicorn is not good at are:
The hybrid I/O model in Phusion Passenger Enterprise 4 or later makes it an excellent choice for these kinds of workloads.
1) it's not limited to GC and can be used for arbitrary work.
2) Phusion Passenger's version works well with multithreaded apps, while Unicorn's does not.
There are more features and advantages, but the list is really long. You should refer to the comprehensive Phusion Passenger manual (Apache version, Nginx version) or the Phusion Passenger website for information.
I/O concurrency models
An article was recently posted on the Phusion blog about optimally tuning the number of processes and threads given your workload. See Tuning Phusion Passenger's concurrency settings.
Capistrano
Capistrano is something completely different. In all the previous sections, "deployment" refers to the act of starting your Ruby app in an application server, so that it becomes accessible to visitors, but before that can happen one typically needs to do some preparation work, such as:
In the context of Capistrano, "deployment" refers to doing all this preparation work. Capistrano is not an application server. Instead, it is a tool for automating all that preparation work. You tell Capistrano where your server is and which commands need to be run every time you deploy a new version of your app, and Capistrano will take care of uploading the Rails app to the server for you and running the commands you specified.
Capistrano is always used in combination with an application server. It does not replace application servers. Vice-versa, application servers do not replace Capistrano, they can be used in combination with Capistrano.
Of course you don't have to use Capistrano. If you prefer to upload your Ruby app with FTP and manually running the same steps of commands every time, then you can do that. Other people got tired of it, so they automate those steps in Capistrano.