怎样配置mod_python

2018年8月1日 | 分类: 【技术】

【介绍】

Mod_python is an Apache module that embeds the Python interpreter within the server. With mod_python you can write web-based applications in Python that will run many times faster than traditional CGI and will have access to advanced features such as ability to retain database connections and other data between hits and access to Apache internals. A more detailed description of what mod_python can do is available in this O’Reilly article.

官网:http://modpython.org/

介绍:http://www.cnblogs.com/ArtsCrafts/p/mod_python.html

【安装】

文档:http://modpython.org/live/current/doc-html/installation.html
文档:http://www.cnblogs.com/oubo/archive/2012/04/06/2434961.html

下载:github.com/grisha/mod_python

wget https://github.com/grisha/mod_python/archive/master.zip && unzip master.zip && cd mod_python-master
./configure --with-apxs=/usr/local/apache2/bin/apxs --with-python=/usr/local/bin/python2.7  --with-flex=/usr/bin/flex
make && sudo make install

【配置】

编辑 /usr/local/apache2/conf/httpd.conf ,

添加:

LoadModule python_module modules/mod_python.so

测试1:

从 mod_python 3.2.0 开始,可以使用 mod_python.testhandler 来诊断配置问题:

添加:

<Location /mpinfo>
  SetHandler mod_python
  PythonHandler mod_python.testhandler
</Location>

重启 httpd:

service httpd stop && service httpd start

在浏览器中打开:http://webinfo.org/mpinfo ,返回系统配置信息页面。

测试2:

创建项目目录 /usr/local/apache2/htdocs/test/

创建文件 /usr/local/apache2/htdocs/test/mptest.py

from mod_python import apache

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

编辑 /usr/local/apache2/conf/httpd.conf ,

添加:

<Directory /usr/local/apache2/htdocs/test>
    AddHandler mod_python .py
    PythonHandler mptest
    PythonDebug On
</Directory>

浏览器中打开:http://webinfo.org/test/mptest.py ,返回页面显示:

Hello World!