Docs ModMySQLVhost » History » Revision 18

Revision 17 (Anonymous, 2008-09-03 12:08) → Revision 18/22 (stbuehler, 2009-02-17 10:59)

h1. Module mod_mysql_vhost - [[TracNav(DocsToc)]] 


 MySQL-based vhosting 

 {{toc}} ----------------------- 
 Module: mod_mysql_vhost 

 h2. .. meta:: 
   :keywords: lighttpd, mysql, vhost 

 .. contents:: Table of Contents 


 With MySQL-based vhosting you can store the path to a given host's 
 document root in a MySQL database. 

 *Note*: .. note:: Keep in mind that only one vhost module should be active at a time. 
           Don't mix mod_simple_vhost with mod_mysql_vhost. 

 h2. Options 



   mysql-vhost.db               = "lighttpd" 
   mysql-vhost.user             = "lighttpd" 
   mysql-vhost.pass             = "secret" 
   mysql-vhost.sock             = "/var/run/mysqld/mysqld.sock" 
   mysql-vhost.sql              = "SELECT docroot FROM domains WHERE domain='?';" 
   mysql-vhost.hostname             = "localhost" 
   mysql-vhost.port             = 3306 

 If specified, @mysql-vhost.hostname@ mysql-vhost.hostname overrides @mysql-vhost.sock@. mysql-vhost.sock. 

 MySQL setup: 


   GRANT SELECT ON lighttpd.* TO lighttpd@localhost IDENTIFIED BY 'secret'; 

   CREATE DATABASE lighttpd; 

   USE lighttpd; 

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

   INSERT INTO domains VALUES ('host.dom.ain','/http/host.dom.ain/'); 



 h2. Wildcard subdomains simple 

 I had a problem, i have webhosts with wildcard subdomains like *, and i have the files in the same docroot 

 Insert directory. I wanted to have the same functionality like spamassassin has in the SQL config, so a domain names name in the SQL like '' into works. Here's the sql table, and use the following query: 

    query that solves this problem: 


   mysql-vhost.sql              = "SELECT docroot FROM domains WHERE '?' like domain;" 


 h2. Per-vhost configuration (not using the module) 


 I wanted to be able to add configuration directives per vhost.    This is how I did it. 


   domain varchar(64) NOT NULL PRIMARY KEY, 
   docroot varchar(128) NOT NULL, 
   config text 

 then I added this line to my mysql-vhost.conf (but you can obviously create a use lighttpd.conf) 


 include_shell "/usr/share/lighttpd/ lighttpd config from a database (load it via the @include_shell@ command in the lighttpd config); secret" 
 but there is no point in using </pre> 

 the same table again with mod_mysql_vhost - parameters are just use server.document-root. the info above.    I didn't know how to pass the actual variables (maybe someone else does?) so I just repeated their values. 

 Your I then made a script called /usr/share/lighttpd/, which just prints the info.    You could write this in perl, php, or bash, if you want. 

 it should output something like that for every vhost: 

 this format: 


 $HTTP["host"] == "<DOMAIN_FROM_DATABASE>" { 
   server.document-root = "<DOCROOT_FROM_DATABASE>" 

 mine looks like this: 


 #!/usr/bin/env python 
 import sys 
 import MySQLdb 

 Please note that # load configuration data from the database 
 db=MySQLdb.connect(host='localhost', db=sys.argv[1], user=sys.argv[2], passwd=sys.argv[3]) 
 cur = db.cursor() 
 cur.execute("SELECT * FROM domains") 

 for domain in rs:     

     print "$HTTP[\"host\"] == \"%s\" {\nserver.document-root = \"%s\"\n%s\n}" % (domain[0], domain[1], domain[2]) 

 Now, you should not allow your users to modify can put whatever directives you want in the config which gets inserted here, or they just may use include_shell with "rm -rf /" - which gets probably executed with root. 

 And you have field. 
 Make sure to restart your webserver every time server to enable the settings, if you change something. them in the database.