我能够从源代码成功安装 apache 和 python。
我使用以下配置命令安装了 mod_python:
./configure --prefix=/usr/local/python/lib/python2.5/site-packages/mod_python --with-apxs=/usr/local/apache/bin/apxs --with-python=/usr/local/python/bin/python2.5
我将我的测试文件 (mptest.py) 复制到 htdocs/test 文件夹,以下是我的 mptest:
from mod_python import apache
def handler(req):
req.log_error('handler')
req.content_type = 'text/plain'
req.send_http_header()
req.write('mptest.py\n')
return apache.OK
我也将我的 .htaccess 复制到 htdocs/test 文件夹,以下是我的 .htaccess:
AddHandler mod_python .py
PythonHandler mptest
PythonDebug On
我收到错误,Internet 服务器错误,以下是我的 apache 错误日志:
[Wed Nov 16 17:10:48 2011] [notice] mod_python: Creating 8 session mutexes based on 256 >max processes and 0 max threads.
[Wed Nov 16 17:10:48 2011] [notice] mod_python: using mutex_directory /tmp
[Wed Nov 16 17:10:48 2011] [notice] Apache/2.2.21 (Unix) PHP/5.2.17 mod_python/3.3.2-dev-20080819 Python/2.5.4 configured -- resuming normal operations
[Wed Nov 16 17:11:00 2011] [error] make_obcallback: could not import mod_python.apache.\n
ImportError: No module named mod_python.apache
[Wed Nov 16 17:11:00 2011] [error] make_obcallback: Python path being used "['/Library/Python/2.5/site-packages/setuptools-0.6c11-py2.5.egg', '/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python25.zip', '/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5', '/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-dynload', '/Library/Python/2.5/site-packages', '/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python/PyObjC', '/System/Library/Frameworks/Python.framework/Versions/2.5/Extras/lib/python/wx-2.8-mac-unicode']".
[Wed Nov 16 17:11:00 2011] [error] get_interpreter: no interpreter callback found.
[Wed Nov 16 17:11:00 2011] [error] [client ::1] python_handler: Can't get/create interpreter., referer: http://localhost/test/
我的 mod_python.so 链接到我不想要的系统 python,证明如下。我怎样才能将它链接到我安装的 python?
Mac-Pro:~ user$ otool -L /usr/local/apache/modules/mod_python.so
/usr/local/apache/modules/mod_python.so:
/System/Library/Frameworks/Python.framework/Versions/2.5/Python (compatibility version 2.5.0, current version 2.5.4)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.0.0) /usr/lib/libgcc_s.1.dylib (compatibility version 1.0.0, current version 625.0.0)
我只想在我的网络浏览器中显示 mptest.py,有人可以帮我吗?
我知道 mod_wsgi 是比 mod_python 更好的选择,但我需要让 mod_python 工作。我稍后会尝试 mod_wsgi。
谢谢。
请您参考如下方法:
恐怕我帮不了 mod_python。我们已将所有代码从 mod_python 切换为支持 mod_wsgi,并且再也没有回头。
mod_wsgi 有一些很棒的特性 —— 更不用说轻松创建调试/诊断代码的能力,而不是完全从 python 运行而无需运行 Apache 的麻烦(这开启了运行调试器和直接打印到 stdout 的能力)。
如果您选择 wsgi,请考虑使用 webob 来管理请求/响应对象。
这里是你如何在 mod_wsgi 中编写你的例子
# --------- EXAMPLE ----------
from webob import Response
def application(environ, start_response):
res = Response()
res.content_type = 'text/plain'
res.body = "mptest.py\r\n"
return res(environ, start_response)
祝你好运。
