2008年10月22日星期三

lighttpd & django搭建测试环境

1. 软件环境
我的机器ip为192.168.1.183。
在此机器上安装了lighttpd1.4.20、python2.5、以及django1.0、flu1.0。
注:若在安装lighttpd时缺少依赖包,直接使用sudo apt-get install lighttpd来安装试试。其它可以源码编译安装。

2.创建工程
在/work/sites下创建一个django工程mysite,如:pwd/django-admin.py startproject mysite
在mysite里添加一个py文件first.py

from django.http import HttpResponse
def index(request):
return HttpResponse('first django page .....')

修改url.py,在patterns里添加:
(r'^mysite/$','mysite.first.index')


3.在mysite下添加一个文件mysite.cgi
#!/usr/bin/python
import sys,os
sys.path.inert(0, "/work/sites/mysite")
os.environ['DJANGO_SETTING_MODULE'] = 'setting'

from django.core.servers.fastcgi import runfastcgi
runfastcgi(method='threaded', daemonize='fase', socket='/work/sites/mysite/django.sock')


4.配置lighty
server.modules= ("mod_rewrite","mod_redirect","mod_alias","mod_access","mod_fastcgi","mod_accesslog")

#添加如下信息(采用load balance),采用tcp方式通讯:
$HTTP["remoteip"] == "192.168.1.183"{
server.document-root = "/work/sites/mysite/"
fastcgi.server = (
"/mysite.fcgi" => (
("host" => "127.0.0.1",
"port" => 8100,
#"socket" => "/work/sites/mysite/django.sock",
"check-local" => "disable"),
("host" => "192.168.1.183",
"port" => 8101,
"check-local" => "disable"),
),
)

alias.url = (
"/media/" => "/work/sites/mysite/media/",
)

url.rewrite-once = (
"^(/media.*)$" => "$1",
"^/favicon.ico$" => "/media/favicon.ico",
"^(/.*)$" => "/mysite.fcgi$1",
)
}

启动lighty

5.启动2个fastcgi服务(注意host区别,在前端分发时会匹配)
sudo python manage.py runfcgi host=127.0.0.1 port=8100 method=threaded
sudo python manage.py runfcgi host=192.168.1.183 port=8101 method=threaded

6.访问
http://192.168.1.183/mysite
页面输出为:first django page .....

可观察到请求被正常分发到127.0.0.1:8100和192.168.1.183:8101上(不同的fastcgi server)。

没有评论: