论坛首页 编程语言技术论坛

在Ubuntu下安装和配置Rails 3详解 (LightTPD + FastCGI)

浏览 12144 次
精华帖 (0) :: 良好帖 (17) :: 新手帖 (5) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-08-19   最后修改:2011-08-22
robbin很久之前的帖子讲的也是LightTPD+FastCGI配置Rails,不过现在的环境和Rails 3的特征使得有些细节不太一致,今天实际操作了一下,把详细过程记录了下来,供大家参考。

另外,Rails 3以后部署的选择又多了很多,比如Passenger或者用Capistrano,不知道和FastCGI相比性能如何?


先说明我的环境:

$ uname -a
Linux xjia-laptop 2.6.38-11-generic #48-Ubuntu SMP Fri Jul 29 19:05:14 UTC 2011 i686 i686 i386 GNU/Linux
$ ruby -v
ruby 1.9.2p290 (2011-07-09 revision 32553) [i686-linux]
$ rails -v
Rails 3.0.10

然后开始安装LightTPD:

$ mkdir ~/rails_deploy
$ cd ~/rails_deploy/
$ mkdir lighttpd
$ wget http://download.lighttpd.net/lighttpd/releases-1.4.x/lighttpd-1.4.29.tar.bz2
$ tar xvf lighttpd-1.4.29.tar.bz2
$ cd lighttpd-1.4.29
$ sudo apt-get build-dep lighttpd
$ ./configure --prefix=/home/xjia/rails_deploy/lighttpd --with-pcre --with-zlib --with-bzip2 --with-fam
... 省略
Plugins:

enabled:
  mod_access
  mod_accesslog
  ... 省略
disabled:
  mod_cml
  mod_magnet
  mod_mysql_vhost

Features:

enabled:
  auth-crypt
  compress-bzip2
  compress-deflate
  compress-gzip
  large-files
  network-ipv6
  regex-conditionals
  stat-cache-fam
disabled:
  auth-ldap
  network-openssl
  storage-gdbm
  storage-memcache
  webdav-locks
  webdav-properties

检查这个列表,看是否已经开启了你需要的所有东西,然后继续执行以下命令

