让 Apache 支持 mod_python

mod_pytho 中文文档
http://man.chinaunix.net/develop/python/mod_python/mod_python.html

1, 下载 ModPython-3.3.1 (http://www.apache.org/dist/httpd/modpython/win/3.3.1/), 安装, 安装过程中会讯问 apache 安装目录.
2, 在apache的httpd.conf中加入下面一行:

LoadModule python_module modules/mod_python.so

3, 再加入下面一段

Alias /py/ "d:/PythonCode/"
<Directory "d:/PythonCode/">
    AddHandler mod_python .py
    #PythonHandler mptest
    PythonHandler mod_python.publisher
    PythonDebug On
</Directory>

4, 在 d:/PythonCode/ 目录下新建一名为 mptest.py 的测试文件,内容如下

from mod_python import apache
def handler(req):
   req.content_type = "text/plain"
   req.write("Hello World!")
   return apache.OK

5, 通过 http://127.0.0.1/py/mptest.py 访问测试

可能会遇到的问题:


http://hi.baidu.com/javaway/blog/item/ea7401822bc491aa0cf4d2d8.html

环境:Python:2.5.4 / Apache:2.2
我安装完mod_python之后,在apache的配置文件里httpd.conf,添加了一行:
LoadModule python_module modules/mod_python.so
使得Apache加载mod_python模块
可是在我重新启动Apache时,apache却报错无法启动,看看日志:
[Fri Mar 27 15:07:49 2009] [notice] Parent: Received restart signal -- Restarting the server.
[Fri Mar 27 15:07:49 2009] [notice] Child 948: Exit event signaled. Child process is ending.
httpd.exe: Syntax error on line 127 of D:/Program Files/Apache Software Foundation/Apache2.2/conf/httpd.conf: Cannot load D:/Program Files/Apache Software Foundation/Apache2.2/modules/mod_python.so into server: \xd5\xd2\xb2\xbb\xb5\xbd\xd6\xb8\xb6\xa8\xb5\xc4\xc4\xa3\xbf\xe9\xa1\xa3
[Fri Mar 27 15:07:50 2009] [warn] (OS 995)由于线程退出或应用程序请求,已放弃 I/O 操作。 : winnt_accept: Asynchronous AcceptEx failed.

我又花了很长的时间来查找原因,初始时以为是字符编码问题,然后我又看看mod_python的权限,貌似没有问题。没辙,股沟了一下,在modpython的论坛中有人提出可能是因为Python安装时选择值安装到自己的账户导致的:

Please don't follow up to a different thread with a new question.
Subject now changed.

If you are running Apache as a service, it may be running as a
specific user, not the one which you installed the mod_python code as.
If this is the case, you need to make sure that the user that Apache
is running as has correct access rights to be able to read the module
file.

If this has occurred, you may also have problems with loaded module
then being able to read the installed Python code files.

Generally it is recommended that Apache, Python and mod_python all be
installed as an administrator and not as an individual user.

我不是很确定,但我记得安装Python时的确是选择了单用户,尝试重新安装Python,选择为所有用户安装,重启Apache,OK.
This entry was posted in Apache, Python and tagged , . Bookmark the permalink. Trackbacks are closed, but you can post a comment.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>