Linux下Rails3 + Lighttpd + fcgi部署研究
??? 前言:為什么要研究Ruby on Rails的部署
??? 學習Ruby on Rails已經一段時間了,一直使用自帶的WEBrick服務器進行開發。
??? WEBrick是一款純Ruby編寫的服務器,使用方便,很適合開發環境下進行系統調試。但是它不支持多線程訪問,換句話說,所有的Ruby請求都是按照到達的時間先后,順序處理的,因此效率不高,無法應用在實際的生產環境中。所以今天研究了一下如何將Rails3應用部署到真實的線上環境中。
???
??? 搜集了一下當前比較流行的Rails部署方案,這些資料里面介紹了許多可選的方案:
??? 1、Agile Web Development with Rails (4th edition),Chapter 16;
??? 2、范凱的博客:在Linux平臺上安裝和配置Ruby on Rails詳解;
??? 3、一個日本開發者的博客:Rails 3.0.0rc + lighttpd + FastCGI で無理矢理動かしてみた。
??? 其中,范凱在RoR部署方案深度剖析一文中詳細列舉和分析了各種方案的優缺點以及性能上的差異,十分有借鑒意義。他認為各個方案在性能方面的排序為:
??? Lighttpd+FastCGI? >? Lighttpd+Mongrel? >? Nginx+Mongrel? >? Apache+Mongrel? >? Ngignx+FastCGI? >? Apache+FastCGI
??? 因此,本文也采取了和他相同的部署方案。但是,由于他的博客較老,Ruby on Rails兩年來從2.x升級到了3.x,以前的部署步驟也發生了變化,按照他的方法已經無法成功部署Rails3應用了。因此,本文根據相關資料,進行了些嘗試。
???
??? 我使用的環境如下:
??? 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)
??? 在開始部署之前,請確保你有一個Unix系統和較高版本的gcc編譯器。
???
??? 一、安裝Ruby解釋器
??? 現在Ruby提供了針對Ubuntu平臺的deb安裝包,并且你也可以使用apt-get安裝Ruby解釋器,但是我仍然建議自行下載Ruby源代碼編譯安裝。因為可以自己定制Ruby安裝的路徑,并且可以在編譯過程中自行添加更多的特性,還可以自己控制安裝的版本。
??? Ruby的源代碼可以從Ruby官方網站下載:Ruby解釋器下載
??? 下載源代碼包到本地Linux主機,然后解壓縮,進入該目錄,進行配置,編譯和安裝:
??? 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參數,可以:
??? ./configure -h
??? 如果不定制安裝的目錄,默認將安裝到/usr/local目錄下面。然而我建議自行定制一個ruby的安裝目錄,例如/opt/ruby-1.9.2-p180,這樣便于以后的升級,不會和操作系統其他軟件混在一起。而且今后可能會安裝其他版本的Ruby解釋器,通過分開目錄安裝還可以在不同的Ruby解釋器之間切換。
?
??? 安裝好以后,我比較喜歡在/usr/local/bin下面建立軟連接的方式來把Ruby加入系統搜索路徑:
??? 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
??? 確認服務器已經連接互聯網的情況下執行:
??? gem install rails
??? 即通過gem從rubyforge網站下載rails所有依賴包安裝。
??
??? 安裝好rails以后,可以執行:
??? ln -s /opt/ruby-1.9.2-p180/bin/rails /usr/local/bin/rails
??? rails –v
??? 確認一下rails的版本。
??? 三、安裝Ruby的數據庫適配器
??? Rails發行包中已經自帶純Ruby的MySQL數據庫適配器,然而對于生產環境來說,我們仍然應該下載安裝C版本的數據庫適配器,以達到更好的性能。Rails升級到3.0之后,推薦使用mysql2作為MySQL驅動,從這里可以下載mysql2-0.2.11.gem
??? gem install mysql2-0.2.11.gem -l -- --with-mysql-dir=/opt/mysql5
??? 注意--with-mysql-dir應該指向MySQL數據庫的安裝路徑,如果數據庫服務器和Web服務器不在同一臺機器上,那么Web服務器上也必須安裝MySQL軟件,因為ruby的C版本MySQL適配器需要在編譯的時候聯接MySQL的系統庫。
??? 四、安裝fcgi支持庫
??? 由于Ruby的fcgi支持庫需要在編譯的時候聯接fcgi的系統庫,因此我們需要先安裝fcgi庫,下載fcgi源代碼發行包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安裝在自己指定的目錄下,而不是默認的/usr/local,避免多個軟件混在一起。
??? 五、安裝Ruby的FCGI支持
??? 接下來就可以安裝ruby的fcgi支持庫了,下載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
??? 注意:安裝完畢之后,請確保/opt/ruby-1.9.2/lib/ruby/site_ruby/1.9.1/i686-linux/目錄下有這三個共享庫文件:fcgi.so? mysql.so? openssl.so,否則運行中會出現問題。如果缺少fcgi.so請重復步驟五,如果缺少mysql.so請重復步驟三,如果缺少openssl.so,請安裝openssl庫之后重復步驟一。
??? 六、安裝Lighttpd Web Server
??? 在安裝lighttpd之前,應該確認操作系統已經安裝pcre,即Perl兼容的規則表達式庫。
??? 如果沒有,請執行:
??? 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完畢以后,會給出一個激活的模塊和沒有激活模塊的清單,可以檢查一下,是否自己需要的模塊都已經激活,在enable的模塊中一定要有“mod_rewrite”這一項,否則重新檢查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/
??? 同時可以根據你的實際情況進行修改,主要修改如下:
??? 1)對/etc/lighttpd/modules.conf進行如下修改:
?????? 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",
?????? )
?????? 這個文件設置了lighttpd需要開啟的模塊。
??? 2)對/etc/lighttpd/modules.conf進行如下修改:
?????? server.document-root, server.error-log,accesslog.filename需要指定相應的目錄,如果沒有創建對應的目錄,lighttpd啟動時會報錯。
??? 3)用什么權限來運行Lighttpd
?????? server.username = "USER_NAME"
?????? server.groupname = "USER_GROUP"
?????? 從安全角度來說,不建議用root權限運行Web Server,可以自行指定普通用戶權限。
??? 4) 配置Lighttpd和Ruby on Rails之間的通信
?????? 這一步配置十分重要,最簡單的配置如下:
?????? $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啟動2個FCGI進程,lighttpd和fcgi之間使用本機Unix Socket通信。
?????? socket:指定了通信的socket文件的存放位置;
?????? bin-path:指定了request轉發的程序位置;
?????? server.error-handler-404:指定了請求的資源找不到時,進行處理的程序。相當于把動態請求過濾給Ruby on Rails服務器進行處理。
?????? 5) 配置mime
????????? 必須設置mime配置文件,不然lighttpd無法識別css、image等資源類型。
?????????
????????? 在/etc/lighttpd/lighttpd.conf中添加一行:
????????? include "mime.conf"
????????? 然后創建mime配置文件:
????????? touch /etc/lighttpd/mime.conf
?????????
????????? 并且加入如下內容:
????????? ##? 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",
????????? )
????八、啟動Lighttpd
??? 在/etc/hosts文件中加入下面一行:
??? 127.0.0.1?????? testmengli.com
??? Lighttpd的啟動命令是:/usr/local/lighttpd/sbin/lighttpd -f /etc/lighttpd/lighttpd.conf
??? 通過以上步驟,就基本實現了Rails應用在生產環境下的部署,用到的技術包括Lighttpd+fcgi+mysql,希望對大家有幫助!
轉載于:https://www.cnblogs.com/mengli/archive/2012/11/20/2778431.html
總結
以上是生活随笔為你收集整理的Linux下Rails3 + Lighttpd + fcgi部署研究的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python中设置时间格式--模块dat
- 下一篇: ARM Linux.2.6.34内核移植