【问题标题】:Django application not visibleDjango 应用程序不可见
【发布时间】:2025-12-11 12:15:01
【问题描述】:

我第一次尝试在Ubuntu 12.04 VM 上使用mod_wsgiApache 部署一个Django 应用程序。我一直在关注几个教程,尤其是Ayman Farhat blog,这个excellent YouTube video,当然还有官方的 Django 文档

这是在earlier question I posted here 之后,想知道为什么我的 Django 调查在我将其上传到 /var/www/ 时并不能正常工作(脸红!)我一直在根据答案调查mod_wsgi

我不确定我错过了什么阶段。该项目能够通过python manage.py runserver 在服务器上启动而没有错误。我还运行了python manage.py collectstatic,没有任何错误。

然后我用

重新启动Apache
sudo service apache2 restart

但是,当我转到 URL http://phaedrus.scss.tcd.ie/bias_experiment/surveythree/ 时,我希望看到调查,那里什么也没有。我只看到标准的 404 错误。

我真的不知道接下来我要做什么或为什么这不起作用。

以下是我的设置以及到目前为止我尝试过的内容。

注意:我有一个在 Pydev 中创建的 Bias_Experiment Django 项目。它在src 文件夹中包含三个应用程序。

  • 调查(我的工作项目)
  • 民意调查(我正在关注的教程)
  • bias_experiment(带有我的设置文件等的根应用程序)

我的项目结构

我的虚拟主机位于/etc/apache2/sites-available/bias_experiment

<VirtualHost *:80>
ServerAdmin admin@email.com
ServerName kdeg-vm-18.scss.tcd.ie
ServerAlias http://collegeserver.ie/bias_experiment
WSGIScriptAlias / var/www/bias_experiment/src/bias_experiment/index.wsgi

Alias /static/ /var/www/bias_experiment/src/bias_experiment/static/
<Location "/static/">
    Options -Indexes
</Location >
</VirtualHost >

我的 WSGI 文件位于/var/www/bias_experiment/src/bias_experiment/index.wsgi

import os
import sys
import site

# Add the site-packages of the chosen virtualenv to work with
site.addsitedir('~/var/www/bias_experiment/lib/python2.7/site-packages')

# Add the app's directory to the PYTHONPATH
sys.path.append('/var/www/bias_experiment')
sys.path.append('/var/www/bias_experiment/src/bias_experiment')

os.environ['DJANGO_SETTINGS_MODULE'] = 'bias_experiment/src/bias_experiment.settings'

# Activate your virtual env
activate_env=os.path.expanduser("~/var/www/bias_experiment/bin/activate_this.py")
execfile(activate_env, dict(__file__=activate_env))

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

我的网址格式来自bias_experiment/src/bias_experiment/urls.py

urlpatterns = patterns('',
    url(r'^polls/', include('polls.urls', namespace="polls")),
    url(r'^admin/', include(admin.site.urls)),        
    url(r'^surveythree/$', SurveyWizard.as_view([SurveyForm1, SurveyForm2, SurveyForm3, SurveyForm4, SurveyForm5])),   
)

【问题讨论】:

    标签: django apache python-2.7 mod-wsgi


    【解决方案1】:

    您要在浏览器中访问的地址与您的 Apache 配置中的 ServerName 或 ServerAlias 指令不匹配,因此虚拟主机不会知道响应该请求。

    请注意,ServerAlias 应该类似于 ServerName - 主机名,而不是 URL,没有 http 前缀或路径。另请注意,如果您需要该虚拟主机响应多个主机名,则可以为 ServerAlias 设置多个值。

    如果您希望 Django 应用程序在 /bias_experiment 下提供服务,那应该是 WSGIScriptAlias 的一部分。

    应该是这样的:

    ServerAlias phaedrus.scss.tcd.ie
    WSGIScriptAlias /bias_experiment /var/www/bias_experiment/src/bias_experiment/index.wsgi
    

    我也对代码的位置感到困惑。是在 /var/www/... 还是 /home/whoever/var/www 中?您的 wsgi 文件指的是后者,但 Apache conf 有前者。

    接下来,virtualenv 应该负责设置所有 Python 路径。因此,由于您正在运行激活脚本,您可以删除修改 sys.path 和 site.addsitedir 的行。尽管您可能需要保留添加src 目录的那个。

    另一个问题是您的 DJANGO_SETTINGS_MODULE。它应该是一个 Python 模块,而不是一个文件路径——实际上你所拥有的是它们两者之间的交叉。由于src 位于 Python 路径上,因此您只需将其设置为 'bias_experiment.settings'。

    【讨论】:

    • 感谢您的帮助 ;-) 我不知道我应该访问的地址应该与服务器名称或别名匹配,一旦我进行其他更改,我将尝试它们的变体。
    • 我已按照您的指示更新了 ServerAlias 和 WSGIScriptAlias。我的代码在/var/www/ 而不是`~/var/www/ 我也改变了这个。我还进行了其他更改。
    • 进行这些更改后,我仍然没有看到想要的结果。我托管在一个已通过phaedrus.scss.tcd.ie/bias_experiment 公开的私有虚拟机上。我认为这种混乱是问题的根源。既然这些其他问题已经解决,我认为最好发布另一个问题?
    • 我会尽量正确,因为它回答了多个问题并发布了不同的问题。感谢您的帮助
    最近更新 更多