Skip to content

Instantly share code, notes, and snippets.

@jfstenuit
Created December 30, 2024 09:34
Show Gist options
  • Save jfstenuit/dfad3a70f0dac71f7e16bc2ceffba8f3 to your computer and use it in GitHub Desktop.
Save jfstenuit/dfad3a70f0dac71f7e16bc2ceffba8f3 to your computer and use it in GitHub Desktop.
Express install Lighttpd and PHP under Ubuntu 24.04

Lighttpd with PHP-FPM and HTTPS Setup Commands

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.

Installation Commands

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

Generate Self-Signed Certificate

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

Verify PHP-FPM Socket

grep '^listen = ' /etc/php/8.3/fpm/pool.d/www.conf

Update Configuration Files

Edit /etc/lighttpd/conf-available/15-fastcgi-php.conf

Locate the line that starts with "socket" => and update with the value from above:

"socket" => "/run/php/php8.3-fpm.sock",

Edit /etc/lighttpd/conf-available/10-ssl.conf

Locate the line that starts with ssl.pemfile = and update it as follows:

ssl.pemfile = "/etc/lighttpd/certs/lighttpd.pem"

Create /etc/lighttpd/conf-available/99-redirect.conf

Add the following content:

$HTTP["scheme"] == "http" {
    $HTTP["host"] =~ ".*" {
        url.redirect = ("" => "https://%0$0")
    }
}

Create a PHP Info File

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

Enable HTTP to HTTPS Redirection

Create a symbolic link to enable the redirect configuration:

cd /etc/lighttpd/conf-enabled/
ln -s ../conf-available/99-redirect.conf

Restart Lighttpd

systemctl restart lighttpd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment