lighttpd

提供: ArchWiki
ナビゲーションに移動 検索に移動

lighttpd は高速性の重視される環境に最適化された、安全・高速で標準に準拠し、とても柔軟なウェブサーバーです。他のウェブサーバーと比べてメモリ使用量や CPU 負担が少ないのが特徴です。高度な機能セット (FastCGI, CGI, 認証, 圧縮出力, URL の書き換えなど) により、lighttpd は負担が気になるサーバーにうってつけのウェブサーバーソフトウェアとして君臨します。

インストール

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

lighttpd.service起動有効化してサーバーを起動してください。

ブラウザで localhost を開いて見てください、テストページが表示されるはずです。

設定ファイルのサンプルは /usr/share/doc/lighttpd/ にあります。

ログ出力

lighttpd はエラー・アクセスログをファイルに書き出すことができます。ログオプションを有効にするには /etc/lighttpd/lighttpd.conf を以下のように編集:

server.modules += (
   "mod_access",
   "mod_accesslog",
)

server.errorlog   = "/var/log/lighttpd/error.log"
accesslog.filename = "/var/log/lighttpd/access.log"

SSL による HTTPS の有効化

警告: SSL/TLS を使用する場合、OpenSSL の記事を読んでください。
ヒント:
  • Mozilla は lighttpd で使える SSL 設定ジェネレータ を提供しています。
  • SSL の設定後、Qualys SSL Labs の SSL Server Check を使うことで設定をチェックできます。
自己署名証明書

自己署名 SSL 証明書は以下のように作成できます (openssl のインストールが必要です):

# mkdir /etc/lighttpd/certs
# openssl req -x509 -nodes -days 7300 -newkey rsa:2048 -sha256 -keyout /etc/lighttpd/certs/server.pem -out /etc/lighttpd/certs/server.pem
# chmod 600 /etc/lighttpd/certs/server.pem

/etc/lighttpd/lighttpd.conf を編集して以下の行を追加すれば https が有効になります:

$SERVER["socket"] == ":443" {
    ssl.engine                  = "enable" 
    ssl.pemfile                 = "/etc/lighttpd/certs/server.pem" 
 }
Let's Encrypt

Let's Encrypt によって署名された証明書を使うこともできます。手動で証明書を作成した後、生成された privkey.pemfullchain.pem をひとつのファイルに結合してください:

# cat /etc/letsencrypt/live/domain/{privkey.pem,fullchain.pem} > /etc/letsencrypt/live/domain/combined.pem

/etc/lighttpd/lighttpd.conf を編集して以下の行を追加します:

$SERVER["socket"] == ":443" {
    ssl.engine                  = "enable" 
    ssl.pemfile                 = "/etc/letsencrypt/live/domain/combined.pem"
    ssl.ca-file                 = "/etc/letsencrypt/live/domain/fullchain.pem"
}

curl で接続したときに "empty reply from server" と返される場合、上記の設定に以下を追加してください:

ssl.openssl.ssl-conf-cmd = ("Protocol" => "-ALL, TLSv1.2")

また、上記の設定で Firefox を使って HTTPS でサイトをロードできない場合も解決することがあります。

ディレクトリのパスワード保護

libmariadbclient[リンク切れ: 置換パッケージ: mariadb-libs] をインストールする必要があります。lighttpd の passwd ファイルはシステムがユーザー認証に使用する /etc/passwd と同じです。以下のように簡単にエントリを作成することができます:

$ user=foo
$ password=b@R102
$ realm='Password Required'
$ hash=`echo -n "$user:$realm:$password" | md5sum | cut -b -32`

# echo "$user:$realm:$hash" >> /etc/lighttpd/lighttpd.user

/etc/lighttpd/lighttpd.conf を編集して以下の行を追加することでディレクトリの保護が有効になります:

server.modules = (
   "mod_auth",
 )

auth.debug = 2
auth.backend                = "htdigest"
auth.backend.htdigest.userfile = "/etc/lighttpd/lighttpd.user"

# note this entry is relative to the server.document-root
auth.require = ( "/secret" =>
   (
    "method" => "basic",
    "realm" => "Password Required",
    "require" => "user=foo"
   )
)
ノート: /etc/lighttpd/lighttpd.conf の realm と user は /etc/lighttpd/lighttpd.user の値と一致していないと認証が機能しません。

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 などを使いたい場合は以下を読んで下さい。

ノート: 新しいデフォルトユーザーとグループ: lighttpd は nobody グループではなくデフォルトで http ユーザー/グループで動作するようになっています。

まず /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

php-cgi を使う

phpphp-cgi をインストールします (PHPLAMP を参照)。

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"
ノート: モジュールがロードされる順番は重要です。正しい順番は /usr/share/doc/lighttpd/config/modules.conf に記載されています。設定を誤ると lighttpd がクラッシュする可能性があります。

lighttpd をリロード:

# systemctl reload lighttpd
ノート:
  • php ファイルにアクセスしたときに No input file found などのエラーが表示される場合、複数の原因が考えられます。詳しくは この FAQ を見て下さい。
  • 他のモジュール (例: mod_cgi) が .php 拡張子を処理するようになっていないか確認してください。
php-fpm を使う

最近の lighttpd のリリースでは適応型のプロセス生成はなくなっています。PHP プロセスの動的な管理がしたい場合は、php-fpm をインストールしてください。そして php-fpm.service起動有効化してください。

ノート: /etc/php/php-fpm.conf ファイルを編集することでプールのサーバーの数やその他の設定オプションを変更できます。php-fpm の詳細は php-fpm のウェブサイト を見て下さい。/etc/php/php.ini に変更を加えた場合、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"
      ))
)

Python FastCGI

FastCGI をインストール・設定してください (上の #FastCGI を参照)。

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()

詳しくは こちらの投稿 を参照。

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/certs/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"

圧縮したいコンテンツのタイプを選択することもできます。/etc/lighttpd/conf.d/compress.confcompress.filetype パラメータを編集してください:

compress.filetype           = ("text/plain", "text/html", "text/javascript", "text/css", "text/xml")

参照