16

我正在与我的托管服务提供商合作以启动并运行一个 Django 应用程序,但我们都没有非常有经验,而且我们基本上已经走到了完全的死胡同。

我没有直接访问 conf 文件的权限,但我是这样描述它的内容的:

<IfModule mod_wsgi.c>
WSGIScriptAlias /fredapp/ /home/fred/public_html/cgi-bin/fredapp/apache/django.wsgi
WSGIDaemonProcess fred threads=15 display-name=%{GROUP} python-path=/home/fred/public_html/cgi-bin/fredapp/apache/
WSGIProcessGroup fred
WSGIApplicationGroup %{GLOBAL}
</IfModule>

Alias /robots.txt /home/fred/public_html/fred-site/robots.txt
Alias /favicon.ico /home/fred/public_html/fred-site/favicon.ico

Alias /settings/media/ /home/fred/public_html/fred-site/media/

我的“django.wsgi”脚本没什么特别的:

import os, sys
sys.path.append('/home/fred/public_html/cgi-bin/')
sys.path.append('/home/fred/public_html/cgi-bin/fredapp/')
os.environ['DJANGO_SETTINGS_MODULE'] = 'fredapp.settings'

import django.core.handlers.wsgi

application = django.core.handlers.wsgi.WSGIHandler()

所以我的理解是,所有这一切都意味着如果对 domain.com/fredapp/ 的请求进入,它应该通过 django.wsgi 移交给应用程序。但是,我得到的唯一回应是:

[Fri Jan 22 18:46:08 2010] [error] [client xx.xxx.xx.xx] File does not exist: /home/fred/public_html/domain.com/500.shtml
[Fri Jan 22 18:46:08 2010] [error] [client xx.xxx.xx.xx] mod_wsgi (pid=26760): Exception occurred processing WSGI script '/home/fred/public_html/cgi-bin/fredapp/apache/django.wsgi'.
[Fri Jan 22 18:46:03 2010] [error] [client xx.xxx.xx.xx] File does not exist: /home/fred/public_html/domain.com/404.shtml
[Fri Jan 22 18:46:03 2010] [error] [client xx.xxx.xx.xx] File does not exist: /home/fred/public_html/domain

这是在 Linux 上的 Apache 下运行的。我尝试在服务器上的 Python 解释器中运行 .wsgi 脚本的每一行,但它们都没有返回任何错误。我也尝试了这个sys.stdout = sys.stderr技巧,但没有得到比上面更多的输出。该文件不存在错误与站点的其余设置有关,并且在任何请求时都会发生。我还没有正确完成所有设置(错误页面和索引页面等),因为我只是想让应用程序本身运行。

我已经在我自己的机器上在 Apache 下启动并运行了这个应用程序,虽然不是在守护程序模式下,但它是我的第一个 Django 应用程序,我认为我的托管服务提供商以前从未配置过,所以我们正在飞行小盲人。如果有人有任何建议,我将不胜感激。谢谢!

4

3 回答 3

22

如果引用的配置是您正在使用的,则错误实际上是相当明显的。你有:

WSGIDaemonProcess fred threads=15 display-name=%{GROUP} python-path=/home/fred/public_html/cgi-bin/fredapp/apache/
WSGIProcessGroup scratchf

它应该是:

WSGIDaemonProcess fred threads=15 display-name=%{GROUP} python-path=/home/fred/public_html/cgi-bin/fredapp/apache/
WSGIProcessGroup fred

即进程组的名称必须匹配。

您应该已经看到一条错误消息:

No WSGI daemon process called 'scratchf' has been configured

这可能在记录的错误之前:

Exception occurred processing WSGI script

这就是为什么提供所有错误日志消息并且不要假设它们不相关的原因很重要。

或者,您引用的配置与您正在使用的配置不同或不是所有配置。


更新 1

看起来您可能在 Apache 中启用了 ErrorDocument 指令,以将错误重定向到特定 URL。但是,因为您已经将 Django 安装在 Web 服务器的根目录,并且没有排除那些错误 URL 被传递给 Django,所以当生成错误时,Django 会获取错误文档的重定向,但它无法解析 URL 并随后生成 404。因为 Apache 看到 404 错误页面重定向,所以它返回一个 500 默认错误页面。最终结果是真正的原始错误和任何信息都丢失了。

因此,进入 Apache 配置并注释掉 ErrorDocument 指令。


更新 2

将配置更改为:

WSGIScriptAlias /fredapp /home/fred/public_html/cgi-bin/fredapp/apache/django.wsgi

您不应该在线上的第二个值上有斜杠。错过了您实际上是在尝试挂载在子 URL 而不是 Web 服务器的根目录。

于 2010-01-22T10:12:25.703 回答
1

您的起始目录是否可能不是项目所在的一个?

今天我也在设置 Apache+mod_wsgi+Django 应用程序并在添加到 django.wsgi 之后:

 os.chdir('/home/user/my_django_project')

一切都开始像魅力一样发挥作用。

于 2010-01-22T00:02:48.763 回答
0

当运行 Apache 的用户没有读取文件的权限时,我们遇到了同样的错误。

于 2010-01-22T08:32:31.150 回答