Saturday, December 1, 2007

web.py with fastcgi on Apache

我们为您准备了这个文档来帮助您快速的使用web.py

你可以在windows平台使用putty,UNIX则需要使用SSH.
你的用户需要有使用shell的权限,这个可以在用户控制面板中进行设置
在评论页可以看到更多的信息

目录

1.架设 web.py
1.1 CGI
1.1.1 0. 开始
1.1.2 1. 安装 web.py
1.1.3 2. 安装 Flup
1.1.4 3. 使用 Apache 服务
1.1.5 4. 检查并发现错误
1.2 FCGI
1.2.1 Benchmarking
1.2.2 重启 FASTCGI
1.2.3 改进稳定性和加快启动速度

架设 web.py

CGI
如果您按照我们的步骤来,那么架设CGI将是一件十分容易的事情.在下面的示例代码中,将example.com替换成您自己映射在dreamhost的域名.

0. 开始

进入到您的web主目录.

cd ~/example.com


1.安装web.py

使用Subversion命令行代码工具来获取最新的web.py

svn co http://webpy.org/svn/trunk/web/

按照如下方式来建立一个index.cgi,这将是您的第一个web.py网页

#!/usr/bin/env python2.4
import web
urls = ('/', 'index')
class index:
def GET(self):
print "Hi web.py, finally we meet!"
def runfcgi_apache(func):
web.wsgi.runfcgi(func, None)
if __name__ == "__main__":
web.wsgi.runwsgi = runfcgi_apache
web.run(urls, globals())


将该文件上传到web主目录后,使用如下的命令来使该文件可访问

chmod +x index.cgi


2.安装Flup

使用wget命令来获取最新的fcgi

wget http://svn.saddi.com/py-lib/trunk/fcgi.py

按照如下提示修改web/wsgi.py

--- wsgi.py (revision 130)
+++ wsgi.py (working copy)
@@ -13,8 +13,8 @@

def runfcgi(func, addr=('localhost', 8000)):
"""Runs a WSGI function as a FastCGI server."""
- import flup.server.fcgi as flups
- return flups.WSGIServer(func, multiplexed=True, bindAddress=addr).run()
+ import fcgi as flups
+ return flups.WSGIServer(func, multiplexed=False, bindAddress=addr).run()


(译者注:使用++的内容替换掉--的内容)

3.使用apache的服务

编辑apache的.htaccess文件来启用cgi

vim .htaccess

添加如下代码

Options +ExecCGI
AddHandler cgi-script .py

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.cgi/$1 [L]



4.检查并发现错误
现在,你的web主目录应该是这个样子

$ ls -F ~/example.com
fcgi.py index.cgi* web/


使用你的浏览器访问http://example.com/(您自己映射在dreamhost的域名),应该就可以看到问候语.如果浏览器显示"500内部错误",那就应该检查下错误日志.

tail -n 30 ~/logs/example.com/http/error.log


(译者注:上面的内容足够成功运行web.py了,下面的内容我也还没有进行,所以仅供参考)

FCGI
1.使用FastCGI架设您的域名或二级域名.假设您的域名是todo.dabase.com
2.cd; rmdir ~/todo.dabase.com
3.svn co http://svn.natalian.org/projects/todo/
4.ln -s ~/todo ~/todo.dabase.com
5.Tweak ~/todo with your mysql db (see config.py) and email stuff for errors
并不建议您在shell下进行fcgi的测试开发,因为如果fastcgi的进程开始运行后,停止或重启这个进程都是一件让人头痛的事情.您可以使用web.reloader在本地进行开发,这样,您的任何改变将立即被反应到您的web程序.

Benchmarking

ab is from the apache2-utils Debian package.
从apache2-utils Debian包中找到ab组件

/usr/sbin/ab -c 4 -n 300 http://todo.dabase.com/

4 concurrent connections pushing out 300 requests. If you find it too slow, considering running lighttpd on a dedicated server.
4个并发连接将产生300个请求.如果你发现它运行太慢,请考虑在一台专用服务器上运行lighttpd.

重启FastCGI进程

为了使新的代码起作用,你需要重启fcgi进程.
如下代码将会起作用

killall python2.4



改进稳定性和加快启动速度

1.使用http://svn.saddi.com/py-lib/trunk/fcgi.py来代替较新的flup
2.按照如下方法修改wsgi.py

--- wsgi.py (revision 130)
+++ wsgi.py (working copy)
@@ -13,8 +13,8 @@

def runfcgi(func, addr=('localhost', 8000)):
"""Runs a WSGI function as a FastCGI server."""
- import flup.server.fcgi as flups
- return flups.WSGIServer(func, multiplexed=True, bindAddress=addr).run()
+ import fcgi as flups
+ return flups.WSGIServer(func, multiplexed=False, bindAddress=addr).run()

No comments:

Post a Comment