Current Location: Home> Latest Articles> Complete Guide to Deploying PHP on Lighttpd: A Lightweight Web Server Setup

Complete Guide to Deploying PHP on Lighttpd: A Lightweight Web Server Setup

gitbox 2025-07-18

Benefits of Running PHP on Lighttpd

Lighttpd is a high-performance, low-memory-footprint web server designed for speed and efficiency. When paired with PHP, a widely used server-side scripting language, you can build scalable and fast web applications—ideal for embedded devices, small websites, or performance-focused deployments.

Environment Preparation

Before configuration, ensure both Lighttpd and PHP are installed on your Linux server. Follow these installation commands:

Install Lighttpd

sudo apt-get update
sudo apt-get install lighttpd

Install PHP and Common Extensions

sudo apt-get install php php-cgi php-mysql

Configure Lighttpd to Support PHP

By default, Lighttpd doesn't parse PHP files. Manual configuration is required to enable PHP support.

Enable CGI Module

Edit the Lighttpd config file:

sudo nano /etc/lighttpd/lighttpd.conf

Locate and modify the server.modules section to include:

server.modules = (
  "mod_access",
  "mod_alias",
  "mod_cgi"
)

Set Up PHP FastCGI Handler

In the same config file, add the following PHP handler configuration:

server.modules += ( "mod_fastcgi" )

mimetype.assign = (
  ".php" => "application/x-httpd-php"
)

fastcgi.server = (
  ".php" => ((
    "socket" => "/var/run/php/php7.4-fpm.sock",
    "bin-path" => "/usr/bin/php-cgi",
    "check-local" => "disable",
    "max-procs" => 1,
    "bin-environment" => (
      "PHPRC" => "/etc/php/7.4/cli/php.ini"
    )
  ))
)

Restart Lighttpd Service

To apply changes, restart Lighttpd:

sudo service lighttpd restart

Test PHP Functionality

Create a PHP test file named info.php:

<?php phpinfo(); ?>

Place this file in your web root (typically /var/www/html) and visit http://your_server_ip/info.php in a browser. If configured correctly, you will see the PHP information page.

Common Issues and Solutions

PHP Code Displayed Instead of Executed

If the browser displays PHP source code instead of executing it, this usually means PHP isn't being parsed correctly. Double-check that mod_fastcgi is enabled and PHP is correctly configured.

500 Internal Server Error

This may indicate a PHP configuration error or permission issue. Check Lighttpd's error log at /var/log/lighttpd/error.log for detailed information.

Conclusion

With this setup, you can successfully run PHP on a Lighttpd server. This combination is fast, lightweight, and ideal for small to medium-scale projects. Proper configuration ensures high performance and a responsive web environment tailored to your deployment needs.