Lighttpd Web Server

Web Server といえばApache(日本語サイト)/The Apache Software Foundationがもっとも一般的で有名だが、ここではLighttpdを使ってWeb Serverを立ち上げることを想定する。

Lighttpdは高速で軽いのが特徴だが、Rental Hostingで利用するにはそれなりに制限があるので、何にでも利用できるとは言えない。また、管理権限をDirectory毎に持つような、.htaccessを用いた制御などは難しいので、Virtual Hostingとして使うときには運用上難しい点が存在する。

Installed application

LighttpdはpkgsrcからInstallする。

なお、クライアント側からのrequest-bodyの送信速度を保証する為には、手前にsquid等を設置してReverse Proxyとして動かす方が良さそうなのだが、ここではそこまではしない。

今回はCMS等に対応するためにも、bzip/ssl/memcache/luaを導入することにする。また、認証などでDatabaseを利用したい場合には、mySQLやLDAP等との連携も考える必要があるだろう。また、mod_trigger_b4を利用するなら、libmemcacheかgdbmを生かす必要がある。今回は使わないが、一応libmemcacheは入れておく。

なお、apache並のURL Rewriteをするならば、lua必須で、mod_magnetを導入すること。特にCMS対応を考えるなら必須だと思った方が良い。

PKG_OPTIONS.lighttpd=           bzip ssl memcache lua   # fam ldap gdbm mysql

あとは、普通にcompile。

Configuration

mod_magnet

CMSを利用するなら、URLを書き換えたいことは非常に多くあるはず。その場合に、apacheでは

# cat .htaccess
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
#

のような設定をすることが多い。しかし、lighttpdのmod_rewriteではこの設定は出来ない。なぜなら、-f-dを判断できないから。

というわけで、mod_magnetを利用して設定する事になる。

まず、lighttpd.confで、こんな感じの設定を。今回はDrupalを引き合いに出すが、考え方はConcrete5でもWordPressでも同じ。

$HTTP["url"] =~ "^/drupal" {
    # we only need index.php here.
    index-file.names = ( "index.php" )
    # for clean urls
    magnet.attract-physical-path-to = ( "/some/where/drupal.lua" )
}

で、/some/where/drupal.luaはこんな感じ

-- little helper function
function file_exists(path)
  local attr = lighty.stat(path)
  if (attr) then
      return true
  else
      return false
  end
end
function removePrefix(str, prefix)
  return str:sub(1,#prefix+1) == prefix.."/" and str:sub(#prefix+2)
end

-- prefix without the trailing slash
local prefix = '/drupal'

-- the magic ;)
if (not file_exists(lighty.env["physical.path"])) then
    -- file still missing. pass it to the fastcgi backend
    request_uri = removePrefix(lighty.env["uri.path"], prefix)
    if request_uri then
      lighty.env["uri.path"]          = prefix .. "/index.php" 
      local uriquery = lighty.env["uri.query"] or "" 
      lighty.env["uri.query"] = uriquery .. (uriquery ~= "" and "&" or "") .. "q=" .. request_uri
      lighty.env["physical.rel-path"] = lighty.env["uri.path"]
      lighty.env["request.orig-uri"]  = lighty.env["request.uri"]
      lighty.env["physical.path"]     = lighty.env["physical.doc-root"] .. lighty.env["physical.rel-path"]
    end
end
-- fallthrough will put it back into the lighty request loop
-- that means we get the 304 handling for free. ;)

あとはluaのcodeとManualをよく見て欲しい。

注意点

  • fastcgiを利用できる
    • しかし、SignalをMaskしてくれない。故に、lighttpdを操作する為に流したシグナルが、lighttpdの子プロセスであるfastcgiプロセスまで到達する。
    • たとえば、動作中のlighttpdの動作を制御する際に、SIGHUPやSIGINTやSIGUSR1を使うが、それが、fastcgiプロセスにも到達する。

Basic Authentication

Basic 認証を行うには、設定ファイルにいちいち記載する必要がある。ここがapacheと比べて面倒。

Apacheは.htaccessを書くことで対応できるが、LighttpdはConfig fileに書かなければならないので、Serverの再起動が必要となる。

Module mod_auth - Using Authentication

  1. password fileを作る
    • この例では Plain text passwordにしてある。digestも利用できるがとりあえず。
    • /usr/pkg/etc/lighttpd/lighty-pass-global
      lighty-pass-global
      example1:_zxcvb_
      example2:qwerty!!
      
    • /usr/pkg/etc/lighttpd/lighty-pass-vhost
      lighty-pass-vhost
      vhexample1:_zxcvb_
      vbexample2:qwerty!!
      
  2. lighttpd.confを修正
    • System全体に関連するもの
      #### auth module
      ## read authentication.txt for more info
      auth.backend               = "plain"					← Plaintext認証
      auth.backend.plain.userfile = "/user/pkg/etc/lighty-pass-global"	← password file
      
      auth.require               = ( "/server-status" =>
                                     (
                                       "method"  => "basic",
                                       "realm"   => "admin",
                                       "require" => "user=example1"
                                     ),
                                     "/server-config" =>
                                     (
                                       "method"  => "basic",
                                       "realm"   => "admin",
                                       "require" => "user=example1|user=example2"
                                     ),
                                     "/phpinfo.php" =>
                                     (
                                       "method"  => "basic",
                                       "realm"   => "admin",
                                       "require" => "valid-user"
                                     )
                                   )
      
    • Virtual host毎のもの
      $HTTP["host"] == "virtualhost.example.net" {
      auth.backend                   = "plain"				Plaintext認証
      auth.require                   = ( "/vhost-special.php" =>
                                         (
                                           "method"  => "basic",
                                           "realm"   => "vhost-special",
                                           "require" => "valid-user"
                                         ),
                                       )
      ....
      }
      
  3. 再起動
    • # /etc/rc.d/lighttpd restart
      
serverapp/lighttpd.txt · 最終更新: 2010/05/18 11:38 by seirios
CC Attribution-Noncommercial-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0