Installing Lighttpd with PHP 7 (PHP-FPM) and MySQL 5.7 on Ubuntu 16.04 LTS

Lighttpd is a secure, fast, standards-compliant web server designed for speed-critical environments. This tutorial shows how you can install Lighttpd on an Ubuntu 16.04 server with PHP 7 support (through PHP-FPM) and MySQL 5.7. 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.

I'm running all the steps in this tutorial with root privileges, so make sure you're logged in as root:

sudo -s


2 Installing MySQL 5.7

First, we install MySQL like this:

apt-get -y 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

Type in the MySQL password. 

The installer has set a MySQL root password, but there are some more settings that should be changed for a secure MySQL installation. This can be done with the mysql_secure_installation command.


The command is interactive:

[email protected]:~# mysql_secure_installation

Securing the MySQL server deployment.

Enter password for user root: <-- Enter the MySQL root password

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: <-- Press y if you want this function or press Enter otherwise.
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) : <-- Press enter

... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production

Remove anonymous users? (Press y|Y for Yes, any other key for No) : <-- y

Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : <-- y

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production

Remove test database and access to it? (Press y|Y for Yes, any other key for No) : <-- y
- Dropping test database...

- Removing privileges on test database...

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : <-- y

All done!

3 Installing Lighttpd

Lighttpd is available as an Ubuntu package. Therefore, we can install it directly with apt from the Ubuntu Xenial Xerus package repository:

apt-get -y install lighttpd

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

The Ubuntu Lighttpd default page.

Lighttpd's default document root is /var/www/html on Ubuntu, 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 PHP 7.0

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

apt-get -y install php7.0-fpm php7.0

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


5 Configuring Lighttpd and PHP 7.0

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

nano /etc/php/7.0/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

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.conf.bak) and modify 15-fastcgi-php.conf as follows:

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

# /usr/share/doc/lighttpd-doc/fastcgi.txt.gz

## Start an FastCGI server for php (needs the php7.0-cgi package)
fastcgi.server += ( ".php" =>
                "socket" => "/var/run/php/php7.0-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 Apr 27 11:26 10-fastcgi.conf -> ../conf-available/10-fastcgi.conf
lrwxrwxrwx 1 root root 37 Apr 27 11:26 15-fastcgi-php.conf -> ../conf-available/15-fastcgi-php.conf
lrwxrwxrwx 1 root root 42 Apr 21 11:10 90-javascript-alias.conf -> ../conf-available/90-javascript-alias.conf
[email protected]:/etc/lighttpd/conf-available#

Then we reload Lighttpd:

service lighttpd force-reload

 Note: If you get locale errors then you can remove the error by using

apt-get -y install language-pack-en-base 
dpkg-reconfigure locales

6 Testing PHP 7.0 / Getting Details About Your PHP Installation

The document root of the default website is /var/www/html. 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.

PHP info on lighttpd.

As you see, PHP 7.0 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 PHP yet.


7 Getting MySQL support in PHP

To get MySQL support in PHP, we can install the php7.0-mysql package. It's a good idea to install some other PHP modules as well as you might need them for your applications. You can search for available PHP modules like this:

apt-cache search php7.0

Pick the ones you need and install them like this:

apt-get -y install php7.0-mysql php7.0-curl php7.0-gd php7.0-intl php-pear php-imagick php7.0-imap php7.0-mcrypt php-memcache  php7.0-pspell php7.0-recode php7.0-sqlite3 php7.0-tidy php7.0-xmlrpc php7.0-xsl php7.0-mbstring php-gettext

APCu is an extension for the PHP Opcache module that comes with PHP 7, it adds some compatibility features for software that supports the APC cache (e.g. Wordpress cache plugins).

APCu can be installed as follows:

apt-get -y install php-apcu

Now reload PHP-FPM:

service php7.0-fpm reload

Now reload in your browser and scroll down to the modules section again. You should now find lots of new modules there, including the mysqli and mysqlnd module:


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 -y install phpmyadmin

You will see the following questions:

Web server to reconfigure automatically: <-- lighttpd
Configure database for phpmyadmin with dbconfig-common? <-- Yes
MySQL application password for phpmyadmin: <-- Press Enter

If you get the following error:

Run /etc/init.d/lighttpd force-reload to enable changes
dpkg: error processing package phpmyadmin (--configure):
subprocess installed post-installation script returned error exit status 2
E: Sub-process /usr/bin/dpkg returned an error code (1)

Then run these commands:

/etc/init.d/lighttpd force-reload
apt-get -y install phpmyadmin

Afterwards, you can access phpMyAdmin under


9 Making PHP-FPM use a TCP Connection (Optional)

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

nano /etc/php/7.0/fpm/pool.d/www.conf

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

;listen = /var/run/php/php7.0-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:

service php7.0-fpm reload

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

# /usr/share/doc/lighttpd-doc/fastcgi.txt.gz

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

Finally reload Lighttpd:

service lighttpd force-reload


Share this page:

8 Comment(s)

Add comment


From: ganewbie at: 2016-05-10 18:20:43


Could you add ISPConfig3 on this setup? If yes, have you tried it?


From: till at: 2016-05-15 08:24:43

ISPConfig 3 requires Apache or Nginx as web server, s you can not use it with lighttpd. Nginx is as fast as Lighttpd, so if you want to have a fast alternative to Apache, then use one of the Nginx perfect server tutorials to install ISPConfig.

From: Jay at: 2016-05-27 18:04:24

Untill phpmyadmin everything worked, dunno if it's phpmyadmin or this tuto, I expect the first.

From: Jeff at: 2016-05-30 15:16:53

In order to bring up phpMyAdmin I had to perform this command sudo ln -s /usr/share/phpmyadmin /var/www/html 

From: till at: 2016-05-30 15:35:10

That's only necessary if you missed to enable the lighttpd option (with the space bar of your keyboard) in the apt installer in this step of the tutorial:


Web server to reconfigure automatically: <-- lighttpd

Another way to fix it if you missed to enable it during install is to run: dpkg-reconfigure phpmyadmin

From: Jeff at: 2016-05-28 23:45:51

Tried to change the socket to listen on and now my whole site is down showing 503 Service Not Available. Tried to change back to using the sock and still nothing. Cannot restart FPM without it throwing and error. Help please.

From: Jeff at: 2016-05-30 15:18:08

I said screw it and just rebuilt another server and this time I will not try to change to listen on

From: Krzysiu at: 2016-08-16 13:07:30

My knowledge about Linux is yet not so good. After updating to Ubuntu 14 my HTTP server died. Now I know the exact problem was PHP5>PHP7 update. Your tutorial helped me fix it - 15-fastcgi-php.conf was wrong. So thanks a lot, you saved me a lot of time!