Sunday, May 17, 2009

Ruby on rails setting.

Ruby on Rails 架設一直有一個問題
找不到適當的 Application Server
Webricks 太慢,fastcgi 太玄妙,scgi 太年輕
這個時候
只有 Mongrel 可以拯救世界呀~~~~

Mongrel 首頁是這樣寫的

Mongrel is a fast HTTP library and server for Ruby that is intended for hosting Ruby web applications of any kind using plain HTTP rather than FastCGI or SCGI. It is framework agnostic and already supports Ruby On Rails, Og+Nitro, and Camping frameworks.
他跟Webricks 一樣,是一個方便我們架設 Ruby on Rails 環境的網頁伺服器
並且他比 Webricks 還要來得快速

安裝方式很簡單
gem i mongrel
使用方式呢,基本上跟 Webrick 差不多
到 Rails 根目錄
打入
mongrel_rails start
你就會發現他已經可以使用了

如果要用 deamon 模式來使用的話
打入
mongrel_rails start -d
即可

如果要使用特定的 port
mongrel_rails start -p 1234
即可

用不同環境啟動
mongrel_rails start -e production/development/test
即可

指定log file
mongrel_rails start -l log/mongrel_log
即可

有啟動的問題
mongrel_rails start -h
即可

重起 Mongrel
mongrel_rails restart

停止 Mongrel
mongrel_rails stop





Mongrel 如果只有單獨啟動在 80 port
他充其量不過是一個速度較普普的 Web Server
但是,當作 Backend Application Server 才是他的宿命呀

今天的需求是這樣
我們可以開啟數個 Mongrel Process 在其他 port
前端開一個 Proxy Server
當 Proxy Server 接受到 request
他會傳給 backend application server
其實作法很像 Mod_fastcgi 的作法
只是中間的 application server 將 fastcgi 改成了 mongrel

如果要開啟眾多的 Mongrel Process
我們當然可以用
mongrel_rails start -d -p 8000 -P log/mongrel_1.pid
mongrel_rails start -d -p 8001 -P log/mongrel_2.pid
....
這樣來執行,但是要管理就變得相當的麻煩
不但要一一起動,要關掉或是重起的變得相當麻煩
如果 Mongrel 那麼麻煩的話
那我們還搞屁,直接用 fastcgi 就好啦 XD
這時候請用 mongrel cluster
有了他,Mongrel 才變成簡單好用的 Application Server

安裝方式
gem i mongrel_cluster
即可

設定方式
一開始,請到 Ruby on Rails 的根目錄
先設定 Mongrel 的設定檔
mongrel_rails cluster::configure -e 那些環境 \
-p 8000 \
-N 3 \
-c /var/www/servers/ \
-a 127.0.0.1 \
--user mongrel \
--group mongrel
他會將 config 寫到 config/database.yml
我解釋一下選項
  • -e 就是用那個環境啟動(production/development/test)
  • -p 就是用那個 port 開始
  • -N 就是開啟幾個 process
  • -c 就是先切到那個目錄,再執行 mongrel cluster (通常是這個Ruby on Rails 的根目錄)
  • -a 就是在那個 host 啟動
  • --user 就是以那個 user 身份啟動
  • --group 就是以那個 group 啟動

如果設定好後,以後使用 mongrel_rails cluster::start 他就會立刻使用剛剛的設定檔
這裡要注意的是 -N 跟 -p 的選項
假設 -p = 8000 -N 為 3
那他會開啟三個mongrel process ,分別以 8000 , 8001 , 8002 來聽
其實 -p 8000 -N 3
就跟
mongrel_rails start -d -p 8000 -P log/mongrel_1.pid
mongrel_rails start -d -p 8001 -P log/mongrel_2.pid
mongrel_rails start -d -p 8002 -P log/mongrel_3.pid
一樣的意義

以此類推假設 -p = 7000 -N 為 5
那他會開啟五個mongrel process ,分別以 7000 , 7001 , 7002 , 7003 , 7004 來聽

使用方式
啟動方式
mongrel_rails cluster::start

關閉方式
mongrel_rails cluster::stop

重起方式
mongrel_rails cluster::restart




