--最近准备开发部署RoR,转一篇文章备用
可以毫不夸张地说,服务器应用的部署是整个开发周期中难度最大的任务之一。且不说要如何使得应用可扩展性好、系统吞吐量大、稳定性好...单只让应用run起来,就不是一件很简单的事情。Rails作为一个Web应用框架,几乎是最大程度地减轻了开发人员的负担:安装Ruby运行环境、Rails和所需使用的数据库服务器之后,开发人员就可以开始用Rails自带的WEBrick服务器进行开发和调试了,基本上不需要对环境作太多的配置(当然,如果需要使用一些Ruby或者Rails的第三方库,自然是需要额外安装等)。然而到了将应用部署到实际环境中的时候,我们就要接触到繁杂的部署过程了,这时候,只靠官方的文档,往往是不够的,一般还要加上Google和最重要的,耐心。
好,在部署之前,我们看看Rails应用可以在什么环境下部署:
通常来说,比较一下上面的表格,我们会很自然地选择lighttpd-fcgi这种方案来部署我们的应用,soopie正是选用了这一方案。
接下来我们给出Rails应用在Linux服务器上部署的基本过程:
好,在部署之前,我们看看Rails应用可以在什么环境下部署:
![](http://static.flickr.com/46/139476796_9b5b36217f_o.jpg)
通常来说,比较一下上面的表格,我们会很自然地选择lighttpd-fcgi这种方案来部署我们的应用,soopie正是选用了这一方案。
接下来我们给出Rails应用在Linux服务器上部署的基本过程:
- 所需要的软件包:
-
- lighttpd-1.4.10.tar.gz
- fcgi-2.4.0.tar.gz
- ruby-1.8.4.tar.gz
- rubygems-0.8.11.tgz
- ruby-fcgi-0.8.6.tgz
- 安装步骤:
- 安装Ruby
- 安装rubygems
- 安装ruby-fcgi
- 安装rails,用命令行命令 gem install rails
- 安装lighttpd(创建用户www和组www, 创建目录/var/lighty作为文档根目录, /var/lighty/logs作为日志目录)
- 最关键的步骤:在lighttpd的配置文件(默认是/etc/lighttpd/lighttpd.conf)中配置rails应用支持。这里给出一个配置文件的完整例子(用Virtual host 方式配置,容易支持一个主机上多个rails应用):更详细的信息可以参考这里。
# lighttpd configuration file
#
# use it as a base for lighttpd 1.0.0 and above
#
# $Id: lighttpd.conf,v 1.7 2004/11/03 22:26:05 weigon Exp $
############ Options you really have to take care of ####################
server.modules = (
"mod_rewrite",
"mod_access",
"mod_fastcgi",
"mod_simple_vhost",
"mod_evhost",
"mod_accesslog" )
## a static document-root, for virtual-hosting take look at the
## server.virtual-* options
server.document-root = "/var/lighty/cartrain/public"
## where to send error-messages to
server.errorlog = "/var/lighty/logs/lighttpd.error.log"
# files to check for if .../ is requested
#index-file.names = ( "index.php", "index.html",
# "index.htm", "default.htm" )
## set the event-handler (read the performance section in the manual)
# server.event-handler = "freebsd-kqueue" # needed on OS X
# 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",
".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"
)
# Use the "Content-Type" extended attribute to obtain mime type if possible
#mimetype.use-xattr = "enable"
## send a different Server: header
## be nice and keep it at lighttpd
# server.tag = "lighttpd"
#### accesslog module
accesslog.filename = "/var/lighty/logs/access.log"
## deny access the file-extensions
#
# ~ is for backupfiles from vi, emacs, joe, ...
# .inc is often used for code includes which should in general not be part
# of the document-root
url.access-deny = ( "~", ".inc" )
$HTTP["url"] =~ "\.pdf$" {
server.range-requests = "disable"
}
##
# which extensions should not be handle via static-file transfer
#
# .php, .pl, .fcgi are most often handled by mod_fastcgi or mod_cgi
static-file.exclude-extensions = ( ".php", ".pl", ".fcgi" )
######### Options that are good to be but not neccesary to be changed #######
## bind to port (default: 80)
server.port = 80
var.osapp = "/var/lighty/cartrain"
$HTTP["host"] == "cu.ttthree.com"{
server.document-root = var.osapp + "/public"
server.error-handler-404="/dispatch.fcgi"
fastcgi.server =
( ".fcgi" =>
( "localhost" =>
( "min-procs" => 2,
"max-procs" => 2,
"socket" => "/tmp/mycoolapp.fcgi.socket",
"bin-path" => var.osapp+ "/public/dispatch.fcgi",
"bin-environment" => ( "RAILS_ENV" => "development" )
)
)
)
}
url.rewrite = (
"^/([\-_a-zA-Z0-9]+)/([\-_a-zA-Z0-9]+)/([\-_a-zA-Z0-9%]+)\??([\-_a-zA-Z0-9=&%]*)$" =>
"/dispatch.fcgi?controller=$1&action=$2&id=$3&$4",
"^/([\-_a-zA-Z0-9]+)/([\-_a-zA-Z0-9]+)/?\??([\-_a-zA-Z0-9=&%]*)$" =>
"/dispatch.fcgi?controller=$1&action=$2&$3",
"^/([\-_a-zA-Z0-9]+)/?\??([\-_a-zA-Z0-9=&%]*)$" =>
"/dispatch.fcgi?controller=$1&action=index&$2"
)