精華區beta Ruby 關於我們 聯絡資訊
出自我的 Blog http://lightyror.blogspot.com/2006/10/mongrel.html 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 -- lighty RoR 是一個介紹 lighttpd , SQLite , Ruby and Rails 的 Blog http://lightyror.blogspot.com/ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.218.90.242 > -------------------------------------------------------------------------- < 作者: giive (lala) 看板: Ruby 標題: [心得] Mongrel 三部曲之二:Mongrel Cluster 時間: Thu Oct 5 17:29:47 2006 出自我的Blog http://lightyror.blogspot.com/2006/10/mongrel-part-2-mongrel-cluster.html 看完上一篇,大家一定很想丟雞蛋 這樣的 tutorial 也可以出來混一篇文章 在大家浪費買蛋錢之前,我必須要說,好戲在後頭呀!!! 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 -- lighty RoR 是一個介紹 lighttpd , SQLite , Ruby and Rails 的 Blog http://lightyror.blogspot.com/ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.218.90.242 > -------------------------------------------------------------------------- < 作者: giive (lala) 看板: Ruby 標題: [心得] Mongrel 三部曲之三:lighttpd + Mongrel Cluster 時間: Thu Oct 5 17:30:51 2006 出自我的 Blog http://lightyror.blogspot.com/2006/10/lighttpd-modproxycore-mongrel-cluster.html 經過上一篇的 Mongrel Cluster 我們可以發現到我們已經可以配置 Mongrel Cluster 再來的問題就是如何使用他 有鑑於之前寫過的 Lighttpd mod_proxy_core 的歷史 我們使用 Lighttpd mod_proxy_core 來使用 Mongrel Cluster 首先,先配置 Mongrel Cluster 在本機端 mongrel_rails cluster::configure \ -e production \ -p 8000 \ -N 3 \ -c /var/www/servers/ \ -a 127.0.0.1 \ --user mongrel \ --group mongrel 我們啟動了三個 Mongrel Cluster 並且 Listen 8000 , 8001 , 8002 port 然後下載這份 Lighttpd trunk (預計會在 1.5 放入 Lighttpd ) http://www.lighttpd.net/download/lighttpd-1.4.12-20060724-0947.tar.gz ./configure && make && make install 再來,將 lighttpd.conf 加入 server.modules = ( ..... , "mod_proxy_core" , ....) 並將你的 Rails 的 Domain name 寫入下列的設定 $HTTP["host"] == "domain.example.com" { proxy-core.balancer = "round-robin" proxy-core.protocol = "http" proxy-core.backends = ( "127.0.0.1:8000", "127.0.0.1:8001" , "127.0.0.1:8002") } proxy-core.balancer 代表的意思是如何去分配的 ,一共有 hash, fair, rr 三個 balancer 剩下的都很好看懂吧 最後重起 lighttpd 即可 ps. 這裡的 mod_proxy_core 主要是作 reverse_proxy 的工作 -- lighty RoR 是一個介紹 lighttpd , SQLite , Ruby and Rails 的 Blog http://lightyror.blogspot.com/ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.218.90.242 ※ 編輯: giive 來自: 61.218.90.242 (10/05 17:54)