Installing Lighttpd with PHP (PHP-FPM mode) and MySQL or MariaDB on Ubuntu 15.10
This tutorial exists for these OS versions
- Ubuntu 15.10 (Wily Werewolf)
- Ubuntu 15.04 (Vivid Vervet)
- Ubuntu 13.04 (Raring Ringtail)
- Ubuntu 11.10 (Oneiric Ocelot)
- Ubuntu 9.04 (Jaunty Jackalope)
- Ubuntu 12.10 (Quantal Quetzal)
On this page
- 1 Preliminary Note
- 2 Installing MySQL / MariaDB
- 3 Installing Lighttpd
- 4 Installing PHP 5.6
- 5 Configuring Lighttpd and PHP
- 6 Testing PHP5 / Getting Details About Your PHP5 Installation
- 7 Getting MySQL Support In PHP5
- 8 phpMyAdmin
- 9 Making PHP-FPM Use A TCP Connection (optional)
- 10 Virtual machine image download of this tutorial
- 11 Links
Lighttpd is a secure, fast and standards-compliant web server designed for speed-critical environments. This tutorial shows how to install Lighttpd on an Ubuntu 15.10 server with PHP support (through PHP-FPM) and MySQL or MariaDB. 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 before you proceed:
2 Installing MySQL / MariaDB
Ubuntu provides packages for the original MySQL database server (MySQL 5.6) and the MySQL fork MariaDB (10). Most users prefer to use MariaDB today as it is a bit faster than MySQL and the development progresses faster. I let the decision what to install to you here and will explain both options. Follow either the instructions in chapter 2.1 or 2.2, but not both.
2.1 Installation of MySQL
Install MySQL like this:
apt-get -y install mysql-server-5.6 mysql-client-5.6
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
2.2 Installation of MariaDB
Install MariaDB like this:
apt-get -y install mariadb-server mariadb-client
Now we set a root password for MariaDB.
You will be asked these questions:
Enter current password for root (enter for none): <-- press enter
Set root password? [Y/n] <-- y
New password: <-- Enter the new MariaDB root password here
Re-enter new password: <-- Repeat the password
Remove anonymous users? [Y/n] <-- y
Disallow root login remotely? [Y/n] <-- y
Reload privilege tables now? [Y/n] <-- y
3 Installing Lighttpd
Lighttpd is available as an Ubuntu package, therefore we can install it like this:
apt-get -y install lighttpd
Now direct your browser to http://192.168.1.100/index.lighttpd.html, 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 5.6
We can make PHP5 work in Lighttpd through PHP-FPM which we install like this:
apt-get -y install php5-fpm php5
PHP-FPM is a daemon process (with the systemd service unit php5-fpm.service) that runs a FastCGI server on the socket /var/run/php5-fpm.sock.
5 Configuring Lighttpd and PHP
To enable PHP5 in Lighttpd, we must modify /etc/php5/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-spawnfcgi.conf) and modify 15-fastcgi-php.conf as follows:
cp 15-fastcgi-php.conf 15-fastcgi-php-spawnfcgi.conf
# /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 php5-cgi package) fastcgi.server += ( ".php" => (( "socket" => "/var/run/php5-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 Nov 11 13:59 10-fastcgi.conf -> ../conf-available/10-fastcgi.conf
lrwxrwxrwx 1 root root 37 Nov 11 13:59 15-fastcgi-php.conf -> ../conf-available/15-fastcgi-php.conf
Then we reload Lighttpd:
service lighttpd force-reload
6 Testing PHP5 / Getting Details About Your PHP5 Installation
The document root of the default web site 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 5.6 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 PHP5
To get MySQL support in PHP, we can install the php5-mysqlnd package. This is preferred over the php5-mysql package as it contains a newer MySQL dribver that supports MySQL as well as 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 -y install php5-mysqlnd php5-curl php5-gd php5-intl php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-ming php5-ps php5-pspell php5-recode php5-snmp 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 -y install php5-xcache
Now reload PHP-FPM:
service php5-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 MySQL 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
Password of the database's administrative user: <-- Enter the MySQL/MariaDB root password
MySQL application password for phpmyadmin: <-- Press ENTER
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/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...
... and make the listen line look as follows:
[...] ;listen = /var/run/php5-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 php5-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 php5-cgi package) fastcgi.server += ( ".php" => (( "host" => "127.0.0.1", "port" => "9000", "broken-scriptfilename" => "enable" )) )
Finally reload Lighttpd:
service lighttpd force-reload
10 Virtual machine image download of this tutorial
This tutorial is available as ready to use virtual machine image in ovf/ova format for Howtoforge Subscribers. The VM format is compatible with VMWare and Virtualbox. The virtual machine image uses the following login details:
SSH / Shell Login
This user has sudo rights.
MySQL / MariaDB Login
The IP of the VM is 192.168.1.100, it can be changed in the file /etc/network/interfaces. Please change all the above passwords to secure the virtual machine.