Lighttpd (日本語)
Lighttpd は高速性の重視される環境に最適化された、安全・高速で標準に準拠し、とても柔軟なウェブサーバーです。他のウェブサーバーと比べてメモリ使用量や CPU 負担が少ないのが特徴です。高度な機能セット (FastCGI, CGI, 認証, 圧縮出力, URL の書き換えなど) により、lighttpd は負担が気になるサーバーにうってつけのウェブサーバーソフトウェアとして君臨します。
Contents
インストール
公式リポジトリから lighttpd パッケージをインストールしてください。
設定
基本設定
lighttpd の設定ファイルは /etc/lighttpd/lighttpd.conf です。デフォルトではテストページが表示されるようになっています。
lighttpd.conf に問題が存在しないか次のコマンドを使うことで確認できます、これによって設定の誤りを早期発見することが可能です:
$ lighttpd -t -f /etc/lighttpd/lighttpd.conf
デフォルトの設定ファイルでは /srv/http/ をウェブ上に公開するドキュメントのディレクトリとして指定しています。
インストールをテストするには:
# echo 'TestMe!' >> /srv/http/index.html # chmod 755 /srv/http/index.html
サーバーを起動するには:
# systemctl start lighttpd
ブラウザで localhost を開いて見て下さい、テストページが表示されるはずです。
ブート時にサーバーを起動させるには:
# systemctl enable lighttpd
設定ファイルのサンプルは /usr/share/doc/lighttpd/ にあります。
CGI
Lighttpd では、特に設定をしなくても、CGI モジュールを有効にするだけで CGI スクリプトが動作します。使用するプログラミング言語のインタプリタがインストールされているか確認してください (例えば python なら python をインストールします)。
/etc/lighttpd/conf.d/cgi.conf ファイルを作成して以下の内容を追加します:
server.modules += ( "mod_cgi" )
cgi.assign = ( ".pl" => "/usr/bin/perl",
".cgi" => "/usr/bin/perl",
".rb" => "/usr/bin/ruby",
".erb" => "/usr/bin/eruby",
".py" => "/usr/bin/python",
".php" => "/usr/bin/php-cgi" )
index-file.names += ( "index.pl", "default.pl",
"index.rb", "default.rb",
"index.erb", "default.erb",
"index.py", "default.py",
"index.php", "default.php" )
PHP スクリプトの場合、以下を /etc/php/php.ini に設定する必要があります:
cgi.fix_pathinfo = 1
Lighttpd の設定ファイルを /etc/lighttpd/lighttpd.conf に以下を追加してください:
include "conf.d/cgi.conf"
FastCGI
fcgi をインストールしてください。それで lighttpd に fcgi サポートが追加されます。fcgi サポートを追加するだけなら設定はそれだけです。Ruby on Rails, PHP, Python などを使いたい場合は以下を読んで下さい。
まず /usr/share/doc/lighttpd/config/conf.d/fastcgi.conf から /etc/lighttpd/conf.d にサンプル設定ファイルをコピーします。
以下を設定ファイル /etc/lighttpd/conf.d/fastcgi.conf に追加する必要があります:
server.modules += ( "mod_fastcgi" )
#server.indexfiles += ( "dispatch.fcgi" ) #this is deprecated
index-file.names += ( "dispatch.fcgi" ) #dispatch.fcgi if rails specified
server.error-handler-404 = "/dispatch.fcgi" #too
fastcgi.server = (
".fcgi" => (
"localhost" => (
"socket" => "/run/lighttpd/rails-fastcgi.sock",
"bin-path" => "/path/to/rails/application/public/dispatch.fcgi"
)
)
)
そして /etc/lighttpd/lighttpd.conf に以下を記述してください:
include "conf.d/fastcgi.conf"
PHP と Ruby on Rails については次のセクションを見て下さい。
PHP
php と php-cgi をインストールします (PHP や LAMP を参照)。
php-cgi が動作するかは php-cgi --version で確認:
PHP 5.4.3 (cgi-fcgi) (built: May 8 2012 17:10:17) Copyright (c) 1997-2012 The PHP Group Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies
以上のように出力されたら php は正しくインストールされています。
新しい設定ファイルを作成:
/etc/lighttpd/conf.d/fastcgi.conf
# Make sure to install php and php-cgi. See:
# https://wiki.archlinux.org/index.php/Fastcgi_and_lighttpd#PHP
server.modules += ("mod_fastcgi")
# FCGI server
# ===========
#
# Configure a FastCGI server which handles PHP requests.
#
index-file.names += ("index.php")
fastcgi.server = (
# Load-balance requests for this path...
".php" => (
# ... among the following FastCGI servers. The string naming each
# server is just a label used in the logs to identify the server.
"localhost" => (
"bin-path" => "/usr/bin/php-cgi",
"socket" => "/tmp/php-fastcgi.sock",
# breaks SCRIPT_FILENAME in a way that PHP can extract PATH_INFO
# from it
"broken-scriptfilename" => "enable",
# Launch (max-procs + (max-procs * PHP_FCGI_CHILDREN)) procs, where
# max-procs are "watchers" and the rest are "workers". See:
# https://redmine.lighttpd.net/projects/1/wiki/frequentlyaskedquestions#How-many-php-CGI-processes-will-lighttpd-spawn
"max-procs" => 4, # default value
"bin-environment" => (
"PHP_FCGI_CHILDREN" => "1" # default value
)
)
)
)
lighttpd が新しい設定ファイルを使うように設定:
/etc/lighttpd/lighttpd.conf
include "conf.d/fastcgi.conf"
lighttpd をリロード:
# systemctl reload lighttpd
php-fpm を使う
最近の lighttpd のリリースでは適応型のプロセス生成はなくなっています。PHP プロセスの動的な管理がしたい場合は、php-fpm を使うことができます。
# pacman -S php-fpm # systemctl enable php-fpm # systemctl start php-fpm
/etc/lighttpd/conf.d/fastcgi.conf に以下を追加:
server.modules += ( "mod_fastcgi" )
index-file.names += ( "index.php" )
fastcgi.server = (
".php" => (
"localhost" => (
"socket" => "/run/php-fpm/php-fpm.sock",
"broken-scriptfilename" => "enable"
))
)
eAccelerator
AUR から eaccelerator をインストールします。
eaccelerator の設定ファイルを追加:
/etc/php/conf.d/eaccelerator-own.ini
zlib.output_compression = On cgi.fix_pathinfo=1 eaccelerator.cache_dir="/home/phpuser/eaccelerator/cache"
php ページのテスト
以下の php ページを作成して、index.php という名前をつけ、/srv/http/ と /srv/http-ssl/html/ の両方にコピーを配置してください:
<?php phpinfo(); ?>
ウェブブラウザを使って、あなたのサーバーの http と https 両方のアドレスを開いてみて下さい。phpinfo のページが表示されるはずです。
eaccelerator のキャッシュの確認:
# ls -l /home/phpuser/eaccelerator/cache
上のコマンドで以下のような出力がされる場合:
-rw------- 1 phpuser phpuser 456 2005-05-05 14:53 eaccelerator-277.58081 -rw------- 1 phpuser phpuser 452 2005-05-05 14:53 eaccelerator-277.88081
eaccelerator は正しく php スクリプトをキャッシュしており、速度がアップしています。
Ruby on Rails
FastCGI をインストール・設定してください (上の #FastCGI を参照)。
[extra] から ruby を、AUR から ruby-fcgi をインストールしてください。
RubyOnRails の指示に従って下さい。
Python FastCGI
FastCGI をインストール・設定してください (上の #FastCGI を参照)。
flup をインストール:
# pacman -S python2-flup
設定:
fastcgi.server = (
".py" =>
(
"python-fcgi" =>
(
"socket" => "/run/lighttpd/fastcgi.python.socket",
"bin-path" => "test.py",
"check-local" => "disable",
"max-procs" => 1,
)
)
)
test.py をサーバーのルートに置いて下さい (忘れずに chmod +x で実行権限を与えます)。
#!/usr/bin/env python2
def myapp(environ, start_response):
print 'got request: %s' % environ
start_response('200 OK', [('Content-Type', 'text/plain')])
return ['Hello World!']
if __name__ == '__main__':
from flup.server.fcgi import WSGIServer
WSGIServer(myapp).run()
Thanks to firecat53 for his explanation
SSL
SSL 証明書は以下のように生成します:
# mkdir /etc/lighttpd/certs # openssl req -x509 -nodes -days 7300 -newkey rsa:2048 -keyout /etc/lighttpd/certs/www.example.com.pem -out /etc/lighttpd/certs/www.example.com.pem # chmod 600 /etc/lighttpd/certs/www.example.com.pem
/etc/lighttpd/lighttpd.conf を編集してください。lighttpd を SSL のみにするには (サーバーのポートを 443 に設定する必要があります):
ssl.engine = "enable" ssl.pemfile = "/etc/lighttpd/certs/www.example.com.pem"
通常の HTTP に加えて SSL も有効にするには:
$SERVER["socket"] == ":443" {
ssl.engine = "enable"
ssl.pemfile = "/etc/lighttpd/certs/www.example.com.pem"
}
別のサイトを運営したい場合、socket 条件文でドキュメントルートを変更できます:
$SERVER["socket"] == ":443" {
server.document-root = "/srv/ssl" # use your ssl directory here
ssl.engine = "enable"
ssl.pemfile = "/etc/lighttpd/certs/www.example.com.pem" # use the path where you created your pem file
}
もしくは scheme 条件文を使ってセキュアなリクエストと通常のリクエストを区別することも可能です:
$HTTP["scheme"] == "https" {
server.document-root = "/srv/ssl" # use your ssl directory here
ssl.engine = "enable"
ssl.pemfile = "/etc/lighttpd/certs/www.example.com.pem" # use the path where you created your pem file
}
上記の ssl.engine のまわりで scheme 条件文を使うことはできないので注意してください。lighttpd がどのポートで SSL を有効にするのか知る必要があるためです。
Server Name Indication
lighttpd で SNI を使用するには、ssl.pemfile 設定ディレクティブを host 条件文の中に追加してください。デフォルトの ssl.pemfile が必要です。
$HTTP["host"] == "www.example.org" {
ssl.pemfile = "/etc/lighttpd/certs/www.example.org.pem"
}
$HTTP["host"] == "mail.example.org" {
ssl.pemfile = "/etc/lighttpd/certs/mail.example.org.pem"
}
HTTP のリクエストを HTTPS にリダイレクト
/etc/lighttpd/lighttpd.conf の server.modules 行に "mod_redirect" を追加してください:
server.modules += ( "mod_redirect" )
$SERVER["socket"] == ":80" {
$HTTP["host"] =~ "example.org" {
url.redirect = ( "^/(.*)" => "https://example.org/$1" )
server.name = "example.org"
}
}
$SERVER["socket"] == ":443" {
ssl.engine = "enable"
ssl.pemfile = "/etc/lighttpd/ssl/server.pem"
server.document-root = "..."
}
全てのホストをセキュアな URL にリダイレクトするには上の socket 80 の設定のところに以下を記述します:
$SERVER["socket"] == ":80" {
$HTTP["host"] =~ ".*" {
url.redirect = (".*" => "https://%0$0")
}
}
サイトの一部だけリダイレクトするには (例: secure または phpmyadmin):
$SERVER["socket"] == ":80" {
$HTTP["url"] =~ "^/secure" {
url.redirect = ( "^/(.*)" => "https://example.com/$1" )
}
}
圧縮出力
/etc/lighttpd/lighttpd.conf に以下を追加:
var.cache_dir = "/var/cache/lighttpd"
圧縮ファイル用にディレクトリを作成:
# mkdir /var/cache/lighttpd/compress # chown http:http /var/cache/lighttpd/compress
サンプル設定ファイルをコピー:
# mkdir /etc/lighttpd/conf.d # cp /usr/share/doc/lighttpd/config/conf.d/compress.conf /etc/lighttpd/conf.d/
以下を /etc/lighttpd/lighttpd.conf に追加:
include "conf.d/compress.conf"
トラブルシューティング
Lighttpd で .php ファイルがダウンロードされてしまう
.php ファイルが実行されず lighttpd によってダウンロードが始まってしまう場合、おそらく以下の行を /etc/lighttpd/lighttpd.conf に追加し忘れています:
server.modules = (
"mod_fastcgi",
)
fastcgi.server = ( ".php" => ((
"bin-path" => "/usr/bin/php-cgi", #depends where your php-cgi has been installed. Default here.
"socket" => "/tmp/php.socket",
"max-procs" => 2,
"bin-environment" => (
"PHP_FCGI_CHILDREN" => "16",
"PHP_FCGI_MAX_REQUESTS" => "10000"
),
"bin-copy-environment" => (
"PATH", "SHELL", "USER"
),
"broken-scriptfilename" => "enable"
)))