Django WSGI Apache VirtualHost,返回404错误

7
  • Ubuntu 12.04 - Ubuntu 12.04
  • Apache 2.2.2 - Apache 2.2.2
  • Python 3.2.3 - Python 3.2.3
  • Django 1.6.1 - Django 1.6.1

我按照在Ubuntu 12.04上使用mod_wsgi部署Django应用的指导设置了Apache和Django的WSGI。但是,虚拟主机域现在返回了一个令人困惑的404错误:

[Mon Dec 16 19:53:49 2013] [error] [client 127.0.0.1] File does not exist: /etc/apache2/htdocs

有点令人困惑的是...那个目录根本不存在。 VirtualHost 设置在 /var/www/main,配置文件在 /var/apache2/sites-available/default 中看起来像这样:

<VirtualHost *:80>
        ServerName 127.0.0.1
        ServerAlias coral
        WSGIScriptAlias / /home/dfy/code/djdev/djdev/wsgi.py
</VirtualHost>

我在编辑后重新加载了Apache。那个wsgi文件是Django在我开始项目时生成的。我根据教程进行了修改,以符合所需内容:

import os
import sys # added from tutorial
sys.path.append('/home/dfy/code/djdev/djdev/') # added from tutorial
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "djdev.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

在我看来,这一切似乎应该可以工作,但Apache正在寻找一个不存在的目录(/etc/apache2/htdocs)中的静态文件。我已经搜索了几个小时,试图弄清楚这里出了什么问题,但没有找到答案。出于一时兴起,我尝试了chmod 777 wsgi.py,但这并没有改变任何东西,所以我确定这不是权限问题。我真的不知道出了什么问题。

2个回答

4

但是Apache在一个不存在的目录中寻找静态文件。

我不确定这里发生了什么,但我怀疑您的主配置不正确。如果您没有提供DOCUMENT_ROOT,Apache将在默认位置htdocs中查找它。

根据 Django部署文档,您需要为您的虚拟主机提供以下配置。您应该根据您的设置更改路径:

Alias /robots.txt /path/to/mysite.com/static/robots.txt

Alias /favicon.ico /path/to/mysite.com/static/favicon.ico

AliasMatch ^/([^/]*\.css) /path/to/mysite.com/static/styles/$1

Alias /media/ /path/to/mysite.com/media/
Alias /static/ /path/to/mysite.com/static/

<Directory /path/to/mysite.com/static>
    Order deny,allow
    Allow from all
</Directory>

<Directory /path/to/mysite.com/media>
    Order deny,allow
    Allow from all
</Directory>

WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py
WSGIPythonPath /path/to/mysite.com

<Directory /path/to/mysite.com/mysite>
    <Files wsgi.py>
        Order deny,allow
        Require all granted
    </Files>
</Directory>

请使用官方文档,该文档针对特定的Django版本进行更新。

谢谢!那帮了我解决问题。我尝试过官方文档,但有时候他们好像漏掉了一些东西,我就无法弄清楚,只能去其他地方查找来填补空白。 - Zamphatta

2
也许你遗漏了什么: 在我的虚拟主机配置中,我有以下内容:
<virtualhost *:80>
...
Alias /static/ /the/path/to/static/
<Location "/static/">
        Options -Indexes
</Location>
...

</virtualhost>

我希望你能帮助到您


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接