Rails部署Mongrel+Apache
在windows平台下面部署rails,一个比较好的选择就是Mongrel+Apache。在windows平台下面部署rails,一個比較好的選擇就是Mongrel+Apache。
Mongrel对rails的支持非常好,基本不需要什么配置,效率也不错,但是也有些弊病。Mongrel對rails的支持非常好,基本不需要什麼配置,效率也不錯,但是也有些弊病。 例如一个Mongrel实例只能同时处理一个请求,不支持http compress(至少我没找到)。例如一個Mongrel實例只能同時處理一個請求,不支持http compress(至少我沒找到)。
Apache就不用说了,工业标准,功能强大,就是配置稍显繁琐,对rails的支持限于fastCGI,据说Apache2还很不兼容。Apache就不用說了,工業標準,功能強大,就是配置稍顯繁瑣,對rails的支持限於fastCGI,據說Apache2還很不兼容。
所以现在流行的一个搭配,就是集二者之所长,还可以使用Apache的http代理功能,启动多个mongrel进程实现可伸缩的负载均衡。所以現在流行的一個搭配,就是集二者之所長,還可以使用Apache的http代理功能,啟動多個mongrel進程實現可伸縮的負載均衡。

Mongrel的安装很简单:Mongrel的安裝很簡單:
gem install mongrel –ygem install mongrel –y
gem install mongrel_service -ygem install mongrel_service -y
然后然後
mongrel_rails service::install -N web1 -c c:\website -p 3000 –e productionmongrel_rails service::install -N web1 -c c:\website -p 3000 –e production
-N指明服务名称,-d指明rails应用的目录,-p是mongrel监听的tcp端口,-e是启动模式为生产模式
即可创建rails网站系统服务。
-N指明服務名稱,-d指明rails應用的目錄,-p是mongrel監聽的tcp端口,-e是啟動模式為生產模式
即可創建rails網站系統服務。

我们可以创建两个进程:我們可以創建兩個進程:
mongrel_rails service::install -N web1 -c c:\website -p 3000 –e productionmongrel_rails service::install -N web1 -c c:\website -p 3000 –e production
mongrel_rails service::install -N web2 -c c:\website -p 3001 –e productionmongrel_rails service::install -N web2 -c c:\website -p 3001 –e production


Apache安装就不用介绍了,安装完毕以后修改httpd.conf文件,启用以下模块:Apache安裝就不用介紹了,安裝完畢以後修改httpd.conf文件,啟用以下模塊:
LoadModule proxy_module modules/mod_proxy.soLoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_balancer_module modules/mod_proxy_balancer.soLoadModule proxy_balancer_module modules/mod_proxy_balancer.so
LoadModule proxy_http_module modules/mod_proxy_http.soLoadModule proxy_http_module modules/mod_proxy_http.so
如果你希望对页面输出使用压缩,还需要这个模块:如果你希望對頁面輸出使用壓縮,還需要這個模塊:
LoadModule deflate_module modules/mod_deflate.soLoadModule deflate_module modules/mod_deflate.so

最后加上:最後加上:
程序代码 程序代码程序代碼

#启用http压缩#啟用http壓縮

AddOutputFilterByType DEFLATE text/html text/css text/javascript application/x-javascript AddOutputFilterByType DEFLATE text/html text/css text/javascript application/x-javascript


#创建虚拟目录,让apache来处理javascript和css以及图片文件,分担mongrel的压力#創建虛擬目錄,讓apache來處理javascript和css以及圖片文件,分擔mongrel的壓力
Alias /javascripts C:/website/public/javascriptsAlias /javascripts C:/website/public/javascripts
Alias /stylesheets C:/website/public/stylesheetsAlias /stylesheets C:/website/public/stylesheets
Alias /images C:/website/public/imagesAlias /images C:/website/public/images
#设置目录访问权限#設置目錄訪問權限

order allow,deny order allow,deny
Allow from all Allow from all



order allow,deny order allow,deny
Allow from all Allow from all



order allow,deny order allow,deny
Allow from all Allow from all


#设置负载代理,即mongrel进程#設置負載代理,即mongrel進程
ProxyRequests Off ProxyRequests Off

BalancerMember http://localhost:3000BalancerMember http://localhost:3000
BalancerMember http://localhost:3001BalancerMember http://localhost:3001


#apache监听80端口,将www.yourwebdomain.com的请求转发给mongrel负载代理,脚本,css,图片除外#apache監聽80端口,將www.yourwebdomain.com的請求轉發給mongrel負載代理,腳本,css,圖片除外

ServerName www.yourwebdomain.comServerName www.yourwebdomain.com
DocumentRoot C:/website/publicDocumentRoot C:/website/public
ProxyPass /images ! ProxyPass /images !
ProxyPass /stylesheets ! ProxyPass /stylesheets !
ProxyPass /javascripts ! ProxyPass /javascripts !
ProxyPass / balancer://myCluster/ ProxyPass / balancer://myCluster/
ProxyPassReverse / balancer://myCluster/ ProxyPassReverse / balancer://myCluster/
ProxyPreserveHost on ProxyPreserveHost on




配置完毕,启动mongrel服务,apache服务就ok了。配置完畢,啟動mongrel服務,apache服務就ok了。