Web Server といえばApache(日本語サイト)/The Apache Software Foundationがもっとも一般的で有名だが、ここではLighttpdを使ってWeb Serverを立ち上げることを想定する。
Lighttpdは高速で軽いのが特徴だが、Rental Hostingで利用するにはそれなりに制限があるので、何にでも利用できるとは言えない。また、管理権限をDirectory毎に持つような、.htaccessを用いた制御などは難しいので、Virtual Hostingとして使うときには運用上難しい点が存在する。
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。
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をよく見て欲しい。
Basic 認証を行うには、設定ファイルにいちいち記載する必要がある。ここがapacheと比べて面倒。
Apacheは.htaccessを書くことで対応できるが、LighttpdはConfig fileに書かなければならないので、Serverの再起動が必要となる。
Module mod_auth - Using Authentication
/usr/pkg/etc/lighttpd/lighty-pass-global example1:_zxcvb_ example2:qwerty!!
vhexample1:_zxcvb_ vbexample2:qwerty!!
#### 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"
)
)
$HTTP["host"] == "virtualhost.example.net" {
auth.backend = "plain" Plaintext認証
auth.require = ( "/vhost-special.php" =>
(
"method" => "basic",
"realm" => "vhost-special",
"require" => "valid-user"
),
)
....
}
# /etc/rc.d/lighttpd restart