How to Install Lighttpd with PHP-FPM on Ubuntu 26.04 LTS
Set up Lighttpd with PHP-FPM on Ubuntu 26.04 LTS: install the packages, enable fastcgi-php-fpm, add virtual hosts and test. Includes Ubuntu 24.04 differences.
Lighttpd is a small, fast web server that suits VPS plans with little memory. This guide installs Lighttpd from the Ubuntu repositories, connects it to PHP-FPM, and sets up virtual hosts for several domains or subdomains. It is written for Ubuntu 26.04 LTS, which ships Lighttpd 1.4.82 and PHP 8.5; the steps are the same on Ubuntu 24.04 LTS (Lighttpd 1.4.74 and PHP 8.3) apart from the PHP version number in file and service names.
Older tutorials install php5-cgi and let Lighttpd start PHP processes itself. PHP 5 has been unsupported for years and the package no longer exists; the maintained setup is a separate PHP-FPM service that Lighttpd reaches through a Unix socket.
Requirements
- A server running Ubuntu 26.04 LTS or 24.04 LTS.
- A user with
sudorights (this guide usesdeploy). - No other web server listening on port 80. If Apache or Nginx is installed, stop and disable it first.
- For virtual hosts: DNS records for each name pointing to the server.
Step 1: Install Lighttpd and PHP-FPM
Refresh the package lists and install both packages:
sudo apt update
sudo apt install lighttpd php-fpm
php-fpm is a small package that depends on the release's default PHP version: php8.5-fpm on Ubuntu 26.04 and php8.3-fpm on 24.04. Add the extensions your application needs in the same way, for example:
sudo apt install php-mysql php-curl php-mbstring php-xml
Both services start on installation. Confirm that they are running:
systemctl status lighttpd php8.5-fpm --no-pager
On Ubuntu 24.04 the second unit is php8.3-fpm. If you use UFW, allow web traffic:
sudo ufw allow 80/tcp
Opening http://203.0.113.10/ (your server's address) in a browser now shows the Lighttpd placeholder page from /var/www/html.
Step 2: Connect Lighttpd to PHP-FPM
The Ubuntu package keeps optional configuration snippets in /etc/lighttpd/conf-available/ and activates them with lighty-enable-mod, which links them into /etc/lighttpd/conf-enabled/. Two snippets are needed: fastcgi loads the FastCGI module and fastcgi-php-fpm sends .php requests to the PHP-FPM socket.
sudo lighty-enable-mod fastcgi fastcgi-php-fpm
sudo systemctl restart lighttpd
Do not enable fastcgi-php as well. That older snippet runs PHP through php-cgi and conflicts with fastcgi-php-fpm.
The enabled snippet, /etc/lighttpd/conf-enabled/15-fastcgi-php-fpm.conf, contains this:
fastcgi.server += ( ".php" =>
((
"socket" => "/run/php/php-fpm.sock",
"broken-scriptfilename" => "enable"
))
)
/run/php/php-fpm.sock is a version-independent name that points to the socket of the installed PHP-FPM version, so the snippet needs no editing after a PHP upgrade. Check that it exists:
ls -l /run/php/
If only a versioned socket such as php8.5-fpm.sock is listed, put that path in the snippet instead and restart Lighttpd.
Step 3: Test PHP
Create a test script in the default document root:
echo '<?php phpinfo();' | sudo tee /var/www/html/info.php
Request it and look for the PHP version in the output:
curl -s http://localhost/info.php | grep -i "php version"
The page should also report FPM/FastCGI as the Server API. Delete the file afterwards, because it shows your whole configuration to anyone who finds it:
sudo rm /var/www/html/info.php
Step 4: Add virtual hosts
The main configuration file is /etc/lighttpd/lighttpd.conf. It sets the default document root (/var/www/html), runs the server as www-data, and includes every file in /etc/lighttpd/conf-enabled/. Rather than editing the main file, keep your sites in a snippet of their own.
Create a directory per site and make your user the owner:
sudo mkdir -p /var/www/example.com/public /var/www/blog.example.com/public
sudo chown -R deploy:deploy /var/www/example.com /var/www/blog.example.com
Create the snippet:
sudo nano /etc/lighttpd/conf-available/50-vhosts.conf
Add one conditional block per site. Settings inside a block apply only to requests whose Host header matches:
$HTTP["host"] =~ "^(www\.)?example\.com$" {
server.document-root = "/var/www/example.com/public"
}
$HTTP["host"] == "blog.example.com" {
server.document-root = "/var/www/blog.example.com/public"
}
== compares the host name exactly; =~ matches a regular expression, in which dots must be escaped as \. and the pattern should be anchored with ^ and $. Requests for any other host name fall through to the default document root.
Link the snippet into conf-enabled:
sudo ln -s ../conf-available/50-vhosts.conf /etc/lighttpd/conf-enabled/50-vhosts.conf
PHP works in every virtual host without further settings, because the FastCGI snippet applies to the whole server. The files only need to be readable by www-data, the account both Lighttpd and the default PHP-FPM pool run as; directories the application writes to (uploads, caches) must be writable by it:
sudo mkdir -p /var/www/example.com/public/uploads
sudo chown www-data:www-data /var/www/example.com/public/uploads
Step 5: Check the configuration and restart
Test the configuration before every restart. The -tt option checks the syntax and also loads and initialises the modules:
sudo lighttpd -tt -f /etc/lighttpd/lighttpd.conf
If no error is reported, restart the service:
sudo systemctl restart lighttpd
Verify each site from the server itself by sending the host name explicitly:
echo '<?php echo "blog ok\n";' > /var/www/blog.example.com/public/index.php
curl -H "Host: blog.example.com" http://localhost/
Where things are
| Item | Ubuntu 26.04 | Ubuntu 24.04 |
|---|---|---|
| Lighttpd main configuration | /etc/lighttpd/lighttpd.conf | same |
| Lighttpd error log | /var/log/lighttpd/error.log | same |
| PHP-FPM service | php8.5-fpm | php8.3-fpm |
| PHP-FPM pool | /etc/php/8.5/fpm/pool.d/www.conf | /etc/php/8.3/fpm/pool.d/www.conf |
| PHP settings for FPM | /etc/php/8.5/fpm/php.ini | /etc/php/8.3/fpm/php.ini |
After changing php.ini or the pool file, restart PHP-FPM, not Lighttpd:
sudo systemctl restart php8.5-fpm
Troubleshooting
- 503 Service Unavailable on PHP pages. Lighttpd cannot reach PHP-FPM. Check that the service is running and that the socket path in the snippet exists; the reason is logged in
/var/log/lighttpd/error.log. - 403 Forbidden on a PHP file. The FastCGI snippets are not enabled, so Lighttpd refuses to send the source code as a static file. Run the
lighty-enable-modcommand from step 2 again and restart. - Lighttpd does not start. Run the
-tttest; it prints the file and line of a syntax error. If the port is taken,sudo ss -tlnp | grep ':80'shows which program holds it. - The wrong site is shown. The host name does not match any block. Remember that
==is an exact comparison, sowww.example.comdoes not match"example.com".
Frequently asked questions
What is the difference between fastcgi-php and fastcgi-php-fpm?
With fastcgi-php, Lighttpd starts and supervises php-cgi processes itself. With fastcgi-php-fpm, PHP runs as its own systemd service with its own process manager, logs and pool settings, and can be restarted or tuned without touching the web server. Only one of the two can be enabled.
Does Lighttpd read .htaccess files?
No. Rewrite rules, access restrictions and redirects that an application ships as .htaccess have to be translated into Lighttpd configuration, for example with url.rewrite-if-not-exists from mod_rewrite for front-controller applications.
Are directory listings enabled?
No. A directory without an index file returns an error unless you turn listings on, globally or inside a single host block, with dir-listing.activate = "enable". The index file names Lighttpd looks for are index.php and index.html.
How do I run a newer PHP version than the release provides?
The Ubuntu archive carries one PHP version per release and supports it for the life of that release. A different version has to come from a third-party repository, which you then have to trust and keep updated yourself; for most sites the packaged version is the safer choice.