python - Django WSGI Apache VirtualHost,返回 404 错误

标签 python django apache ubuntu

  • Ubuntu 12.04
  • Apache 2.2.2
  • python 3.2.3
  • Django 1.6.1

我按照 Using mod_wsgi to Serve Applications on Ubuntu 12.04 的指示进行操作,用于在 Apache 上为我的 Django 设置 WSGI。问题是,VirtualHost 域现在返回一个令人困惑的奇怪 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,但这并没有改变任何东西,所以我确定这不是权限问题。我真的不知道出了什么问题。

最佳答案

but Apache is lookin' for static files in a non-existent directory

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

根据 django documentation on deployment ,您需要为虚拟主机提供以下配置。您可以更改适用于您的设置的路径:

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 版本的更改而更新的官方文档。

关于python - Django WSGI Apache VirtualHost,返回 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20624163/

相关文章:

python - 使用线程将 stdout 重定向到 Tkinter 文本小部件的问题

python - 编辑 Django 管理表单,以便只有员工用户才能更改用户密码

javascript - 如何防止 django-pipeline 中 javascript 函数的名称修改

python - Django 鹡鸰 : Is there any option to make wagtail fieldPanel disabled?

apache - 单节点集群(hadoop)中的最大容器是多少?

linux - 如何测试 503 自定义错误页面。

python - 检查 RDD 中是否存在值

python - 用函数初始化卡住的数据类属性

python - 如何在 m 组中对大小为 (m*n,) 的 numpy 数组求和?

Apache 无法启动 - ServerRoot 必须是有效目录并且无法找到指定的模块