$ make
$ make install
$ mkdir ../lighttpd/config
$ cp -R doc/config/conf.d/ doc/config/*.conf doc/config/vhosts.d/ ../lighttpd/config/
$ cd ../lighttpd
$ sbin/lighttpd -f config/lighttpd.conf -t
Syntax OK

这里说明lighttpd安装成功了

$ mkdir log
$ mkdir www
$ mkdir www/vhosts
$ mkdir www/htdocs
$ mkdir run
$ mkdir cache
$ mkdir sockets
$ gedit config/lighttpd.conf

在配置文件的一开始处可以找到下面几个变量,把里面的用户名“xjia”改成你自己的即可

var.log_root    = "/home/xjia/rails_deploy/lighttpd/log"
var.server_root = "/home/xjia/rails_deploy/lighttpd/www"
var.state_dir   = "/home/xjia/rails_deploy/lighttpd/run"
var.home_dir    = "/home/xjia/rails_deploy/lighttpd"
var.conf_dir    = "/home/xjia/rails_deploy/lighttpd/config"

下面这行也类似,你可以在文件里搜索var.cache_dir找到这一行

var.cache_dir   = "/home/xjia/rails_deploy/lighttpd/cache"

下面这一行设置LightTPD运行的端口,我的80、3000、8080端口都有服务在运行,所以设成9000

server.port = 9000

保存文件并继续执行以下命令

$ sbin/lighttpd -f config/lighttpd.conf -t
Syntax OK

这里说明配置文件修改后的格式无误

$ gedit lighttpd.sh

我们创建一个脚本方便对LightTPD进行操作,把下面的脚本放到lighttpd.sh文件中

#!/bin/sh

case "$1" in
  start)        
    /home/xjia/rails_deploy/lighttpd/sbin/lighttpd -f /home/xjia/rails_deploy/lighttpd/config/lighttpd.conf > /dev/null 2>&1
    ;;
  stop)
    killall lighttpd
    ;;
  restart)
   $0 stop
   sleep 1
   $0 start
   ;;
  *)
  echo "Usage: lighttpd.sh {start|stop|restart}"
  ;;
esac

exit 0

保存文件并继续执行以下命令

$ chmod a+rx lighttpd.sh
$ ./lighttpd.sh start
$ gedit www/htdocs/index.html

在index.html随便写点什么,比如<h1>It works!</h1>
保存文件然后访问 http://localhost:9000/ 就可以看到文件内容

$ ./lighttpd.sh restart

试一下重启LightTPD

$ ./lighttpd.sh stop

停止LightTPD服务

$ gedit config/modules.conf

配置如下

server.modules = (
  "mod_access",
#  "mod_alias",
#  "mod_auth",
#  "mod_evasive",
#  "mod_redirect",
  "mod_rewrite",
#  "mod_setenv",
#  "mod_usertrack",
)

# uncomment the following lines
include "conf.d/fastcgi.conf"
include "conf.d/simple_vhost.conf"
include "conf.d/cgi.conf"
include "conf.d/compress.conf"

$ gedit config/conf.d/compress.conf

配置如下

compress.filetype          = ("text/plain", "text/html", "text/javascript", "text/css

$ gedit config/conf.d/fastcgi.conf

配置如下,注意把“Enterprise-Wiki”等内容换成你自己的Rails应用程序的名字或所在的位置

$HTTP["host"] == "localhost" {
  server.document-root     = "/home/xjia/Enterprise-Wiki/public"
  server.dir-listing       = "disable"
  server.error-handler-404 = "/dispatch.fcgi"
 
  fastcgi.server = ( ".fcgi" =>
    ("localhost" =>
      ( "min-procs" => 1,
        "max-procs" => 5,
        "socket" => socket_dir + "/Enterprise-Wiki-fcgi.socket",
        "bin-path" => "/home/xjia/Enterprise-Wiki/public/dispatch.fcgi",
        "bin-environment" => (
              "RAILS_ENV" => "development",
              "TMP" => "/home/xjia/Enterprise-Wiki/tmp",
        ),
      )
    )
  )
}

下面配置FastCGI;注意:“Rails 3 is built on top of Rack and Rack provides a FastCGI handler.”
之前版本的Rails会在public下面有一个dispatch.fcgi,但Rails 3以后是基于Rack的所以没有了,要我们自己添加一个。

#!/usr/local/bin/ruby

require_relative '../config/environment'

class Rack::PathInfoRewriter
  def initialize(app)
    @app = app
  end

  def call(env)
    env.delete('SCRIPT_NAME')
    parts = env['REQUEST_URI'].split('?')
    env['PATH_INFO'] = parts[0]
    env['QUERY_STRING'] = parts[1].to_s
    @app.call(env)
  end
end

Rack::Handler::FastCGI.run Rack::PathInfoRewriter.new(EnterpriseWiki::Application)


先安装FastCGI:

$ cd ~/rails_deploy/
$ mkdir fcgi
$ wget http://www.fastcgi.com/dist/fcgi-2.4.0.tar.gz
$ tar xvf fcgi-2.4.0.tar.gz
$ cd fcgi-2.4.0
$ ./configure --prefix=/home/xjia/rails_deploy/fcgi
$ gedit libfcgi/fcgio.cpp

在#include <limits.h>上面加上下面这行:
#include <stdio.h>
保存文件并继续执行以下命令

$ make
$ make install

然后安装FastCGI for Ruby:

$ cd ~/rails_deploy/
$ wget http://rubyforge.org/frs/download.php/69127/fcgi-0.8.8.tgz
$ tar xvf fcgi-0.8.8.tgz
$ cd fcgi-0.8.8
$ ruby setup.rb config -- --with-fcgi-include=/home/xjia/rails_deploy/fcgi/include --with-fcgi-lib=/home/xjia/rails_deploy/fcgi/lib
$ ruby setup.rb setup
$ sudo ruby setup.rb install

FastCGI的东西都安装好了之后,就可以开启LightTPD运行我们的Rails应用程序了:

$ cd ~/rails_deploy/lighttpd/
$ ./lighttpd.sh start

现在访问 http://localhost:9000/ 你就可以看到你的程序在运行了(一开始可能有点慢,耐心等待一下)

P.S.
1) 在 /home/xjia/rails_deploy/lighttpd/log/error.log 里可以看到错误日志
2) 如果要配置开机自动启动等:http://redmine.lighttpd.net/wiki/lighttpd/InstallFromSource#Init-script
   发表时间:2011-08-22  
麻烦不……请布置第2台
0 请登录后投票
   发表时间:2011-08-23   最后修改:2011-08-23
chloerei 写道
麻烦不……请布置第2台


不麻烦。。我昨天刚刚在服务器上配置过一次
0 请登录后投票
   发表时间:2011-08-23  
二楼的意思是多装几台的话,太麻烦了。
0 请登录后投票
   发表时间:2011-08-24  
kingwmj 写道
二楼的意思是多装几台的话,太麻烦了。


哦,我这里是在一台服务器上部署多个虚拟机,所以只要对配置好的系统做一个snapshot就可以了
0 请登录后投票
   发表时间:2011-08-25  
我rails3.09 为什么下载不了mysql2?
who know?
0 请登录后投票
   发表时间:2011-08-25  
stfairy 写道
chloerei 写道
麻烦不……请布置第2台


不麻烦。。我昨天刚刚在服务器上配置过一次

好吧,这么一大篇还不麻烦。
0 请登录后投票
   发表时间:2011-08-25  
还好吧,用Nginx+RVM+Passenger虽然简单,但是如果要深入定制也要改很多配置文件。
0 请登录后投票
   发表时间:2011-08-26  
QuakeWang 写道
还好吧,用Nginx+RVM+Passenger虽然简单,但是如果要深入定制也要改很多配置文件。


不过我自己也有一个疑问,就是Nginx+Passenger在性能上是否能超过Lighttpd+FastCGI?Passenger官网的Performance Benchmark并没有涉及Lighttpd的评测,所以不是很清楚。而且Passenger已经被Rails官网称为preferred deployment setup了,DHH本人也对Passenger表示肯定。
0 请登录后投票
   发表时间:2011-08-27  
QuakeWang 写道
还好吧,用Nginx+RVM+Passenger虽然简单,但是如果要深入定制也要改很多配置文件。

最近觉得Passenger要编译Nginx,绑得太紧也有些问题。

未来想试试幼女控(Unicorn)这类完全和前端服务器分离的app server。
0 请登录后投票
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics