「Lighttpd」の版間の差分
Kusakata.bot (トーク | 投稿記録) 細 文字列「[[zh-TW:」を「[[zh-hant:」に置換 |
Kusakata.bot2 (トーク | 投稿記録) Pkg/AUR テンプレートの更新 |
||
| (2人の利用者による、間の7版が非表示) | |||
| 1行目: | 1行目: | ||
{{Lowercase title}} |
|||
[[Category:ウェブサーバー]] |
[[Category:ウェブサーバー]] |
||
[[cs:Lighttpd]] |
[[cs:Lighttpd]] |
||
| 7行目: | 8行目: | ||
[[zh-hans:Lighttpd]] |
[[zh-hans:Lighttpd]] |
||
[[zh-hant:Lighttpd]] |
[[zh-hant:Lighttpd]] |
||
[ |
[https://www.lighttpd.net/ lighttpd] は高速性の重視される環境に最適化された、安全・高速で標準に準拠し、とても柔軟な[[Wikipedia:ja:Webサーバ|ウェブサーバー]]です。他のウェブサーバーと比べてメモリ使用量や CPU 負担が少ないのが特徴です。高度な機能セット ([[Wikipedia:ja:FastCGI|FastCGI]], [[Wikipedia:ja:Common Gateway Interface|CGI]], 認証, 圧縮出力, URL の書き換えなど) により、lighttpd は負担が気になるサーバーにうってつけのウェブサーバーソフトウェアとして君臨します。 |
||
==インストール== |
==インストール== |
||
{{pkg|lighttpd}} パッケージをインストールしてください。 |
|||
==設定== |
==設定== |
||
| 22行目: | 23行目: | ||
デフォルトの設定ファイルでは {{ic|/srv/http/}} をウェブ上に公開するドキュメントのディレクトリとして指定しています。 |
デフォルトの設定ファイルでは {{ic|/srv/http/}} をウェブ上に公開するドキュメントのディレクトリとして指定しています。 |
||
インストール |
インストールが正しく行われたかテストするには: |
||
# echo 'TestMe!' >> /srv/http/index.html |
# echo 'TestMe!' >> /srv/http/index.html |
||
# chmod 755 /srv/http/index.html |
# chmod 755 /srv/http/index.html |
||
サーバーを起動 |
{{ic|lighttpd.service}} を[[起動]]・[[有効化]]してサーバーを起動してください。 |
||
# systemctl start lighttpd |
|||
ブラウザで {{ic|localhost}} を開いて見て |
ブラウザで {{ic|localhost}} を開いて見てください、テストページが表示されるはずです。 |
||
設定ファイルのサンプルは {{ic|/usr/share/doc/lighttpd/}} にあります。 |
|||
ブート時にサーバーを起動させるには: |
|||
==== ログ出力 ==== |
|||
# systemctl enable lighttpd |
|||
lighttpd はエラー・アクセスログをファイルに書き出すことができます。ログオプションを有効にするには {{ic|/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 の有効化 ==== |
|||
設定ファイルのサンプルは {{ic|/usr/share/doc/lighttpd/}} にあります。 |
|||
{{Warning|SSL/TLS を使用する場合、[[OpenSSL]] の記事を読んでください。}} |
|||
{{Tip| |
|||
* Mozilla は lighttpd で使える SSL [https://mozilla.github.io/server-side-tls/ssl-config-generator/ 設定ジェネレータ] を提供しています。 |
|||
* SSL の設定後、Qualys SSL Labs の [https://www.ssllabs.com/ssltest/index.html SSL Server Check] を使うことで設定をチェックできます。}} |
|||
===== 自己署名証明書 ===== |
|||
自己署名 SSL 証明書は以下のように作成できます ({{Pkg|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 |
|||
{{ic|/etc/lighttpd/lighttpd.conf}} を編集して以下の行を追加すれば https が有効になります: |
|||
$SERVER["socket"] == ":443" { |
|||
ssl.engine = "enable" |
|||
ssl.pemfile = "/etc/lighttpd/certs/server.pem" |
|||
} |
|||
===== Let's Encrypt ===== |
|||
[[Let's Encrypt]] によって署名された証明書を使うこともできます。手動で証明書を作成した後、生成された {{ic|privkey.pem}} と {{ic|fullchain.pem}} をひとつのファイルに結合してください: |
|||
# cat /etc/letsencrypt/live/''domain''/{privkey.pem,fullchain.pem} > /etc/letsencrypt/live/''domain''/combined.pem |
|||
{{ic|/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 でサイトをロードできない場合も解決することがあります。 |
|||
==== ディレクトリのパスワード保護 ==== |
|||
{{Pkg|libmariadbclient}}{{Broken package link|置換パッケージ: {{Pkg|mariadb-libs}}}} をインストールする必要があります。lighttpd の passwd ファイルはシステムがユーザー認証に使用する {{ic|/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 |
|||
{{ic|/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" |
|||
) |
|||
) |
|||
{{Note|{{ic|/etc/lighttpd/lighttpd.conf}} の realm と user は {{ic|/etc/lighttpd/lighttpd.user}} の値と一致していないと認証が機能しません。}} |
|||
===CGI=== |
===CGI=== |
||
| 66行目: | 143行目: | ||
===FastCGI=== |
===FastCGI=== |
||
{{pkg|fcgi}} をインストールしてください。それで lighttpd に fcgi サポートが追加されます。fcgi サポートを追加するだけなら設定はそれだけです。Ruby on Rails, PHP, Python などを使いたい場合は以下を読んで下さい。 |
{{pkg|fcgi}} をインストールしてください。それで lighttpd に fcgi サポートが追加されます。fcgi サポートを追加するだけなら設定はそれだけです。[[Ruby on Rails]], PHP, Python などを使いたい場合は以下を読んで下さい。 |
||
{{Note|新しいデフォルトユーザーとグループ: lighttpd は {{ic|nobody}} グループではなくデフォルトで {{ic|http}} ユーザー/グループで動作するようになっています。}} |
{{Note|新しいデフォルトユーザーとグループ: lighttpd は {{ic|nobody}} グループではなくデフォルトで {{ic|http}} ユーザー/グループで動作するようになっています。}} |
||
| 89行目: | 166行目: | ||
そして {{ic|/etc/lighttpd/lighttpd.conf}} に以下を記述してください: |
そして {{ic|/etc/lighttpd/lighttpd.conf}} に以下を記述してください: |
||
include "conf.d/fastcgi.conf" |
include "conf.d/fastcgi.conf" |
||
PHP と Ruby on Rails については次のセクションを見て下さい。 |
|||
====PHP==== |
====PHP==== |
||
===== php-cgi を使う ===== |
|||
{{pkg|php}} と {{pkg|php-cgi}} をインストールします ([[PHP]] や [[LAMP]] を参照)。 |
{{pkg|php}} と {{pkg|php-cgi}} をインストールします ([[PHP]] や [[LAMP]] を参照)。 |
||
| 154行目: | 231行目: | ||
{{Note| |
{{Note| |
||
* php ファイルにアクセスしたときに ''No input file found'' などのエラーが表示される場合、複数の原因が考えられます。詳しくは [ |
* php ファイルにアクセスしたときに ''No input file found'' などのエラーが表示される場合、複数の原因が考えられます。詳しくは [https://redmine.lighttpd.net/projects/1/wiki/frequentlyaskedquestions#I-get-the-error-No-input-file-specified-when-trying-to-use-PHP この FAQ] を見て下さい。 |
||
* 他のモジュール (例: {{ic|mod_cgi}}) が ''.php'' 拡張子を処理するようになっていないか確認してください。 |
* 他のモジュール (例: {{ic|mod_cgi}}) が ''.php'' 拡張子を処理するようになっていないか確認してください。 |
||
}} |
}} |
||
| 160行目: | 237行目: | ||
===== php-fpm を使う ===== |
===== php-fpm を使う ===== |
||
最近の lighttpd のリリースでは適応型のプロセス生成はなくなっています。PHP プロセスの動的な管理がしたい場合は、{{Pkg|php-fpm}} を |
最近の lighttpd のリリースでは適応型のプロセス生成はなくなっています。PHP プロセスの動的な管理がしたい場合は、{{Pkg|php-fpm}} をインストールしてください。そして {{ic|php-fpm.service}} を[[起動]]・[[有効化]]してください。 |
||
# pacman -S php-fpm |
|||
# systemctl enable php-fpm |
|||
# systemctl start php-fpm |
|||
{{Note|{{ic|/etc/php/php-fpm.conf}} ファイルを編集することでプールのサーバーの数やその他の設定オプションを変更できます。''php-fpm'' の詳細は [http://php-fpm.org/ php-fpm のウェブサイト] を見て下さい。{{ic|/etc/php/php.ini}} に変更を加えた場合、php-fpm を再起動する必要があります。}} |
{{Note|{{ic|/etc/php/php-fpm.conf}} ファイルを編集することでプールのサーバーの数やその他の設定オプションを変更できます。''php-fpm'' の詳細は [http://php-fpm.org/ php-fpm のウェブサイト] を見て下さい。{{ic|/etc/php/php.ini}} に変更を加えた場合、php-fpm を再起動する必要があります。}} |
||
| 178行目: | 252行目: | ||
)) |
)) |
||
) |
) |
||
===== eAccelerator ===== |
|||
{{Note|2014年11月現在、AUR の {{AUR|eaccelerator}} はコンパイルすることができません。}} |
|||
[[Arch User Repository|AUR]] から {{AUR|eaccelerator}} をインストールします。 |
|||
eaccelerator の設定ファイルを追加: |
|||
{{hc|/etc/php/conf.d/eaccelerator-own.ini|2= |
|||
zlib.output_compression = On |
|||
cgi.fix_pathinfo=1 |
|||
eaccelerator.cache_dir="/home/phpuser/eaccelerator/cache" |
|||
}} |
|||
{{Tip|I additionally set {{ic|safe_mod}} to {{ic|On}} in my setup, but this is not required.}} |
|||
===== php ページのテスト ===== |
|||
以下の php ページを作成して、{{ic|index.php}} という名前をつけ、{{ic|/srv/http/}} と {{ic|/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]] から {{AUR|ruby-fcgi}} をインストールしてください。 |
|||
[[Ruby on Rails]] の指示に従って下さい。 |
|||
==== Python FastCGI ==== |
==== Python FastCGI ==== |
||
FastCGI をインストール・設定してください (上の [[#FastCGI]] を参照)。 |
FastCGI をインストール・設定してください (上の [[#FastCGI]] を参照)。 |
||
flup をインストール: |
{{Pkg|python2-flup}}{{Broken package link|パッケージが存在しません}} をインストールして以下のように設定: |
||
# pacman -S python2-flup |
|||
設定: |
|||
fastcgi.server = ( |
fastcgi.server = ( |
||
".py" => |
".py" => |
||
| 250行目: | 284行目: | ||
}} |
}} |
||
[https://bbs.archlinux.org/viewtopic.php?pid=734173#p734173 |
詳しくは [https://bbs.archlinux.org/viewtopic.php?pid=734173#p734173 こちらの投稿] を参照。 |
||
=== 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 |
|||
{{ic|/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 ===== |
===== Server Name Indication ===== |
||
| 307行目: | 313行目: | ||
$SERVER["socket"] == ":443" { |
$SERVER["socket"] == ":443" { |
||
ssl.engine = "enable" |
ssl.engine = "enable" |
||
ssl.pemfile = "/etc/lighttpd/ |
ssl.pemfile = "/etc/lighttpd/certs/server.pem" |
||
server.document-root = "..." |
server.document-root = "..." |
||
} |
} |
||
| 344行目: | 350行目: | ||
以下を {{ic|/etc/lighttpd/lighttpd.conf}} に追加: |
以下を {{ic|/etc/lighttpd/lighttpd.conf}} に追加: |
||
include "conf.d/compress.conf" |
include "conf.d/compress.conf" |
||
圧縮したいコンテンツのタイプを選択することもできます。{{ic|/etc/lighttpd/conf.d/compress.conf}} の {{ic|compress.filetype}} パラメータを編集してください: |
|||
{{Note| You can not do this (copy compress.conf) and add a needed content in {{ic|/etc/lighttpd/lighttpd.conf}} instead.}} |
|||
compress.filetype = ("text/plain", "text/html", "text/javascript", "text/css", "text/xml") |
|||
==トラブルシューティング== |
|||
=== Lighttpd で .php ファイルがダウンロードされてしまう === |
|||
{{ic|.php}} ファイルが実行されず lighttpd によってダウンロードが始まってしまう場合、おそらく以下の行を {{ic|/etc/lighttpd/lighttpd.conf}} に追加し忘れています: |
|||
{{bc|1= |
|||
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" |
|||
))) |
|||
}} |
|||
== 参照 == |
== 参照 == |
||
* [ |
* [https://redmine.lighttpd.net/projects/lighttpd/wiki Lighttpd wiki] |
||
2020年12月31日 (木) 14:45時点における最新版
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 の有効化
- 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.pem と fullchain.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 などを使いたい場合は以下を読んで下さい。
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 を使う
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"
/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.conf の compress.filetype パラメータを編集してください:
compress.filetype = ("text/plain", "text/html", "text/javascript", "text/css", "text/xml")