Installing Lighttpd With PHP5 (PHP-FPM) And MySQL Support On Ubuntu 14.04LTS
Version 1.0
Author: Falko Timme, updated by Srijan Kishore
Last edited 07/May/2014
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 14.04 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.
I do not issue any guarantee that this will work for you!
In this tutorial I use the hostnameserver1.example.comwith the IP address192.168.0.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:
sudo su
First 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 userroot@localhostas well asroot@server1.example.com, so we don't have to specify a MySQL root password manually later on:
New password for the MySQL "root" user:Repeat password for the MySQL "root" user:
Lighttpd is available as an Ubuntu package, therefore we can install it like this, as apache2 is installed by default in ubuntu14.04 so first we will uninstall apache2 & then further install lighttpd.
service apache2 stop
update-rc.d -f apache2 remove
apt-get remove apache2
apt-get install lighttpd
Now direct your browser tohttp://192.168.0.100/index.lighttpd.html, and you should see the Lighttpd placeholder page:
Lighttpd's default document root is/var/wwwon Ubuntu, and the configuration file is/etc/lighttpd/lighttpd.conf. Additional configurations are stored in files in the/etc/lighttpd/conf-availabledirectory - these configurations can be enabled with thelighttpd-enable-modcommand which creates a symlink from the/etc/lighttpd/conf-enableddirectory to the appropriate configuration file in/etc/lighttpd/conf-available. You can disable configurations with thelighttpd-disable-modcommand.
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 scriptphp5-fpm) that runs a FastCGI server on the socket/var/run/php5-fpm.sock.
To enable PHP5 in Lighttpd, we must modify/etc/php5/fpm/php.iniand uncomment the linecgi.fix_pathinfo=1:
vi /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-pathinfocgi.fix_pathinfo=1[...] Copier après la connexion |
The Lighttpd configuration file for PHP/etc/lighttpd/conf-available/15-fastcgi-php.confis suitable for use with spawn-fcgi, however, we want to use PHP-FPM, therefore we create a backup of the file (named15-fastcgi-php.conf.bak) and modify15-fastcgi-php.confas follows:
cd /etc/lighttpd/conf-available/
cp 15-fastcgi-php.conf 15-fastcgi-php.conf.bak
vi 15-fastcgi-php.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"))) Copier après la connexion |
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.confwhich points to/etc/lighttpd/conf-available/10-fastcgi.confand/etc/lighttpd/conf-enabled/15-fastcgi-php.confwhich points to/etc/lighttpd/conf-available/15-fastcgi-php.conf:
ls -l /etc/lighttpd/conf-enabled
root@server1:/etc/lighttpd/conf-available# ls -l /etc/lighttpd/conf-enabled
total 0
lrwxrwxrwx 1 root root 33 May 7 05:33 10-fastcgi.conf -> ../conf-available/10-fastcgi.conf
lrwxrwxrwx 1 root root 37 May 7 05:33 15-fastcgi-php.conf -> ../conf-available/15-fastcgi-php.conf
root@server1:/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 install language-pack-en-base
dpkg-reconfigure locales
The document root of the default web site 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.
vi /var/www/info.php
<?phpphpinfo ();?> Copier après la connexion |
Now we call that file in a browser (e.g.http://192.168.0.100/info.php):
As you see, PHP5 is working, and it's working through FPM/FastCGI, as shown in theServer APIline. 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.
To get MySQL support in PHP, we can install thephp5-mysqlpackage. 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-mysql 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 install php5-xcache
Now reload PHP-FPM:
service php5-fpm reload
Now reloadhttp://192.168.0.100/info.phpin your browser and scroll down to the modules section again. You should now find lots of new modules there, including the MySQL module:
phpMyAdminis 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:Configure database for phpmyadmin with dbconfig-common?
Afterwards, you can access phpMyAdmin underhttp://192.168.0.100/phpmyadmin/:
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...
vi /etc/php5/fpm/pool.d/www.conf
... and make thelistenline look as follows:
[...];listen = /var/run/php5-fpm.socklisten = 127.0.0.1:9000[...] Copier après la connexion |
This will make PHP-FPM listen on port9000on the IP127.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.confand replace thesocketline withhostandportlines:
vi /etc/lighttpd/conf-available/15-fastcgi-php.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" =>(("host" => "127.0.0.1","port" => "9000","broken-scriptfilename" => "enable"))) Copier après la connexion |
Finally reload Lighttpd:
service lighttpd force-reload