python - 执行格式错误 : apache + mod_wsgi on Mac OS X Lion

标签 python osx-lion mod-wsgi web2py

我正在尝试在运行 Lion (Mac OS X 10.7.3) 的 iMac 上使用 Python 2.7、apache、mod_wsgi 和 web2py 设置 Web 开发环境。

我下载并安装了 mod_wsgi v. 3.3 (./configure; make; sudo make install)

它安装在/usr/libexec/apache2 中。一切看起来都很合理:

07:49 PM ~ [541] otool -L /usr/libexec/apache2/mod_wsgi.so 
/usr/libexec/apache2/mod_wsgi.so:
    /Library/Frameworks/Python.framework/Versions/2.7/Python (compatibility version 2.7.0, current version 2.7.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
    /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 635.19.0)

07:55 PM ~ [542] file /usr/libexec/apache2/mod_wsgi.so 
/usr/libexec/apache2/mod_wsgi.so: Mach-O universal binary with 2 architectures
/usr/libexec/apache2/mod_wsgi.so (for architecture x86_64): Mach-O 64-bit bundle x86_64
/usr/libexec/apache2/mod_wsgi.so (for architecture i386):   Mach-O bundle i386

我在所有 LoadModule 指令之后向/private/etc/apache2/httpd.conf 添加了几个配置指令。

LoadModule wsgi_module libexec/apache2/mod_wsgi.so
WSGIScriptAlias / /Library/WebServer/Documents

我重新启动了 apache 守护进程。 Apache 日志看起来不错:

[2012 年 2 月 9 日星期四 19:19:15] [通知] Apache/2.2.21 (Unix) mod_ssl/2.2.21 OpenSSL/0.9.8r DAV/2 mod_wsgi/3.3 Python/2.7.2 配置 -- 恢复正常操作

我把这个文件放到/Library/WebServer/Documents 文件夹中:

def application(environ, start_response):
    status = '200 OK'
    output = 'Hello World!'

    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]

    start_response(status, response_headers)

    return [output]

我从我的浏览器执行它:http://192.168.1.2/test.py

我收到“500 内部服务器错误”的响应。

我的服务器日志说:

[Thu Feb 09 20:12:10 2012] [error] [client 192.168.1.2] (8)Exec format error: exec of '/Library/WebServer/Documents/test.py' failed
[Thu Feb 09 20:12:10 2012] [error] [client 192.168.1.2] Premature end of script headers: test.py
[Thu Feb 09 20:12:10 2012] [error][client 192.168.1.2] mod_wsgi (pid=4251): Target WSGI script '/Library/WebServer/Documents/favicon.ico' does not contain WSGI application 'application'.

经过大量搜索,我无法找出原因。我什至在文档文件夹中插入了一个 favicon.ico。这导致它被记录下来:

[Thu Feb 09 19:15:44 2012] [error] [client 192.168.1.2] (8)Exec format error: exec of '/Library/WebServer/Documents/test.py' failed
[Thu Feb 09 19:15:44 2012] [error] [client 192.168.1.2] Premature end of script headers: test.py
[Thu Feb 09 19:15:46 2012] [error] [client 192.168.1.2] mod_wsgi (pid=4135): Target WSGI script '/Library/WebServer/Documents/favicon.ico' does not contain WSGI application 'application'.

如有任何帮助,我们将不胜感激。

最佳答案

您将 WSGIScriptAlias 映射到 DocumentRoot 目录本身并且目录上没有尾部斜杠,这可能意味着它试图以某种错误的方式加载目录作为 WSGI 脚本文件并且失败了。除非您没有包含所有 mod_wsgi 配置,否则脚本文件消息过早结束会令人困惑。该消息表明您正在使用未显示的 mod_wsgi 守护程序模式,或者某些内容实际上被解释为 CGI 脚本。

无论如何,如果您想在 DocumentRoot 中删除 .py 文件,您可能想要做的是删除 WSGIScriptAlias 然后添加:

<Directory /Library/WebServer/Documents>
AddHandler wsgi-script .py
Options +ExecCGI
</Directory>

这意味着“http://192.168.1.2/test.py”应该可以工作,而且您仍然可以将其他静态文件放到该目录中,它们将被提供。

确保你去复习:

http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide

关于python - 执行格式错误 : apache + mod_wsgi on Mac OS X Lion,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9222249/

相关文章:

python - 找不到 WSGI 应用程序 - 404

python - 如何在 python(可导入)脚本中只公开几个 'names'(变量/函数)

python - 你如何关闭 python 列表理解中的文件?

objective-c - 更改标准“关于”面板的大小

python 2.7 与 mac osx 狮子 : fn + delete and control + r

ios - Snow Leopard vs. Lion上的iOS Dev:我会失去任何功能吗?

python - 将 apache2 摘要认证信息传递给 mod_wsgi 运行的 wsgi 脚本

django - 在 apache 配置中使用 WSGIApplicationGroup %{GLOBAL} 的问题

python - 构建 '_mysql' 扩展错误 : Unable to find vcvarsall. bat

python - 思想树冠 : Issue Using pyhdf to read and HDF4 file