常见问题
业务指南
文档下载
意见投诉
ICP服务

在lighttpd上配置基于mysql的虚拟主机

lighttpd是一款轻量级的web服务,默认支持虚拟主机,但是我们希望通过增加一些简单的代码获得虚拟主机对mysql的支持。
测试环境为kubuntu 8.04.1,大致的步骤如下
1)下载必要的软件,包括lighttpd,mysql,python,python-mysqldb
2)创建必要的数据库,数据表及测试数据
3)给lighttpd添加自定义的配置文件

下面详细叙述:

一、下载必要的软件
lighttpd是必须的,mysql作为存储数据库的方式,也是必须的,python和python-MySQLdb只所以需要是因为我们增加的配置文件和功能是由python能完成的,当然你也可以使用其他语言来完成,比如perl。
[code]sudo apt-get install lighttpd mysql-server python python-MySQLdb[/code]

二、配置数据库
我们创建一个lighttpd帐号来管理自己的数据库,步骤如下:



# mysql -uroot -p                                                                              
Enter password:                                                                                                
Welcome to the MySQL monitor.  Commands end with ; or \g.                                                      
Your MySQL connection id is 11                                                                                  
Server version: 5.0.51a-3ubuntu5.2 (Ubuntu)                                                                    

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> grant all on lighttpd.* to lighttpd@localhost identified by 'abc123';
Query OK, 0 rows affected (0.07 sec)                                        

mysql> exit
Bye        
# mysql -ulighttpd -p
Enter password:                      
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12                          
Server version: 5.0.51a-3ubuntu5.2 (Ubuntu)              

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> create database lighttpd;
Query OK, 1 row affected (0.08 sec)

mysql> use lighttpd
Database changed
mysql>
mysql> create table domains(
    -> domain varchar(100) not null primary key,
    -> docroot varchar(200) not null,
    -> config text);
Query OK, 0 rows affected (0.04 sec)


然后我们创建一些测试数据库


mysql> insert into domains values(
    -> 'www.foo.com','/var/www/www.foo.com/','dir-listing.activate="enable"\nalias.url=("/"=>"/var/www/www.foo.com/")');
Query OK, 1 row affected (0.09 sec)

mysql> insert into domains values('www.bar.com','/var/www/www.bar.com/','dir-listing.activate="disable"');
Query OK, 1 row affected (0.01 sec)



三、配置Lighttpd
首先创建一个python脚本,用来从指定的数据库中读取必要的信息,并动态生成配置文档。


# cat /usr/share/lighttpd/mysql_vhost.py
#!/usr/bin/env python                                          
import sys                                                      
import MySQLdb

# 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")
rs=cur.fetchall()
db.close()

for domain in rs:

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


然后增加一个lighttpd配置文件,我们遵循lighttpd配置文件的放置规则,首先在/etc/lighttpd/conf-available/目录下创建一个名为10-vhost.conf的文件,内容如下:

include_shell "/usr/share/lighttpd/mysql_vhost.py lighttpd lighttpd abc123"



接下来,在/etc/lighttpd/conf-enabled/目录下创建一个软链接

ln -sf ../conf-available/10-vhost.conf .



四、测试
完成上面的配置后,我们就可以测试了,首先在/var/www/下面创建www.foo.comwww.bar.com两个目录
然后在/etc/hosts文件里添加对这两个域名的解析,最简单的方式是添加下面这行


127.0.0.1  wwww.foo.com www.bar.com


打开浏览器,在地址栏输入http://www.foo.com/,是不是可以看到类似下面的信息

再浏览http://www.bar.com/,是不是得到类似下面的信息

这里很有意思,居然不是说430的错误,而不是报404的错误。当然,你可以在/var/www/www.bar.com/下面添加一个index.html文件,再刷新,是不是有了index.html的内容。

上海电信漕宝机房 地址:漕宝路1600号 漕宝机房介绍 上海移动双线机房 机房地址: 金沙江路1340弄 双线机房介绍
公司地址:上海市普陀区清峪路368弄12号301 邮编:200333 电话 021-52691612 传真 021-52693626
《增值电信业务经营许可证》许可证编号: 沪ICP备05001352号 上海市互联网违法与违规信息举报中心
上海市互联网协会 上海市电子商务行业协会 DNS Stuff 公益性SEO 页面执行时间:46.875毫秒
上海纵智信息技术有限公司 版权所有 2004-2007 营业执照