使用 Windows / Apache / Mod_WSGI 提供 Python 页面服务?

使用 Windows / Apache / Mod_WSGI 提供 Python 页面服务?

我正在尝试在 Windows 上设置 python / apache / WSGI。

我安装了以下软件,全部是 32 位和 vc9:

  • Apache 2.4
  • Python 2.7.9
  • 模块文件夹中的 Mod_WSGI.so。

http://本地主机/成功显示 Apache Haus 网页——Apache 正在运行并提供服务。

这是我的“Hello World”python 脚本:

def application(environ, start_response):
    status = '200 OK'
    output = 'This is my Website!'

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

    return [output]

这是我的 Apache 配置:

WSGIScriptAlias /wsgi "C:/wsgi_app/wsgi_app.py"
<Directory "C:/wsgi_app">
AllowOverride None
Options MultiViews
Require all granted
</Directory>

此版本将以纯文本形式显示文件,而不是解释它。

如果我将选项更改为无......

WSGIScriptAlias /wsgi "C:/wsgi_app/wsgi_app.py"
<Directory "C:/wsgi_app">
AllowOverride None
Options none
Require all granted
</Directory>

我收到 404 Not Foundhttp://localhost/wsgi

Apache 错误日志中似乎没有太多对故障排除有用的信息。

[Wed Feb 10 16:17:49.293987 2016] [wsgi:warn] [pid 4448:tid 336] mod_wsgi: Compiled for Python/2.7.9+.
[Wed Feb 10 16:17:49.293987 2016] [wsgi:warn] [pid 4448:tid 336] mod_wsgi: Runtime using Python/2.7.9.
[Wed Feb 10 16:17:49.325187 2016] [mpm_winnt:notice] [pid 4448:tid 336] AH00354: Child: Starting 64 worker threads.
[Wed Feb 10 16:17:52.538793 2016] [wsgi:error] [pid 4448:tid 1040] [client ::1:54402] Target WSGI script not found or unable to stat: C:/wsgi_app/wsgi_app.py

如果重要的话,“LoadModule access_compat_module modules/mod_access_compat.so”已被取消注释。

关于如何加载此页面/尝试其他 apache 配置更改,有什么想法吗?

我需要在某处更改 python 配置吗?

答案1

从您收到的错误中:

[wsgi:error] [pid 4448:tid 1040] [client ::1:54402] 目标 WSGI 脚本未找到或无法统计:C:/wsgi_app/wsgi_app.py

很有可能您在“C:/wsgi_app/”目录中没有名为“wsgi_app.py”的文件。

现在做什么呢Options Multiviews

以下是一段引文Apache 2.4 文档

MultiViews 的效果如下:如果服务器收到对 /some/dir/foo 的请求,如果 /some/dir 启用了 MultiViews,并且 /some/dir/foo 不存在,则服务器将读取目录以查找名为 foo.* 的文件,并有效地伪造一个类型映射来命名所有这些文件,为它们分配与客户端按名称请求其中一个文件时相同的媒体类型和内容编码。然后,它会选择与客户端要求最匹配的文件。

从上面的解释可以看出,当启用 Multiviews 选项时,由于名为 的文件wsgi_app.py不存在,服务器将尝试查找名为 的文件wsgi_app.py.*

我怀疑目录中有一个名为的文件wsgi_app.py.txt,并且显示了其内容。您可能在 Windows 中创建了一个文本文件来创建 wsgi_app.py 文件。在 Windows 中,当启用文件夹选项“隐藏已知文件类型扩展名”时,它将隐藏.txt文件的扩展名。因此,名为的文件wsgi_app.py.txt将仅显示为wsgi_app.py

当不再有 Multiviews 选项时,服务器正在寻找一个名为“wsgi_app.py”的文件,并返回错误,无法找到它。

相关内容