Debian Tutorials

Debian Tutorials


Step by step tutorials showing you how to install and configure various applications and services on Debian based Linux distros.

June 2021
M T W T F S S
 123456
78910111213
14151617181920
21222324252627
282930  

Categories


Install Lighttpd web server with PHP5 support using MySQL backend

adminadmin

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 [email protected] IDENTIFIED BY 'mypasswd';
FLUSH PRIVILEGES;
CREATE DATABASE lighttpd;
USE lighttpd;
 
CREATE TABLE vhosts (
domain varchar(64) NOT NULL PRIMARY KEY,
docroot varchar(128) NOT NULL
);
 
INSERT INTO vhosts VALUES ('test.com','/var/www/test.com/');
INSERT INTO vhosts VALUES ('www.test.com','/var/www/test.com/');
 
quit;

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 test.com. (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.

Comments 1
  • RoseHosting
    Posted on

    RoseHosting RoseHosting

    Author

    Do not forget to add ‘mod_fastcgi’ to the server.modules block in the lighttpd configuration file and restart the lighttpd service for the changes to take effect.