IT技術(shù)小屋

          秋風(fēng)秋雨,皆入我心

            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            38 隨筆 :: 1 文章 :: 19 評(píng)論 :: 0 Trackbacks
              前言:為什么要研究Ruby on Rails的部署
              學(xué)習(xí)Ruby on Rails已經(jīng)一段時(shí)間了,一直使用自帶的WEBrick服務(wù)器進(jìn)行開發(fā)。
              WEBrick是一款純Ruby編寫的服務(wù)器,使用方便,很適合開發(fā)環(huán)境下進(jìn)行系統(tǒng)調(diào)試。但是它不支持多線程訪問,換句話說,所有的Ruby請(qǐng)求都是按照到達(dá)的時(shí)間先后,順序處理的,因此效率不高,無(wú)法應(yīng)用在實(shí)際的生產(chǎn)環(huán)境中。所以今天研究了一下如何將Rails3應(yīng)用部署到真實(shí)的線上環(huán)境中。
              
              搜集了一下當(dāng)前比較流行的Rails部署方案,這些資料里面介紹了許多可選的方案:
              1、Agile Web Development with Rails (4th edition),Chapter 16;
              2、范凱的博客:在Linux平臺(tái)上安裝和配置Ruby on Rails詳解;
              3、一個(gè)日本開發(fā)者的博客:Rails 3.0.0rc + lighttpd + FastCGI で無(wú)理矢理動(dòng)かしてみた。
              其中,范凱在RoR部署方案深度剖析一文中詳細(xì)列舉和分析了各種方案的優(yōu)缺點(diǎn)以及性能上的差異,十分有借鑒意義。他認(rèn)為各個(gè)方案在性能方面的排序?yàn)椋?/div>
              Lighttpd+FastCGI  Lighttpd+Mongrel  Nginx+Mongrel  Apache+Mongrel  Ngignx+FastCGI  Apache+FastCGI
              因此,本文也采取了和他相同的部署方案。但是,由于他的博客較老,Ruby on Rails兩年來從2.x升級(jí)到了3.x,以前的部署步驟也發(fā)生了變化,按照他的方法已經(jīng)無(wú)法成功部署Rails3應(yīng)用了。因此,本文根據(jù)相關(guān)資料,進(jìn)行了些嘗試。
              
              我使用的環(huán)境如下:
              1、Linux ubuntu 2.6.32-32-generic #62-Ubuntu SMP Wed Apr 20 21:54:21 UTC 2011 i686 GNU/Linux
              2、gcc version 4.3.4 (Ubuntu 4.3.4-10ubuntu1)
              在開始部署之前,請(qǐng)確保你有一個(gè)Unix系統(tǒng)和較高版本的gcc編譯器。
              
              一、安裝Ruby解釋器
              現(xiàn)在Ruby提供了針對(duì)Ubuntu平臺(tái)的deb安裝包,并且你也可以使用apt-get安裝Ruby解釋器,但是我仍然建議自行下載Ruby源代碼編譯安裝。因?yàn)榭梢宰约憾ㄖ芌uby安裝的路徑,并且可以在編譯過程中自行添加更多的特性,還可以自己控制安裝的版本。
              Ruby的源代碼可以從Ruby官方網(wǎng)站下載:Ruby解釋器下載
              下載源代碼包到本地Linux主機(jī),然后解壓縮,進(jìn)入該目錄,進(jìn)行配置,編譯和安裝:
              tar xvfz ruby-1.9.2-p180.tar.gz
              cd ruby-1.9.2-p180
              ./configure -prefix=/opt/ruby-1.9.2-p180
              make && make install
             
              如果想瀏覽所有的configure參數(shù),可以:
              ./configure -h
              如果不定制安裝的目錄,默認(rèn)將安裝到/usr/local目錄下面。然而我建議自行定制一個(gè)ruby的安裝目錄,例如/opt/ruby-1.9.2-p180,這樣便于以后的升級(jí),不會(huì)和操作系統(tǒng)其他軟件混在一起。而且今后可能會(huì)安裝其他版本的Ruby解釋器,通過分開目錄安裝還可以在不同的Ruby解釋器之間切換。
           
              安裝好以后,我比較喜歡在/usr/local/bin下面建立軟連接的方式來把Ruby加入系統(tǒng)搜索路徑:
              ln -s /opt/ruby-1.9.2-p180/bin/ruby /usr/local/bin/ruby
              ln -s /opt/ruby-1.9.2-p180/bin/gem /usr/local/bin/gem
              二、安裝Ruby on Rails
              確認(rèn)服務(wù)器已經(jīng)連接互聯(lián)網(wǎng)的情況下執(zhí)行:
              gem install rails
              即通過gem從rubyforge網(wǎng)站下載rails所有依賴包安裝。
             
              安裝好rails以后,可以執(zhí)行:
              ln -s /opt/ruby-1.9.2-p180/bin/rails /usr/local/bin/rails
              rails –v
              確認(rèn)一下rails的版本。
              三、安裝Ruby的數(shù)據(jù)庫(kù)適配器 
              Rails發(fā)行包中已經(jīng)自帶純Ruby的MySQL數(shù)據(jù)庫(kù)適配器,然而對(duì)于生產(chǎn)環(huán)境來說,我們?nèi)匀粦?yīng)該下載安裝C版本的數(shù)據(jù)庫(kù)適配器,以達(dá)到更好的性能。Rails升級(jí)到3.0之后,推薦使用mysql2作為MySQL驅(qū)動(dòng),從這里可以下載mysql2-0.2.11.gem
              gem install mysql2-0.2.11.gem -l -- --with-mysql-dir=/opt/mysql5
              注意--with-mysql-dir應(yīng)該指向MySQL數(shù)據(jù)庫(kù)的安裝路徑,如果數(shù)據(jù)庫(kù)服務(wù)器和Web服務(wù)器不在同一臺(tái)機(jī)器上,那么Web服務(wù)器上也必須安裝MySQL軟件,因?yàn)閞uby的C版本MySQL適配器需要在編譯的時(shí)候聯(lián)接MySQL的系統(tǒng)庫(kù)。
              四、安裝fcgi支持庫(kù)
              由于Ruby的fcgi支持庫(kù)需要在編譯的時(shí)候聯(lián)接fcgi的系統(tǒng)庫(kù),因此我們需要先安裝fcgi庫(kù),下載fcgi源代碼發(fā)行包fcgi-2.4.0.tar.gz
              tar xzvf fcgi-2.4.0.tar.gz
              cd fcgi-2.4.0
              ./configure --prefix=/opt/fcgi-2.4.0
              make && make install
              同樣,將fcgi安裝在自己指定的目錄下,而不是默認(rèn)的/usr/local,避免多個(gè)軟件混在一起。
              五、安裝Ruby的FCGI支持
              接下來就可以安裝ruby的fcgi支持庫(kù)了,下載fcgi-0.8.8.tgz
              tar xzvf fcgi-0.8.8.tgz
              cd fcgi-0.8.8
              ruby install.rb config -- --with-fcgi-include=/opt/fcgi-2.4.0/include --with-fcgi-lib=/opt/fcgi-2.4.0/lib
              ruby install.rb setup
              ruby install.rb install
              注意:安裝完畢之后,請(qǐng)確保/opt/ruby-1.9.2/lib/ruby/site_ruby/1.9.1/i686-linux/目錄下有這三個(gè)共享庫(kù)文件:fcgi.so  mysql.so  openssl.so,否則運(yùn)行中會(huì)出現(xiàn)問題。如果缺少fcgi.so請(qǐng)重復(fù)步驟五,如果缺少mysql.so請(qǐng)重復(fù)步驟三,如果缺少openssl.so,請(qǐng)安裝openssl庫(kù)之后重復(fù)步驟一。
              六、安裝Lighttpd Web Server
              在安裝lighttpd之前,應(yīng)該確認(rèn)操作系統(tǒng)已經(jīng)安裝pcre,即Perl兼容的規(guī)則表達(dá)式庫(kù)。
              如果沒有,請(qǐng)執(zhí)行:
              sudo apt-get install libpcre3 libpcre3-dev
           
              然后下載Lighttpd:
              http://www.lighttpd.net/download/
              tar xzvf lighttpd-1.4.28.tar.gz
              cd lighttpd-1.4.28
              ./configure --prefix=/opt/lighttpd-1.4.28
           
              configure完畢以后,會(huì)給出一個(gè)激活的模塊和沒有激活模塊的清單,可以檢查一下,是否自己需要的模塊都已經(jīng)激活,在enable的模塊中一定要有“mod_rewrite”這一項(xiàng),否則重新檢查pcre是否安裝。然后編譯安裝:
              make && make install
              七、配置Lighttpd
              將Lighttpd的示例配置文件拷貝到配置文件目錄中放置:
              mkdir /etc/lighttpd
              cp ./lighttpd-1.4.28/doc/config/lighttpd.conf /etc/lighttpd/
              cp ./lighttpd-1.4.28/doc/config/modules.conf /etc/lighttpd/
              同時(shí)可以根據(jù)你的實(shí)際情況進(jìn)行修改,主要修改如下:
              1)對(duì)/etc/lighttpd/modules.conf進(jìn)行如下修改:
                 server.modules = (
                     "mod_access",
                     "mod_alias",
                     "mod_auth",
                     "mod_evasive",
                     "mod_redirect",
                     "mod_rewrite",
                     "mod_setenv",
                     "mod_usertrack",
                     "mod_fastcgi",
                     "mod_simple_vhost",
                     "mod_compress",
                     "mod_accesslog",
                 )
                 這個(gè)文件設(shè)置了lighttpd需要開啟的模塊。
              2)對(duì)/etc/lighttpd/modules.conf進(jìn)行如下修改:
                 server.document-root, server.error-log,accesslog.filename需要指定相應(yīng)的目錄,如果沒有創(chuàng)建對(duì)應(yīng)的目錄,lighttpd啟動(dòng)時(shí)會(huì)報(bào)錯(cuò)。
              3)用什么權(quán)限來運(yùn)行Lighttpd
                 server.username = "USER_NAME"
                 server.groupname = "USER_GROUP"
                 從安全角度來說,不建議用root權(quán)限運(yùn)行Web Server,可以自行指定普通用戶權(quán)限。
              4) 配置Lighttpd和Ruby on Rails之間的通信
                 這一步配置十分重要,最簡(jiǎn)單的配置如下:
                 $HTTP["host"] =~ "(^|\.)testmengli\.com" {
                     server.document-root = "/home/jowett/mini/public"
                     server.error-handler-404 = "/dispatch.fcgi"
                     fastcgi.server = (".fcgi" =>
                         ("localhost" =>
                             (
                               "min-procs" => 1,
                               "max-procs" => 2,
                               "socket" => "/home/jowett/mini/tmp/socket/rails.socket",
                               "bin-path" => "/home/jowett/mini/public/dispatch.fcgi",
                               "bin-environment" => ("RAILS_ENV" => "development")
                             )
                         )
                     )
                 }
                 即由lighttpd啟動(dòng)2個(gè)FCGI進(jìn)程,lighttpd和fcgi之間使用本機(jī)Unix Socket通信。
                 socket:指定了通信的socket文件的存放位置;
                 bin-path:指定了request轉(zhuǎn)發(fā)的程序位置;
                 server.error-handler-404:指定了請(qǐng)求的資源找不到時(shí),進(jìn)行處理的程序。相當(dāng)于把動(dòng)態(tài)請(qǐng)求過濾給Ruby on Rails服務(wù)器進(jìn)行處理。
                 5) 配置mime
                    必須設(shè)置mime配置文件,不然lighttpd無(wú)法識(shí)別css、image等資源類型。
                    
                    在/etc/lighttpd/lighttpd.conf中添加一行:
                    include "mime.conf"
                    然后創(chuàng)建mime配置文件:
                    touch /etc/lighttpd/mime.conf
                    
                    并且加入如下內(nèi)容:
                    ##  MimeType handling
                    ## -------------------
                    ##
                    ## Use the "Content-Type" extended attribute to obtain mime type if
                    ## possible
                    ##
                    mimetype.use-xattr        = "disable" 
                    ##
                    ## mimetype mapping
                    ##
                    mimetype.assign             = (
                      ".pdf"          =>      "application/pdf",
                      ".sig"          =>      "application/pgp-signature",
                      ".spl"          =>      "application/futuresplash",
                      ".class"        =>      "application/octet-stream",
                      ".ps"           =>      "application/postscript",
                      ".torrent"      =>      "application/x-bittorrent",
                      ".dvi"          =>      "application/x-dvi",
                      ".gz"           =>      "application/x-gzip",
                      ".pac"          =>      "application/x-ns-proxy-autoconfig",
                      ".swf"          =>      "application/x-shockwave-flash",
                      ".tar.gz"       =>      "application/x-tgz",
                      ".tgz"          =>      "application/x-tgz",
                      ".tar"          =>      "application/x-tar",
                      ".zip"          =>      "application/zip",
                      ".mp3"          =>      "audio/mpeg",
                      ".m3u"          =>      "audio/x-mpegurl",
                      ".wma"          =>      "audio/x-ms-wma",
                      ".wax"          =>      "audio/x-ms-wax",
                      ".ogg"          =>      "application/ogg",
                      ".wav"          =>      "audio/x-wav",
                      ".gif"          =>      "image/gif",
                      ".jpg"          =>      "image/jpeg",
                      ".jpeg"         =>      "image/jpeg",
                      ".png"          =>      "image/png",
                      ".xbm"          =>      "image/x-xbitmap",
                      ".xpm"          =>      "image/x-xpixmap",
                      ".xwd"          =>      "image/x-xwindowdump",
                      ".css"          =>      "text/css",
                      ".html"         =>      "text/html",
                      ".htm"          =>      "text/html",
                      ".js"           =>      "text/javascript",
                      ".asc"          =>      "text/plain",
                      ".c"            =>      "text/plain",
                      ".cpp"          =>      "text/plain",
                      ".log"          =>      "text/plain",
                      ".conf"         =>      "text/plain",
                      ".text"         =>      "text/plain",
                      ".txt"          =>      "text/plain",
                      ".spec"         =>      "text/plain",
                      ".dtd"          =>      "text/xml",
                      ".xml"          =>      "text/xml",
                      ".mpeg"         =>      "video/mpeg",
                      ".mpg"          =>      "video/mpeg",
                      ".mov"          =>      "video/quicktime",
                      ".qt"           =>      "video/quicktime",
                      ".avi"          =>      "video/x-msvideo",
                      ".asf"          =>      "video/x-ms-asf",
                      ".asx"          =>      "video/x-ms-asf",
                      ".wmv"          =>      "video/x-ms-wmv",
                      ".bz2"          =>      "application/x-bzip",
                      ".tbz"          =>      "application/x-bzip-compressed-tar",
                      ".tar.bz2"      =>      "application/x-bzip-compressed-tar",
                      ".odt"          =>      "application/vnd.oasis.opendocument.text", 
                      ".ods"          =>      "application/vnd.oasis.opendocument.spreadsheet", 
                      ".odp"          =>      "application/vnd.oasis.opendocument.presentation", 
                      ".odg"          =>      "application/vnd.oasis.opendocument.graphics", 
                      ".odc"          =>      "application/vnd.oasis.opendocument.chart", 
                      ".odf"          =>      "application/vnd.oasis.opendocument.formula", 
                      ".odi"          =>      "application/vnd.oasis.opendocument.image", 
                      ".odm"          =>      "application/vnd.oasis.opendocument.text-master", 
                      ".ott"          =>      "application/vnd.oasis.opendocument.text-template",
                      ".ots"          =>      "application/vnd.oasis.opendocument.spreadsheet-template",
                      ".otp"          =>      "application/vnd.oasis.opendocument.presentation-template",
                      ".otg"          =>      "application/vnd.oasis.opendocument.graphics-template",
                      ".otc"          =>      "application/vnd.oasis.opendocument.chart-template",
                      ".otf"          =>      "application/vnd.oasis.opendocument.formula-template",
                      ".oti"          =>      "application/vnd.oasis.opendocument.image-template",
                      ".oth"          =>      "application/vnd.oasis.opendocument.text-web",
                    # make the default mime type application/octet-stream.
                      ""              =>      "application/octet-stream",
                    )
              八、啟動(dòng)Lighttpd
              在/etc/hosts文件中加入下面一行:
              127.0.0.1       testmengli.com
              Lighttpd的啟動(dòng)命令是:/usr/local/lighttpd/sbin/lighttpd -f /etc/lighttpd/lighttpd.conf
              通過以上步驟,就基本實(shí)現(xiàn)了Rails應(yīng)用在生產(chǎn)環(huán)境下的部署,用到的技術(shù)包括Lighttpd+fcgi+mysql,希望對(duì)大家有幫助!
          posted on 2011-08-02 10:25 Meng Lee 閱讀(1698) 評(píng)論(0)  編輯  收藏 所屬分類: Rails

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 玉树县| 清徐县| 池州市| 沙湾县| 乌鲁木齐市| 澄江县| 武宁县| 墨玉县| 桓台县| 乌兰浩特市| 若尔盖县| 塔城市| 望奎县| 肥西县| 汝阳县| 德阳市| 阿坝县| 黔南| 刚察县| 密山市| 天气| 和平县| 新龙县| 蚌埠市| 达日县| 临夏县| 黔江区| 泰宁县| 教育| 合山市| 盐城市| 山阳县| 鹰潭市| 金川县| 陵水| 怀远县| 天等县| 华坪县| 杭锦旗| 分宜县| 马鞍山市|