Debian 11にPHPと無料のLet's Encrypt SSLを使用してLighttpdをインストールする方法
Debian 11にPHPと無料のLet's Encrypt SSLを使用してLighttpdをインストールする方法
このチュートリアルはこれらのOSバージョンのために存在します
- Debian 11 (Bullseye)
- Debian 8 (ジェシー)
- Debian 7 (Wheezy)
- Debian 6 (スクイーズ)
- Debian 5 (Lenny)
- Debian 4 (Etch)
このページで
- 前提条件
- Lighttpdをインストールする
- PHPとPHP-FPMをインストールする
- PHP-FPMのためにLighttpdを設定する
- Lighttpdの仮想ホストを作成する
- Let's EncryptでLighttpdを安全にする
- 結論
Lighttpdはシンプルで高速、かつ安全なWebサーバーです。サイズが非常に小さく、メモリやCPUの使用量も少ないため、どのアプリケーションをホストするにも最適なサーバーの一つです。ミッションクリティカルな環境向けに設計されています。一台のサーバーで最大10,000の接続を同時に処理できます。URLリライト、出力圧縮、イベントメカニズム、FastCGI、SCGI、認証など、多くの機能を提供しています。
このチュートリアルでは、Debian 11にLighttpdをPHPとLet's Encrypt SSLでインストールする方法を紹介します。
前提条件
- Debian 11を実行しているサーバー。
- サーバーIPにポイントされた有効なドメイン名。
- サーバーにはルートパスワードが設定されています。
Lighttpdをインストールする
デフォルトでは、LighttpdパッケージはDebian 11の公式リポジトリに含まれています。次のコマンドを実行することでインストールできます:
apt-get install lighttpd -yLighttpdがインストールされたら、Lighttpdサービスを起動し、システム再起動時に自動的に起動するように設定します。
systemctl start lighttpd
systemctl enable lighttpd次のコマンドでLighttpdのステータスも確認できます:
systemctl status lighttpd
次の出力が得られます:
? lighttpd.service - Lighttpd Daemon
Loaded: loaded (/lib/systemd/system/lighttpd.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2022-02-12 07:01:06 UTC; 12s ago
Process: 4663 ExecStartPre=/usr/sbin/lighttpd -tt -f /etc/lighttpd/lighttpd.conf (code=exited, status=0/SUCCESS)
Main PID: 4668 (lighttpd)
Tasks: 1 (limit: 2341)
Memory: 932.0K
CPU: 226ms
CGroup: /system.slice/lighttpd.service
??4668 /usr/sbin/lighttpd -D -f /etc/lighttpd/lighttpd.conf
Feb 12 07:01:06 debian11 systemd[1]: Starting Lighttpd Daemon...
Feb 12 07:01:06 debian11 systemd[1]: Started Lighttpd Daemon.
今、ウェブブラウザを開き、URL http://your-server-ip を使用してLighttpdのウェブページにアクセスしてください。次の画面にLighttpdのテストページが表示されるはずです:
完了したら、次のステップに進むことができます。
PHPとPHP-FPMをインストールする
次に、次のコマンドを実行して、PHPおよびPHP-FPMパッケージをシステムにインストールします。
apt-get install php php-cgi php-fpm php-mysql -yインストール後、php.ini ファイルを編集し、cgi.fix_pathinfo を 1 に設定してください。
nano /etc/php/7.4/fpm/php.ini次の行を変更してください:
cgi.fix_pathinfo=1
作業が完了したら、ファイルを保存して閉じてください。
LighttpdをPHP-FPMと連携させるには、デフォルトのPHP-CGI設定とPHP-FPMソケットを置き換える必要があります。
まず、PHP-FPMの設定ファイルを編集します。
nano /etc/php/7.4/fpm/pool.d/www.conf
次の行を見つけてください:
listen = /run/php/php7.4-fpm.sock
そして次の行に置き換えます:
listen = 127.0.0.1:9000
ファイルを保存して閉じたら、変更を適用するためにPHP-FPMを再起動してください:
systemctl restart php7.4-fpm次のコマンドを使用してPHP-FPMのステータスを確認することもできます。
systemctl status php7.4-fpm次の出力が得られます:
? php7.4-fpm.service - The PHP 7.4 FastCGI Process Manager
Loaded: loaded (/lib/systemd/system/php7.4-fpm.service; enabled; vendor preset: enabled)
Active: active (running) since Sat 2022-02-12 07:04:35 UTC; 1min 7s ago
Docs: man:php-fpm7.4(8)
Process: 15141 ExecStartPost=/usr/lib/php/php-fpm-socket-helper install /run/php/php-fpm.sock /etc/php/7.4/fpm/pool.d/www.conf 74 (code=e>
Main PID: 15138 (php-fpm7.4)
Status: "Processes active: 0, idle: 2, Requests: 0, slow: 0, Traffic: 0req/sec"
Tasks: 3 (limit: 2341)
Memory: 8.8M
CPU: 54ms
CGroup: /system.slice/php7.4-fpm.service
??15138 php-fpm: master process (/etc/php/7.4/fpm/php-fpm.conf)
??15139 php-fpm: pool www
??15140 php-fpm: pool www
Feb 12 07:04:35 debian11 systemd[1]: Starting The PHP 7.4 FastCGI Process Manager...
Feb 12 07:04:35 debian11 systemd[1]: Started The PHP 7.4 FastCGI Process Manager.
完了したら、次のステップに進むことができます。
PHP-FPMのためにLighttpdを設定する
次に、Lighttpdの設定ファイルを編集し、Fast CGIを使用して変更する必要があります。
nano /etc/lighttpd/conf-available/15-fastcgi-php.conf次の行を見つけてください:
"bin-path" => "/usr/bin/php-cgi",
"socket" => "/var/run/lighttpd/php.socket",
そして、次の行に置き換えました:
"host" => "127.0.0.1",
"port" => "9000",
ファイルを保存して閉じたら、次のコマンドを使用してFast CGIモジュールを有効にします:
lighty-enable-mod fastcgi
lighty-enable-mod fastcgi-php
最後に、変更を適用するためにLighttpdサービスを再起動します:
systemctl restart lighttpdLighttpdの仮想ホストを作成する
Lighttpdは、仮想ホスティングを使用して複数のウェブサイトをホストすることもできます。test.example.comという名前のウェブサイトをホストするための新しい仮想ホスト設定ファイルを作成しましょう。
nano /etc/lighttpd/conf-available/test.conf次の行を追加してください:
$HTTP["host"] == "test.example.com" {
server.document-root = "/var/www/html/"
server.errorlog = "/var/log/lighttpd/example.com-error.log"
}
ファイルを保存して閉じ、その後、以下のコマンドでバーチャルホストを有効にします:
ln -s /etc/lighttpd/conf-available/test.conf /etc/lighttpd/conf-enabled/次に、index.phpファイルを作成します:
nano /var/www/html/index.php次の行を追加してください:
<?php
phpinfo();
?>
ファイルを保存して閉じたら、次のコマンドで適切な権限と所有権を設定します:
chown -R www-data:www-data /var/www/html/
chmod -R 755 /var/www/html次に、変更を適用するためにLighttpdサービスを再起動します。
systemctl restart lighttpdさて、ウェブブラウザーを開いて、URL http://test.example.com を使用してウェブサイトを確認してください。次の画面にPHPテストページが表示されるはずです:
Let's EncryptでLighttpdを安全にする
Lighttpdは、Let's Encrypt SSLでウェブサイトを保護することもできます。これを行うには、まず次のコマンドでCertbotクライアントをインストールしてください。
apt-get install certbot -y次に、ウェブサイトのためにLet's Encrypt SSLをダウンロードするには、次のコマンドを実行してください。
certbot certonly --webroot -w /var/www/html/ -d test.example.comあなたは、以下に示すライセンス条項に同意し、メールアドレスを提供するよう求められます。
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator webroot, Installer None
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): [email
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y
Once the certificates are downloaded successfully, you should see the following output:
IMPORTANT NOTES:
- Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/test.example.com/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/test.example.com/privkey.pem
Your cert will expire on 2022-05-11. To obtain a new or tweaked
version of this certificate in the future, simply run certbot
again. To non-interactively renew *all* of your certificates, run
"certbot renew"
- If you like Certbot, please consider supporting our work by:
Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
Donating to EFF: https://eff.org/donate-le
次に、証明書と秘密鍵を1つのファイルに結合する必要があります。次のコマンドを使用して行うことができます:
cat /etc/letsencrypt/live/test.example.com/cert.pem /etc/letsencrypt/live/test.example.com/privkey.pem > /etc/letsencrypt/live/test.example.com/web.pem次に、Lighttpdの仮想ホストファイルを編集し、Let's Encrypt SSL証明書のパスを定義する必要があります。
次のコマンドで実行できます:
nano /etc/lighttpd/conf-enabled/test.conf以下のようにファイルを変更してください:
$HTTP["host"] == "test.example.com" {
server.document-root = "/var/www/html/"
}
$SERVER["socket"] == ":443" {
ssl.engine = "enable"
ssl.pemfile = "/etc/letsencrypt/live/test.example.com/web.pem"
ssl.ca-file = "/etc/letsencrypt/live/test.example.com/chain.pem"
server.name = "test.example.com"
server.document-root = "/var/www/html/"
server.errorlog = "/var/log/lighttpd/example.com_error.log"
accesslog.filename = "/var/log/lighttpd/example.com_access.log"
}
$HTTP["scheme"] == "http" {
$HTTP["host"] == "test.example.com" {
url.redirect = ("/.*" => "https://test.example.com$0")
}
}
ファイルを保存して閉じます。それから、設定変更を適用するためにLighttpdサービスを再起動します。
systemctl restart lighttpd今、URL https://test.example.com を使用して、ウェブサイトに安全にアクセスできます。
結論
おめでとうございます!Debian 11にLighttpdとPHP、Let's Encrypt SSLを正常にインストールしました。これでLighttpdウェブサーバーを使用してウェブサイトのデプロイを開始できます。質問があればお気軽にお尋ねください。

