`
webcode
  • 浏览: 5941101 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

apache的python部署

 
阅读更多

我的环境是server2003,apache2.2已经安装好了并已经运行。

首先下载软件:

python:http://www.python.org,给最新下载页面http://www.python.org/download/releases/2.5.2/

mod_python:http://apache.mirror.phpchina.com/httpd/modpython/win/3.3.1/(严重注意版本,python对版本要求很严格,请务必下载符合你需要的版本)

第一步:安装python,这步没有什么难度,一路下一步就可以了。

第二步:安装mod_python,我下载的文件是mod_python-3.3.1.win32-py2.5-Apache2.2.exe,一路下一步。注意最后一步选择apache安装目录,请选择apache2.2的目录,一定要选择到apache2.2,不然mod不能正确生成到modules,如果第一次没有正确请重新运行安装mod_python即可。

安装完成后出现以下信息,表示安装完成:

Important Note for Windows users, PLEASE READ!!!


1. This script does not attempt to modify Apache configuration,

you must do it manually:


Edit D:/program files/Apache Software Foundation/Apache2.2confhttpd.conf,

find where other LoadModule lines are and add this:

LoadModule python_module modules/mod_python.so


2. Now test your installation using the instructions at this link:

http://www.modpython.org/live/current/doc-html/inst-testing.html

3.第三步,配置apache服务器。修改httpd.conf文件

a)找到 LoadModule 位置加上下面一行

LoadModule python_module modules/mod_python.so
说明:在安装modpy_thon结束时会报告mod_python.so的位置和LoadModule目录的形式
c) 找到Alias别名定义(当然你可以使用根目录),加入如下行

Alias /python D:/program files/Apache Software Foundation/Apache2.2/htdocs/python
c) 找到目录定义加入如下行:

AllowOverride FileInfo
AddHandler mod_python .py
PythonHandler mptest
PythonDebug On

4.第四步:测试一下

在D:/program files/Apache Software Foundation/Apache2.2/htdocs/python下新建一个文件test.py

文件内容如下:

注意第三、第四行的缩进(不要问原因,python的语法就是这么变态)

Python代码
from mod_python import apache
def handler(req):
req.write("Hello World!BlogGuy")
return apache.OK
出来的结果为Hello World!BlogGuy!

配置完成。


问题:

这样设置会导致每次的请求都会访问mptest.py文件,解决方法:

将mytest.py修改成如下内容:

import os;  
from mod_python import apache  
def handler(req):  
    handler = req.uri[1:];  
    if handler[-3:] == ".py" :  
        handler = handler[0:-3];  
    if not handler == "index" :  
        req.add_handler("PythonHandler", handler);  
    return apache.OK

ok!

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics