🪶

【Lighttpd】conda-forge版でLighttpdに入門してみる【Pixi】

に公開

手探りでLighttpdを始める

https://www.lighttpd.net/

Zennには一つとして記事がないらしいが(あった)、Lighttpd(Lighty)というWebサーバーがある。更にconda-forgeにも登録されているため、仮想環境を使って簡単に導入、破棄することができる。

https://anaconda.org/conda-forge/lighttpd

低負荷高速安全柔軟などと謳われているものの、Lighttpdに関しする情報が圧倒的に少ないため、同じくconda-forgeから導入できるNginxを差し置いて使うべき場面は少ないと考えられる。
にも拘わらずLighttpdを試すのには興味以外にも理由があるが、主旨とは無関係になるため後述する。

0. Pixi導入

https://prefix.dev/channels/conda-forge/packages/lighttpd

conda-forgeCondaの擅にする攸ではなく、Pixiでも使用できる。Pixiにはタスクという機能があるため、これを使って長いコマンドを省略する。

curl -fsSL https://pixi.sh/install.sh | sh
# あるいは
wget -qO- https://pixi.sh/install.sh | sh

1. Lighttpd導入

ワークスペースを作り、この中にLighttpdを導入する。

ワークスペース作成

# ワークスペース作成
$ pixi init lighty_sample
✔ Created /⋯/lighty_sample/pixi.toml
# 移動
$ cd lighty_sample/

インストール

PixiLighttpdを導入する。

$ pixi add lighttpd
✔ Added lighttpd >=1.4.73,<2

仮想環境

Pixiで何らかのパッケージを導入した時点で、.pixi/env配下に仮想環境defaultが自動的に作られる。

$ ls .pixi/envs/
default
$ ls .pixi/envs/default/
bin  conda-meta  etc  include  lib  libexec  man  sbin  share  ssl

実環境と同様etcフォルダーが存在するが、その中にlighttpdというフォルダーは存在しないことに注意。

$ ls .pixi/envs/default/etc/
conda  openldap  pkcs11  request-key.conf

2. 設定

こちらの記事を見ると、通常は/etc/lighttpd/lighttpd.confという設定ファイルがあるらしい。

https://freelance.shiftinc.jp/column/lighttpd/

しかし上述の通り、仮想環境下に/etc/lighttpd/は存在しないため、そこから想像つくようにlighttpd.confも存在しないようである。

$ find .pixi/envs/default/ -name '*.conf'
.pixi/envs/default/share/examples/krb5/kdc.conf
.pixi/envs/default/share/examples/krb5/krb5.conf
.pixi/envs/default/etc/request-key.conf
.pixi/envs/default/etc/openldap/ldap.conf

設定ファイルを作る

ないものを嘆いても仕方ないのだから、自分で作らなければならない。

整理のため、lighttpdフォルダーを作っておく。この中にlighttpd.confを次の内容で作る。

lighttpd.conf
server.modules = ( "mod_staticfile" )
server.document-root = "/⋯/lighty_sample/var/www/"
server.port = 8080

先の記事を真似して、var/www/フォルダーにindex.htmlを配置することとした。ここでは/⋯/lighty_sample/var/www/抔と、で一部省略しているが、実際には完全な絶対パスを記載しなければならない点に注意。

3. 動作確認

設定ファイルを作ったら、Lighttpdを起動する。なお本記事ではバックグラウンドで動作させない。

index.html

適当に作ってもよいが、今回はGitHubにあるものを流用する。

https://github.com/lighttpd/lighttpd1.4/blob/master/tests/docroot/index.html

起動方法

lighttpdコマンドでWebサーバーが起動するが、バックグラウンドで起動されると停止が手間になるため、オプション-Dを付する。また設定ファイルを-fで指定する。その他オプションは次の通り。

$ pixi run lighttpd --help
/⋯/lighty_sample/.pixi/envs/default/bin/lighttpd: invalid option -- '-'
lighttpd/1.4.73 (ssl) - a light and fast webserver
usage:
 -f <name>  filename of the config-file
 -m <name>  module directory (default: /⋯/lighty_sample/.pixi/envs/default/lib)
 -i <secs>  graceful shutdown after <secs> of inactivity
 -1         process single (one) request on stdin socket, then exit
 -p         print the parsed config-file in internal form, and exit
 -t         test config-file syntax, then exit
 -tt        test config-file syntax, load and init modules, then exit
 -D         don't go to background (default: go to background)
 -v         show version
 -V         show compile-time features
 -h         show this help

ここでは、次のコマンドで実行することとなる。

lighttpd -D -f lighttpd/lighttpd.conf

タスクの使用(Pixi)

以上の話を全てタスクに落とし込むと、次のように書ける。

pixi.toml
[workspace]
channels = ["conda-forge"]
name = "lighty_sample"
platforms = ["linux-64"]
version = "0.1.0"

[tasks]
_mkdir_tmp  = "mkdir -p .pixi/envs/default/tmp"
_mkdir_var_www = "mkdir -p var/www"
start = { cmd = "lighttpd -D -f lighttpd/lighttpd.conf", depends-on = ["_mkdir_tmp", "_install_default_html"] }
test = "lighttpd -t"

[tasks._install_default_html]
cmd = "wget https://raw.githubusercontent.com/lighttpd/lighttpd1.4/refs/heads/master/tests/docroot/index.html"
cwd = "var/www"
depends-on = ["_mkdir_var_www"]
outputs = ["index.html"]

[dependencies]
lighttpd = ">=1.4.73,<2"

この設定によって、全ての作業を次のコマンド一つに任せることができた。

$ pixi run start

http://127.0.0.1:8080/index.htmlにアクセスすれば、テストページが表示される。しかし、画像など不足しているようだった。

テストページ

LighttpdNginxも、別途パッケージを用意することでPHPを用いることができる。しかしNginxの場合に必要なPHP-FPMconda-forgeから入手することができないようだった。一方でLighttpdの場合に必要なのはfcgiであり、こちらはconda-forgeから入手できる。この違いから、敢えてLighttpdを使ってみた次第であった。

Discussion