Install Lighttpd web server with PHP5 support using MySQL backend

Lighttpd is a web server which is designed to be secure, fast, standards-compliant, and flexible while being optimized for speed-critical environments. Its low memory footprint (compared to other web servers), light CPU load and its speed goals make lighttpd suitable for servers that are suffering load problems, or for serving static media separately from dynamic content. lighttpd is free software / open source, and is distributed under the BSD license.

In this tutorial you will be guided through the installation process and to configure the web server to use MySQL backend. First install the lighttpd package and the MySQL backend module

apt-get install lighttpd lighttpd-mod-mysql-vhost

Create a MySQL database, table and insert some sample data

mysql -u root -p
GRANT SELECT ON lighttpd.* TO vhosts@localhost IDENTIFIED BY 'mypasswd';
USE lighttpd;
domain varchar(64) NOT NULL PRIMARY KEY,
docroot varchar(128) NOT NULL
INSERT INTO vhosts VALUES ('','/var/www/');
INSERT INTO vhosts VALUES ('','/var/www/');

Create a configuration file for the mysql backend (pico /etc/lighttpd/conf-available/10-mysql-vhost.conf). Add the following lines:

server.modules += ( "mod_mysql_vhost" )
mysql-vhost.db = "lighttpd"
mysql-vhost.user = "vhosts"
mysql-vhost.pass = "mypasswd"
mysql-vhost.sock = "/var/run/mysqld/mysqld.sock"
mysql-vhost.sql = "SELECT docroot FROM vhosts WHERE domain='?';"

Make sure you change the db, user and pass variables to a valid mysql user and database on the local server. Enable the mysql backend module

ln /etc/lighttpd/conf-available/10-mysql-vhost.conf /etc/lighttpd/conf-enabled/10-mysql-vhost.conf

Install PHP5 packages

apt-get install php5-cgi php5-mysql php5-gd php5-imagick php5-imap php5-mcrypt php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl

Configure PHP5 to display correct values for PATH_INFO and PHP_SELF (pico /etc/php5/cgi/php.ini). Change the following variable found in your php.ini file:

cgi.fix_pathinfo = 1

Configure fastcgi to use PHP5 insted of the default PHP4 (pico /etc/lighttpd/conf-available/10-fastcgi.conf)

"bin-path" => "/usr/bin/php5-cgi",

Enable fastcgi/PHP5:

ln /etc/lighttpd/conf-available/10-fastcgi.conf /etc/lighttpd/conf-enabled/10-fastcgi.conf

Restart the lighttpd web server:

/etc/init.d/lighttpd restart

Now you’re all set and you should be able to browse to (make sure the host exists in DNS or has been placed in your local hosts file)

Lighttpd caches the vhost configurations until next restart so you’ll have to restart lighttpd everytime a change is made in the MySQL database.

Leave a Reply

Your email address will not be published. Required fields are marked *