Lighttpd is a secure, fast, standards-compliant web server designed for speed-critical environments. This tutorial shows how you can install Lighttpd on a Debian 8 (Jessie) server with PHP5 support (through PHP-FPM) and MySQL support. PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features useful for sites of any size, especially busier sites. I use PHP-FPM in this tutorial instead of Lighttpd's spawn-fcgi.

1 Preliminary Note

In this tutorial, I use the hostname server1.example.com with the IP address 192.168.1.100. These settings might differ for you, so you have to replace them where appropriate. Use a Debian minimal server as the basis for this installation, this can either be a minimal image of your datacenter or you install one from scratch by using our Debian minimal server tutorial.

2 Installing MySQL / MariaDB

Debian 8 ships with 2 MySQL compatible databases, the traditional MySQL database and MariaDB, a MySQL fork maintained by the original MySQL inventor Monty Widenius. MariaDB is leading in regard of database speed and development activity at the moment, so I will choose it for my server.

Below I will describe the installation of MariaDB (Chapter 2.1) and MySQL (Chapter 2.2) so you can choose the database that you prefer. Just ensure that you either follow chapter 2.1 or 2.2 but not both.

2.1 Install MariaDB

To install MariaDB, run this command.

apt-get install mariadb-server mariadb-client

You will be asked to provide a password for the MariaDB root user - this password is valid for the user [email protected] as well as [email protected], so we don't have to specify a MariaDB root password manually later on:

2.2 Install MySQL

We install MySQL 5 like this:

apt-get install mysql-server mysql-client

You will be asked to provide a password for the MySQL root user - this password is valid for the user [email protected] as well as [email protected], so we don't have to specify a MySQL root password manually later on:

New password for the MySQL "root" user:<-- yourrootsqlpasswordRepeat password for the MySQL "root" user:<-- yourrootsqlpassword

3 Installing Lighttpd

Lighttpd is available as a Debian package, therefore we can install it like this:

apt-get install lighttpd

Now direct your browser to http://192.168.1.100/, and you should see the Lighttpd placeholder page:

Lighttpd's default document root is /var/www on Debian, and the configuration file is /etc/lighttpd/lighttpd.conf.

Additional configurations are stored in files in the /etc/lighttpd/conf-available directory - these configurations can be enabled with the lighttpd-enable-mod command which creates a symlink from the /etc/lighttpd/conf-enabled directory to the appropriate configuration file in /etc/lighttpd/conf-available. You can disable configurations with the lighttpd-disable-mod command.

4 Installing PHP5

We can make PHP5 work in Lighttpd through PHP-FPM which we install like this:

apt-get install php5-fpm php5

PHP-FPM is a daemon process (with the init script /etc/init.d/php5-fpm) that runs a FastCGI server on the socket /var/run/php5-fpm.sock.

5 Configuring Lighttpd and PHP5

To enable PHP5 in Lighttpd, we must modify /etc/php5/fpm/php.ini and uncomment the line cgi.fix_pathinfo=1:

nano /etc/php5/fpm/php.ini

[...]
; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI. PHP's
; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok
; what PATH_INFO is. For more information on PATH_INFO, see the cgi specs. Setting
; this to 1 will cause PHP CGI to fix its paths to conform to the spec. A setting
; of zero causes PHP to behave as before. Default is 1. You should fix your scripts
; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
; http://php.net/cgi.fix-pathinfo
cgi.fix_pathinfo=1
[...]

And restart the php5-fpm service to apply the configuration change:

systemctl restart php5-fpm.service

The Lighttpd configuration file for PHP /etc/lighttpd/conf-available/15-fastcgi-php.conf is suitable for use with spawn-fcgi, however, we want to use PHP-FPM, therefore we create a backup of the file (named 15-fastcgi-php-spawnfcgi.conf) and modify 15-fastcgi-php.conf as follows:

This creates the symlinks /etc/lighttpd/conf-enabled/10-fastcgi.conf which points to /etc/lighttpd/conf-available/10-fastcgi.conf and /etc/lighttpd/conf-enabled/15-fastcgi-php.conf which points to /etc/lighttpd/conf-available/15-fastcgi-php.conf:

6 Testing PHP5 / Getting Details About Your PHP5 Installation

The document root of the default website is /var/www. We will now create a small PHP file (info.php) in that directory and call it in a browser. The file will display lots of useful details about our PHP installation, such as the installed PHP version.

nano /var/www/html/info.php

<?php
phpinfo();
?>

Now we call that file in a browser (e.g. http://192.168.1.100/info.php):

As you see, PHP5 is working, and it's working through FPM/FastCGI, as shown in the Server API line. If you scroll further down, you will see all modules that are already enabled in PHP5. MySQL is not listed there which means we don't have MySQL support in PHP5 yet.

7 Getting MySQL Support in PHP5

To get MySQL support in PHP, we can install the php5-mysqlnd package. That's a new database driver that is compatible with MySQL and MariaDB. It's a good idea to install some other PHP5 modules as well as you might need them for your applications. You can search for available PHP5 modules like this:

Xcache is a free and open PHP opcode cacher for caching and optimizing PHP intermediate code. It's similar to other PHP opcode cachers, such as eAccelerator and APC. It is strongly recommended to have one of these installed to speed up your PHP page.

Xcache can be installed as follows:

apt-get install php5-xcache

Now reload PHP-FPM:

systemctl restart php5-fpm.service

Now reload http://192.168.1.100/info.php in your browser and scroll down to the modules section again. You should now find lots of new modules there, including the MySQL module:

8 phpMyAdmin

phpMyAdmin is a web interface through which you can manage your MySQL databases. It's a good idea to install it:

10 Links

Over 20 years experience as Software Developer and Linux System Administrator. Till Brehm is the founder and lead developer of the ISPConfig Hosting Control Panel software (since 2000) and he founded HowtoForge in 2005 as a place to share Linux knowledge with other Linux enthusiasts.