debian上やubuntu上のapache2のユーザーディレクトリでRailsを動かす

OSC東北2008で高橋@衣川さんからapache2で動かす方法を教えてくれと言われたので、
debian上で動かす方法をお知らせします。
※ 私はRailsはapache2+fcgidの組み合わせが好きです

以下のものをaptで入れて有効にしておきましょう

  • mod_rewrite(apache2に入っている)
  • mod_fcgid(libapache2-mod-fcgid)
  • rubyfastcgiライブラリ(gem install fastcgiかaptでlibfcgi-ruby1.8)

では、とりあえずfastcgiではなく、重い重いcgi

  • ~/rails に一式アップロード
  • DBを準備して、script/consoleとかscript/runnerとかを動かしてDBとRailsが接続できるのを確認します
    • ./script/runner 'p User.find_all' とかを実行
  • ~xibbar/rails/publicの中身をごっそり~xibbar/public_html/sampleにコピー
  • ~xibbar/public_html/sample/.htaccessを編集
--- .htaccess   2008-02-08 15:47:28.594050840 +0900
+++ .htaccess   2007-04-19 15:08:56.032149526 +0900
@@ -1,5 +1,5 @@
 # General Apache options
-AddHandler fastcgi-script .fcgi
+#AddHandler fastcgi-script .fcgi
 AddHandler cgi-script .cgi
 Options +FollowSymLinks +ExecCGI
 
@@ -24,12 +24,12 @@
 #
 # Example:
 #   Alias /myrailsapp /path/to/myrailsapp/public
-#   RewriteBase /myrailsapp
+RewriteBase /~xibbar/sample
 
 RewriteRule ^$ index.html [QSA]

ハンドラーはdebianで設定しているので、コメントします。
RewriteBaseをここで書くのがミソです。
次にdispatch.cgiを開いて編集します。

--- dispatch.cgi    2008-02-08 15:47:28.642051830 +0900
+++ dispatch.cgi    2008-02-08 15:57:53.478961417 +0900
@@ -1,10 +1,11 @@
 #!/usr/bin/ruby1.8
+RAILS_ENV="production"
 
-require File.dirname(__FILE__) + "/../config/environment" unless defined?(RAILS_ROOT)
+require File.dirname(__FILE__) + "/../../rails/sample/config/environment" unless defined?(RAILS_ROOT)
 
 # If you're using RubyGems and mod_ruby, this require should be changed to an absolute path one, like:
 # "/usr/local/lib/ruby/gems/1.8/gems/rails-0.8.0/lib/dispatcher" -- otherwise performance is severely impaired
 require "dispatcher"
 
 ADDITIONAL_LOAD_PATHS.reverse.each { |dir| $:.unshift(dir) if File.directory?(dir) } if defined?(Apache::RubyRun)

本番環境なのでproduction環境にする宣言をします。
あとはRailsアプリのある場所へパスを変更します。
以上でcgiで動くと思います。おそらく5秒以上表示にかかると思いますが。
次にfastcgiで動かします。
まず、呼び出し先のファイル名の変更します。

--- .htaccess   2008-02-08 16:04:37.035296291 +0900
+++ .htaccess   2007-04-19 15:08:56.032149526 +0900
@@ -29,7 +29,7 @@
 RewriteRule ^$ index.html [QSA]
 RewriteRule ^([^.]+)$ $1.html [QSA]
 RewriteCond %{REQUEST_FILENAME} !-f
-RewriteRule ^(.*)$ dispatch.cgi [QSA,L]
+RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]

.cgiを.fcgiにしているだけです。
次にdispatch.fcgiを編集します。

--- dispatch.fcgi   2008-02-08 15:47:28.642051830 +0900
+++ dispatch.fcgi   2008-02-08 15:58:09.191286043 +0900
@@ -1,4 +1,5 @@
 #!/usr/bin/ruby1.8
+RAILS_ENV="production"
 #
 # You may specify the path to the FastCGI crash log (a log of unhandled
 # exceptions which forced the FastCGI instance to exit, great for debugging)
@@ -18,7 +19,7 @@
 #   # Custom log path, normal GC behavior.
 #   RailsFCGIHandler.process! '/var/log/myapp_fcgi_crash.log'
 #
-require File.dirname(__FILE__) + "/../config/environment"
+require File.dirname(__FILE__) + "/../../rails/sample/config/environment"
 require 'fcgi_handler'
 
 RailsFCGIHandler.process!

さっきのcgiと同じようにfcgiのファイルも
RAILS_ENVを設定して、パスを変更します。
以上で終了なのですが、fastcgiを使う場合は
もうちょっとサーバー側での修正が必要です。
/etc/apache2/mods-enabled/fcgid.conf を編集します。

<IfModule mod_fcgid.c>
  AddHandler fcgid-script .fcgi
  SocketPath /var/lib/apache2/fcgid/sock
  IPCConnectTimeout 20
</IfModule>

IPCConnectTimeoutというのはfcgidのタイムアウト時間です。
これを設定しないとタイムアウトエラーが多数発生します。