Skip to content

How to configure Lighttpd with PHP5 and MySQL5

by Dimi on October 24th, 2009

Over the years Apache has become somewhat of a standard web-server on unix servers but since the end of 2007 a worthy alternative to apache was born, called Lighttpd. Lighttpd has been proven to handle webrequests more efficiently than apache, resulting in a smaller memory footprint and a lower cpu load. For the moment a lot of large websites (more than 10 millions requests per day) are running on a lighttpd webserver, a few examples of these sites are:

This already is a fine curriculum so I thought it was time to configure a lighttpd server (with MySQL5 and PHP5 support) myself although I don’t come across any customers (I work at a hosting provider) who are using this at the moment.

We start of knowing that I usually work on debian-servers so some commands will not work on fedora, red-hat, and others.

First of all we check if apache isn’t already installed which it is in a lot of cases.

dpkg -l | grep apache

ii  apache2-doc            2.2.3-4+etch6    documentation for apache2
ii  apache2-mpm-prefork    2.2.3-4+etch6    Traditional model for Apache
ii  apache2-utils          2.2.3-4+etch6    utility programs for webservers
ii  apache2.2-common       2.2.3-4+etch6    Next generation, scalable,

If apache seems to be installed we will need to remove it to avoid conflicts on port 80.

apt-get remove apache2-doc apache2-mpm-prefork apache2-utils apache2.2-common

Now we will install MySQL (The “-y” parameter avoids questions during the install by answering “yes” to every question.

apt-get install –y mysql-server mysql-client

We start MySQL.

/etc/init.d/mysql start

We configure the MySQL root password.

mysqladmin -u root password ''password_of_your_choice''

After the succesfull installation of MySQL we continue the configuration by installing lighttpd.

apt-get install -y lighttpd

Just as a test we can now surf to the hostname or ip-address of your server to verify the succesfull installation.

lighttpd

The default document root is /var/www/ and the configuration file is /etc/lighttpd/lighttpd.conf.

We will now install PHP5 as a fastCGI module, we will also add some other PHP5-modules so that these can also be addressed by PHP..

apt-get install -y php5-cgi php5-mysql php5-curl ...

After this installation we will need to modify the configuration files of PHP and lighttpd. In the PHP configuration file (eg. /etc/php5/cgi/php.ini) we add the following line (preferably at the end of the file.

cgi.fix_pathinfo = 1

In the lighttpd configuration file (/etc/lighttpd/lighttpd.conf) we modify “server.modules” by adding a new line

"mod_fastcgi",

At the end of this file we also need to add a few lines to tell the webserver that all files with the .php extension need to be handled by the FastCGI module.

fastcgi.server = ( ".php" =>((
"bin-path" => "/usr/bin/php5-cgi",
"socket" => "/tmp/php.socket"
)))

After saving this configuration file we can restart the lighttpd service.

/etc/init.d/lighttpd restart

We can now test be creating a basic php-file like info.php with the following content

<?php phpinfo(); ?>

We can now witness a working lighttpd webserver with PHP5 loaded.

From → Lighttpd

No comments yet

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS