How to create simple virtual hosts with mod_mysql_vhost on Lighttpd on Ubuntu

This guide explains how you can use mod_mysql_vhost to create simple virtual hosts on a lighttpd web server on Ubuntu. With mod_mysql_vhost, lighttpd can read the vhost configuration from a MySQL database. Currently, you can store the domain and the document root in the MySQL database which results in very simple virtual hosts. If you need more directives for your vhosts, you’d have to configure them in the global section of lighttpd.conf, which means they’d be valid for all vhosts. Therefore, mod_mysql_vhost is ideal if your vhosts differ only in the domain and document root.

Because we must run all the steps from this tutorial with root privileges, we can either exec all commands in this tutorial with the string sudo, or we become root right now by typing:

sudo su

Once we become ROOT we install MySQL 5 like this:

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

You will be asked to provide a password for the MySQL root user – this password is valid for the user “root@localhost” as well as ““, 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

Next, we will install lighttpd (if it’s not already installed) and mod_mysql_vhost as follows:

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

To enable mod_mysql_vhost, we open /etc/lighttpd/lighttpd.conf and add/enable “mod_mysql_vhost”, in the server.modules stanza:

vi /etc/lighttpd/lighttpd.conf
 server.modules = (
# “mod_rewrite”,

Afterwards, we will need to restart lighttpd:

/etc/init.d/lighttpd restart

Now we need to configure mod_mysql_vhost. to do that log in to MySQL:

mysql -u root -p

and create the database lighttpd:


Next we create a database user (which we name lighttpd as well) with SELECT privileges for the lighttpd database:

GRANT SELECT ON lighttpd.* TO lighttpd@localhost IDENTIFIED BY ‘secret’;
GRANT SELECT ON lighttpd.* TO lighttpd@localhost.localdomain IDENTIFIED BY ‘secret’;

(Replace “secret” with a password of your choice.)

Then we create the domains table in the lighttpd database and leave MySQL:

USE lighttpd;
CREATE TABLE domains (
domain varchar(64) not null primary key,
docroot varchar(128) not null

Now we will need to open/edit /etc/lighttpd/lighttpd.conf and add the following mod_mysql_vhost configuration at the end of the file:

vi /etc/lighttpd/lighttpd.conf
mysql-vhost.db = “lighttpd”
mysql-vhost.user = “lighttpd”
mysql-vhost.pass = “secret”
mysql-vhost.sql = “SELECT docroot FROM domains WHERE domain=’?’;”
mysql-vhost.hostname = “localhost”
mysql-vhost.port = 3306

(Replace “secret” with the password you’ve previously set for the lighttpd MySQL user.) and restart lighttpd:

/etc/init.d/lighttpd restart

Now it’s time to configure virtual hosts…

We will now configure two virtual hosts, one for (with the document root /var/www/ and another one for (with the document root /var/www/

First, we create the document roots of both web sites (if they don’t already exist):

mkdir -p /var/www/
mkdir -p /var/www/

and we log in to MySQL again…

mysql -u root -p
USE lighttpd;

and create the vhosts as follows:

INSERT INTO domains VALUES (‘’,’/var/www/’);
INSERT INTO domains VALUES (‘’,’/var/www/’);

We can now leave the MySQL shell:


That’s it, the vhosts are now configured and working, and no lighttpd restart is required.

To check if the vhosts are working as expected, we create an index.html file in each document root, one with the string “” in it, the other one with the string “”…

echo “” &gt; /var/www/
echo “” &gt; /var/www/

and call and in a browser. should show, and should display

Be the first to comment

Leave a Reply