This gist provides all the commands needed to install and configure Lighttpd with PHP-FPM, enable HTTPS with a self-signed certificate, and redirect HTTP traffic to HTTPS.
apt install lighttpd php-cgi php-fpm
lighty-enable-mod fastcgi
lighty-enable-mod fastcgi-php
lighty-enable-mod ssl
lighty-enable-mod redirect
service lighttpd force-reload
mkdir -p /etc/lighttpd/certs
openssl req -x509 -newkey rsa:2048 -keyout lighttpd.key -out lighttpd.crt -days 365 -nodes
cat lighttpd.key lighttpd.crt >/etc/lighttpd/certs/lighttpd.pem
mv lighttpd.key lighttpd.crt /etc/lighttpd/certs/
chmod 600 /etc/lighttpd/certs/lighttpd.pem
grep '^listen = ' /etc/php/8.3/fpm/pool.d/www.conf
Locate the line that starts with "socket" =>
and update with the value from above:
"socket" => "/run/php/php8.3-fpm.sock",
Locate the line that starts with ssl.pemfile =
and update it as follows:
ssl.pemfile = "/etc/lighttpd/certs/lighttpd.pem"
Add the following content:
$HTTP["scheme"] == "http" {
$HTTP["host"] =~ ".*" {
url.redirect = ("" => "https://%0$0")
}
}
This will allow to test for the good working of the solution. You can delete the file afterwards.
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
chmod 644 /var/www/html/info.php
Create a symbolic link to enable the redirect configuration:
cd /etc/lighttpd/conf-enabled/
ln -s ../conf-available/99-redirect.conf
systemctl restart lighttpd