Rails + lighttpd + SSL

今回は、Railslighttpdで動かし、SSLもかけてみようと言うもの。


1.RubyRailsのインストールはこちらをご覧ください。
ruby install (update) - 基本へ帰ろう
gem install - 基本へ帰ろう
rails install - 基本へ帰ろう
↑こちらを参考にしてください。

2.秘密鍵、公開鍵、ルート証明書の準備
テストWebサーバー(WEBrick)準備(ベリサインSSL導入) - 基本へ帰ろう
↑こちらを参考にして下さい。

3.pcreのインストール

# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-6.5.tar.gz
# tar xvfz pcre-6.5.tar.gz
# cd pcre-6.5
# ./configure
# make
# make install

4.ruby-fcgiのインストール

wget http://sugi.nemui.org/pub/ruby/fcgi/ruby-fcgi-0.8.6.tar.gz
tar zxf ruby-fcgi-0.8.6.tar.gz
cd ruby-fcgi-0.8.6
ruby install.rb config -- --with-fcgi-include=/usr/local/fcgi/include --with-fcgi-lib=/usr/local/fcgi/lib
ruby install.rb setup
ruby install.rb install

5.lighttpdインストール

# wget http://www.lighttpd.net/download/lighttpd-1.4.18.tar.gz
# tar zxvf lighttpd-1.4.18.tar.gz 
# cd lighttpd-1.4.18
# ./configure --with-openssl --with-openssl-libs=/usr/lib --with-pcre
# make
# make install
# /usr/local/sbin/lighttpd -v
lighttpd-1.4.18 (ssl) - a light and fast webserver
Build-Date: Sep 20 2007 13:56:14

lighttpd.confは最初は、

/usr/lib/ruby/gems/1.8/gems/rails-1.2.3/configs/lighttpd.conf

↑ここにありますので、ここでもよいですが、

/etc/lighttpd/

↑このフォルダを作成して、lighttpd.confを入れてた方が管理しやすいかもしれません。
また、SSLの公開鍵、秘密鍵ルート証明書を格納するフォルダも

/etc/lighttpd/ssl/

↑このように作成するとすっきりするかもしれません。(この辺は好みですのでご自由に)


6.lighttpd.conf を記述

var.basedir = "/srv/rails_test/"
#↑Railsアプリケーションの場所

server.port = 81
#↑これの設定はちゃんと調べないとな・・・。

server.modules           = ( "mod_rewrite", "mod_alias", "mod_access", "mod_fastcgi", "mod_accesslog" )
server.error-handler-404 = "/dispatch.fcgi"
server.document-root     = var.basedir + "public/"

server.pid-file          = "/var/run/lighttpd.pid"

url.rewrite              = ( "^/$" => "index.html", "^([^.]+)$" => "$1.html" )

# multiple sockets(xxx.xxx.xxx.xxx にはIPを入れましょう)
$SERVER["socket"] == "xxx.xxx.xxx.xxx:80" {
  server.errorlog          = var.basedir + "log/lighttpd.error.log"
  accesslog.filename       = var.basedir + "log/lighttpd.access.log"
}

$SERVER["socket"] == "xxx.xxx.xxx.xxx:443" {
  ssl.engine = "enable"
  ssl.pemfile = "/etc/lighttpd/ssl/server.pem"
  ssl.ca-file = "/etc/lighttpd/ssl/ca.cer"

  server.errorlog          = var.basedir + "log/lighttpd-ssl.error.log"
  accesslog.filename       = var.basedir + "log/lighttpd-ssl.access.log"
}


# Change *-procs to 2 if you need to use Upload Progress or other tasks that
# *need* to execute a second request while the first is still pending.
fastcgi.server = ( ".fcgi" =>
  ( "localhost" =>
      (
        "min-procs" => 1,
        "max-procs" => 10,
        "socket"    => var.basedir + "log/fcgi.socket",
        "bin-path"  => var.basedir + "public/dispatch.fcgi",
        "bin-environment" => ( "RAILS_ENV" => "production" )
      )
  )
)

mimetype.assign = (
  ".css"        =>  "text/css",
  ".gif"        =>  "image/gif",
  ".htm"        =>  "text/html",
  ".html"       =>  "text/html",
  ".jpeg"       =>  "image/jpeg",
  ".jpg"        =>  "image/jpeg",
  ".js"         =>  "text/javascript",
  ".png"        =>  "image/png",
  ".swf"        =>  "application/x-shockwave-flash",
  ".txt"        =>  "text/plain"
)

7.lighttpd起動

/usr/local/sbin/lighttpd -f /etc/lighttpd/lighttpd.conf

○ポイント、はまりどころ
ファイヤーウォールで80(http)と443(https)は公開されていますか?
・log/lighttpd.error.logなどはRailsのデフォルトでは作成されませんので、自分でファイルを作りましょう。
lighttpd.confの

url.rewrite              = ( "^/$" => "index.html", "^([^.]+)$" => "$1.html" )

Railsアプリと競合する可能性があるので、うまくいかない方はコメントアウトするのも手かもしれません。
lighttpd.confのSSL

 ssl.pemfile = "/etc/lighttpd/ssl/server.pem" <= ここは、秘密鍵と公開鍵が記述されたファイル。
 ssl.ca-file = "/etc/lighttpd/ssl/ca.cer" <= ここは、ルート証明書

↓ちなみに、server.pemの中身はこんな感じ。

-----BEGIN RSA PRIVATE KEY-----
Proc-Type: X,XXXXXXXX
DEK-Info: XXX-XXX-XXX,xxxxxxxxxxxxxxxxxx

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-----END RSA PRIVATE KEY-----

-----BEGIN CERTIFICATE-----
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxx
-----END CERTIFICATE-----

※行数などは異なります。これは短いです。

↓また、ルート証明書の中身はこんな感じ

-----BEGIN CERTIFICATE-----
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxx
-----END CERTIFICATE-----

※行数などは異なります。これは短いです。
※server.pemの公開鍵と似てますよね。



○環境

$ ruby -v
ruby 1.8.6 (2007-03-13 patchlevel 0) [i686-linux]
$ rails -v
Rails 1.2.3
$ /usr/local/sbin/lighttpd -v
lighttpd-1.4.18 (ssl) - a light and fast webserver
Build-Date: Sep 20 2007 13:56:14
$ less /etc/redhat-release
CentOS release 5 (Final)