Installing Lighttpd with PHP5 (PHP-FPM) and MySQL on Debian 8 (Jessie)

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 with the IP address 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:

New password for the MariaDB "root" user: <-- yourrootsqlpassword
Repeat password for the MariaDB "root" user: <-- yourrootsqlpassword

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: <-- yourrootsqlpassword
Repeat 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, 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

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:

cd /etc/lighttpd/conf-available/
cp 15-fastcgi-php.conf 15-fastcgi-php-spawnfcgi.conf
nano 15-fastcgi-php.conf

# -*- depends: fastcgi -*-
# /usr/share/doc/lighttpd/fastcgi.txt.gz

## Start an FastCGI server for php (needs the php5-cgi package)
fastcgi.server += ( ".php" =>
                "socket" => "/var/run/php5-fpm.sock",
                "broken-scriptfilename" => "enable"

To enable the fastcgi configuration, run the following commands:

lighttpd-enable-mod fastcgi
lighttpd-enable-mod fastcgi-php

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:

ls -l /etc/lighttpd/conf-enabled

[email protected]:/etc/lighttpd/conf-available# ls -l /etc/lighttpd/conf-enabled
total 0
lrwxrwxrwx 1 root root 33 Aug 11 08:20 10-fastcgi.conf -> ../conf-available/10-fastcgi.conf
lrwxrwxrwx 1 root root 37 Aug 11 08:20 15-fastcgi-php.conf -> ../conf-available/15-fastcgi-php.conf

Then we reload Lighttpd:

systemctl force-reload lighttpd.service


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


Now we call that file in a browser (e.g.

PHPInfo for php-fpm on lighttpd.

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:

apt-cache search php5

Pick the ones you need and install them like this:

apt-get install php5-mysqlnd php5-curl php5-gd php5-intl php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-pspell php5-recode php5-sqlite php5-tidy php5-xmlrpc php5-xsl

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 in your browser and scroll down to the modules section again. You should now find lots of new modules there, including the MySQL module:

MySQL driver shown in phpinfo() output.


8 phpMyAdmin

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

apt-get install phpmyadmin

You will see the following questions:

Web server to reconfigure automatically: <-- lighttpd

apt phpmyadmin lighttpd selector.

Configure database for phpmyadmin with dbconfig-common? <-- Yes
Password of the database's administrative user: <-- yourrootsqlpassword
MySQL application password for phpmyadmin: <-- Press <enter>

Afterward, you can access phpMyAdmin under


9 Making PHP-FPM Use A TCP Connection

By default PHP-FPM is listening on the socket /var/run/php5-fpm.sock. It is also possible to make PHP-FPM use a TCP connection. To do this, open /etc/php5/fpm/pool.d/www.conf...

nano /etc/php5/fpm/pool.d/www.conf

... and make the listen line look as follows:

;listen = /var/run/php5-fpm.sock
listen =

This will make PHP-FPM listen on port 9000 on the IP (localhost). Make sure you use a port that is not in use on your system.

Then reload PHP-FPM:

systemctl restart php5-fpm.service

Next open Lighttpd's PHP configuration file /etc/lighttpd/conf-available/15-fastcgi-php.conf and replace the socket line with host and port lines:

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

# -*- depends: fastcgi -*-
# /usr/share/doc/lighttpd/fastcgi.txt.gz

## Start an FastCGI server for php (needs the php5-cgi package)
fastcgi.server += ( ".php" =>
                "host" => "",
                "port" => "9000",
                "broken-scriptfilename" => "enable"

Finally reload Lighttpd:

systemctl force-reload lighttpd.service


Share this page:

3 Comment(s)

Add comment


From: Steven

 Installed all the packages as this tutotial described. But when I try to connect the remote mariadb from PHP by

$db = new mysqli(...);

I got 500 internal server error.

i looked up the lighttpd error log, got 

 (mod_fastcgi.c.2562) unexpected end-of-file (perhaps the fastc

gi process died): pid: 0 socket: unix:/var/run/php5-fpm.sock

Can anybody tell me how to fix this?

From: sheevanian

i did "apt-get upgrade ipkg" , after apt-get install phpmyadmin has failed to install at the end and phpmyadmin didn`t load.

after upgrading ipkg (it said: already newest version, but asked me to approve with yes), phpmyadmin loaded.

this tutorial is awesome however thank you guys very much   \o/


From: Seth

Awesome tutorial!

Couple things I'd like to add from my experience.

1: If you get a 404 when trying to get to http://server/phpmyadmin , you need to run:

ln -s /usr/share/phpmyadmin /var/www/html/phpmyadmin

2: If you get a 403 while trying to access http://server/phpmyadmin, check your permissions from source to root:

In my case, my /usr/share directory did NOT have executable permissions which gave me a 403 every time I tried to access /serverIp/phpmyadmin   Needed to run:

 chmod +x /usr/bin


If anyone else experiences these issues, I'd love to help out so you don't want to smash your pc (or in my case, Raspberry Pi 3) with a hammer! Shoot me an email SethGandy at yahoo dot com