【问题标题】:correct way to add mod_wsgi module to apache将 mod_wsgi 模块添加到 apache 的正确方法
【发布时间】:2013-05-15 19:57:42
【问题描述】:

我正在尝试在 mac osx 上的默认 apache 服务器中安装 mod_wsgi 模块。我正在关注this guy's tutorial. 我已经到了将模块添加到 apache 配置的部分,我插入了这两行:

$ sudo nano /private/etc/apache2/httpd.conf
...
LoadModule wsgi_module libexec/apache2/mod_wsgi.so
WSGIScriptAlias / /Library/WebServer/Documents/
...

回到我输入的终端:

sudo /usr/sbin/apachectl restart

当我在浏览器中访问 localhost/testpy.py 时,我收到一条错误消息,提示无法连接到本地主机。这是我的testpy.py 文件:

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]

编辑

当我添加这两行代码时,apache 错误日志不会产生任何内容。当我把它们拿出来并转到一个我知道在错误日志中得到它之前有效的 URL 时:

caught SIGTERM, shutting down
Init: Session Cache is not configured [hint: SSLSessionCache]
httpd: Could not reliably determine the server's fully qualified domain name, using my-machine.local for ServerName
Digest: generating secret for digest authentication ...
Digest: done
Apache/2.2.22 (Unix) DAV/2 PHP/5.3.15 with Suhosin-Patch mod_ssl/2.2.22 OpenSSL/0.9.8r configured -- resuming normal operations

【问题讨论】:

  • 在浏览器中输入http://127.0.0.1/会发生什么?
  • 我认为您的教程让您误入歧途,您可能想找一个不同的...
  • @MattDMo 也许是……你能给我指一个不同的吗? http://127.0.0.1/ 产生同样的问题。
  • 我个人非常喜欢Django。设置起来非常简单,他们有一个很棒的教程来让一切顺利进行。最重要的是,它包含自己的开发服务器,所以一开始你甚至不需要 Apache/mod_wsgi。
  • 另外,请确保您已关注these instructions 在 OSX 上安装 mod_wsgi

标签: python macos apache


【解决方案1】:

很遗憾,您所遵循的教程没有帮助。 WSGI 并不是一种在浏览 Python 文件时只运行它们的方法:您已经可以直接使用 CGI 来做到这一点。使用 WSGI 的常规方法是创建一个应用程序,通过运行 wsgi 文件为特定前缀(可能为空)下的所有 URL 提供服务。

所以,你有三个问题。首先,您的 WSGIScriptAlias 需要指向一个实际文件,在本例中是您的 testpy.py 文件:

WSGIScriptAlias / /Library/WebServer/Documents/testpy.py

其次,该文件需要是有效的 Python:也就是说,它需要正确缩进,因为缩进在 Python 中很重要。

第三,因为别名指向/,那是你应该去的URL。

【讨论】:

  • 感谢您的帮助。我已将 testpy.py 添加到别名中,正确缩进了我的 python 代码,我将在浏览器中访问 localhost 和 127.0.0.1,但仍然出现“无法连接”。
【解决方案2】:

请注意,将 WSGIScriptAlias 设置为与 DocumentRoot 相同的目录是个坏主意。

实际的问题可能是你应该有:

WSGIScriptAlias / /Library/WebServer/Documents

即从目录路径中删除尾部斜杠。

您也许还应该阅读官方 mod_wsgi 文档:

将目标设为 WSGI 脚本文件的目录不一定是最合适的做法,因此请查看文档以了解其他选项。

使用一些随意的人的博客文章几乎永远不能很好地替代官方文档。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-23
    • 2011-08-30
    • 2018-12-21
    • 2022-12-23
    • 1970-01-01
    • 2020-10-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多