Installing Lighttpd with PHP 7 (PHP-FPM) and MySQL 5.7 on Ubuntu 16.04 LTS
This tutorial exists for these OS versions
On this page
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 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.
I'm running all the steps in this tutorial with root privileges, so make sure you're logged in as root:
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
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
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
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 http://192.168.1.100/, and you should see the Lighttpd placeholder 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:
[...] ; 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 [...]
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:
cp 15-fastcgi-php.conf 15-fastcgi-php.conf.bak
# /usr/share/doc/lighttpd-doc/fastcgi.txt.gz # http://redmine.lighttpd.net/projects/lighttpd/wiki/Docs:ConfigurationOptions#mod_fastcgi-fastcgi ## 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:
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
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
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
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.
<?php phpinfo(); ?>
Now we call that file in a browser (e.g. http://192.168.1.100/info.php):
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 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 mysqli and mysqlnd module:
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:
apt-get -y install phpmyadmin
Afterwards, you can access phpMyAdmin under http://192.168.1.100/phpmyadmin/:
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...
... and make the listen line look as follows:
[...] ;listen = /var/run/php/php7.0-fpm.sock listen = 127.0.0.1:9000 [...]
This will make PHP-FPM listen on port 9000 on the IP 127.0.0.1 (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:
# /usr/share/doc/lighttpd-doc/fastcgi.txt.gz # http://redmine.lighttpd.net/projects/lighttpd/wiki/Docs:ConfigurationOptions#mod_fastcgi-fastcgi ## Start an FastCGI server for php (needs the php7.0-cgi package) fastcgi.server += ( ".php" => (( "host" => "127.0.0.1", "port" => "9000", "broken-scriptfilename" => "enable" )) )
Finally reload Lighttpd:
service lighttpd force-reload