执行格式错误:Mac OS X Lion上的apache + mod_wsgi

4 投票
1 回答
3170 浏览
提问于 2025-04-17 12:44

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

我下载并安装了mod_wsgi版本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

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

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

我重启了apache服务。apache的日志看起来不错:

[Thu Feb 09 19:19:15 2012] [notice] 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'.

任何帮助都将不胜感激。

1 个回答

1

你把 WSGIScriptAlias 映射到了 DocumentRoot 目录本身,而且没有在目录后面加上斜杠,这可能导致它试图以一种不正确的方式加载这个目录,结果失败了。出现“脚本文件过早结束”的消息让人困惑,除非你没有包含所有的 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

撰写回答