---
title: "(转)在Windows平台使用Apache2.2和Mongrel运行Ruby on Rails "
author: "xinlanzero"
date: "2011-01-10T22:50:36Z"
tags: []
categories: ["编程语言"]
slug: "windows-apache2-2-mongrel-ruby-on-rails"
draft: false
lastmod: "2011-06-29T11:32:03Z"
id: 867665
url: "https://xinlanzero.iteye.com/blog/867665"
---

原创作者: [robbin](http://www.iteye.com/topic/43290)   阅读:1067次   评论:0条   更新时间:2008-12-16     [收藏](http://app.iteye.com/links?user_favorite%5Btitle%5D=%E5%9C%A8Windows%E5%B9%B3%E5%8F%B0%E4%BD%BF%E7%94%A8Apache2.2%E5%92%8CMongrel%E8%BF%90%E8%A1%8CRuby%20on%20Rails&user_favorite%5Burl%5D=http%3A%2F%2Fwww.iteye.com%2Fwiki%2Frails_deployment%2F1297-deploy-rails-with-mongrel-and-apache-on-windows-platform)![Spinner](http://www.iteye.com/images/spinner.gif?1294639779)

一、安装Ruby、rails、mongrel和Apache2.2

 

从rubyforge网站下载One-Click Ruby Install，运行安装程序，就安装好了ruby和rubygems。

 

运行命令：

gem install rails –y

gem install mongrel –y

gem install mongrel\_service -y

安装好了rails和mongrel

 

从Apache网站下载Windows版本的Apache2.2，运行安装程序，就安装好了Apache2.2。

 

二、把Mongrel作为Services启动

 

mongrel\_rails service::install -N depot -c d:\\Rubyproject\\depot -p 3000 -e production

-N指明服务名称，-d指明rails应用的目录，-p是mongrel监听的tcp端口，-e是启动模式为生产模式

 

这样打开控制面版|管理工具|服务，就可以发现增加了一项名为“depot”的服务，就可以通过控制面版来管理服务了。如果需要命令行启动和关闭该服务，那么：

mongrel\_rails service::start -N depot

mongrel\_rails service::stop -N depot

 

如果需要从服务中注销该项服务，那么：

mongrel\_rails service::remove -N depot

 

如果需要安装多个mongrel实例，那么可以这样：

mongrel\_rails service::install -N depot0 -c d:\\Rubyproject\\depot -p 3000 -e production

mongrel\_rails service::install -N depot1 -c d:\\Rubyproject\\depot -p 3001 -e production

诸如此类。

 

三、配置Apache2.2

 

用编辑工具打开Apache2.2目录下面的conf/httpd.conf，需要取消如下模块的注释：

LoadModule proxy\_module modules/mod\_proxy.so

LoadModule proxy\_balancer\_module modules/mod\_proxy\_balancer.so

LoadModule proxy\_http\_module modules/mod\_proxy\_http.so

如果你希望对页面输出使用压缩，也需要取消如下模块的注释：

LoadModule deflate\_module modules/mod\_deflate.so

 

然后按如下内容配置基于HTTP代理的负载均衡：

xml 代码

 

01. ProxyRequests Off
02. &lt;Proxy balancer://myCluster&gt;
03. BalancerMember http://localhost:3000
04. BalancerMember http://localhost:3001
05. &lt;/Proxy&gt;
06. &lt;VirtualHost \*:80&gt;
07. ServerName www.xxx.com
08. DocumentRoot d:/rubyproject/depot/public
09. ProxyPass /images !
10. ProxyPass /stylesheets !
11. ProxyPass /javascripts !
12. ProxyPass / balancer://myCluster/
13. ProxyPassReverse / balancer://myCluster/
14. ProxyPreserveHost on
15. &lt;/VirtualHost&gt;

 

myCluster定义了群集中的每个mongrel应用服务器节点。ProxyPass /images !指明该URL开始的请求不代理给Mongrel群集，而由Apache自己处理。重起Apache，然后打开浏览器访问[www.xxx.com](http://www.xxx.com/)，检查配置是否正确。

 

至此，在Windows Server上面一个具备良好稳定性和性能的Ruby on rails生产环境就搭建好了。

 

对于页面输出，还可以使用mod\_deflate进行输出内容压缩，以提高页面下载速度，这个就留给大家自己配置了。