9.16. Ruby を使用する(mod_ruby)

Turbolinux 11 Server には、Apache で ruby を利用するための mod_ruby モジュールが標準で提供されており、初期設定のままで機能しています。mod_ruby が正常に動作していることを確認するために、以下の内容を記述した eruby-test.rhtml ファイルを利用できます。本ガイドでは Ruby および eRuby の詳細については触れません。プログラミングの詳細については、Ruby の Webサイト(http://www.ruby-lang.org/ja/)や専門書籍を参照してください。

<html><head><title>eRuby test</title></head><body>
<p><%="eRuby test"%></p>
</body></html>

作成後、eruby-test.rhtml を Apache の DocumentRoot である /var/www/html/ ディレクトリにコピーします。次に、Web ブラウザを起動し、以下の URL へアクセスします。

http://fully-qualified-domain-name/eruby-test.rhtml

正常に動作していれば、eRuby test と Web ブラウザに表示されます。

mod_ruby モジュールは、以下のように /etc/httpd/conf.d/mod_ruby.conf で読み込まれています。また、拡張子が .rbx と .rhtml のファイルをそれぞれ ruby スクリプトと eRuby(embedded Ruby: HTML 文の中に組み込む ruby)スクリプトとして認識するようにデフォルトで設定されています。mod_ruby モジュールの関連ディレクティブについては、mod_ruby.net の Web サイト http://modruby.net/ja/ を参照してください。

LoadModule      ruby_module     modules/mod_ruby.so

# If the ruby module is installed, this will be enabled.
<IfModule mod_ruby.c>
  # for Apache::RubyRun
  RubyRequire apache/ruby-run

  # exec files under /ruby as ruby scripts.
  # <Location /ruby>
  #   SetHandler ruby-object
  #   RubyHandler Apache::RubyRun.instance
  #   Options ExecCGI
  # </Location>

  # exec *.rbx as ruby scripts.
  <Files *.rbx>
    SetHandler ruby-object
    RubyHandler Apache::RubyRun.instance
  </Files>

  # for Apache::ERubyRun
  RubyRequire apache/eruby-run

  # handle files under /eruby as eRuby files by eruby.
  # <Location /eruby>
  #   SetHandler ruby-object
  #   RubyHandler Apache::ERubyRun.instance
  #   Options ExecCGI
  # </Location>

  # handle *.rhtml as eruby files.
  <Files *.rhtml>
    SetHandler ruby-object
    RubyHandler Apache::ERubyRun.instance
  </Files>

  # for Apache::ERbRun
  # RubyRequire apache/erb-run

  # handle files under /erb as eRuby files by ERb.
  # <Location /erb>
  #   SetHandler ruby-object
  #   RubyHandler Apache::ERbRun.instance
  #   Options ExecCGI
  # </Location>

  # for debug
  # RubyRequire auto-reload
</IfModule>