在 Apache 上设置 django 时出现问题

发布于 2024-09-14 14:11:58 字数 867 浏览 7 评论 0原文

这可能真的很简单。但我想我对 WSGI 和 Django 太陌生,无法自己获得它。我在 /var/www/mysite 中托管的 Ubuntu 虚拟机上有一个全新的闪亮 Django 项目。该项目是通过

django-admin startproject mysite

我遵循 WSGI 教程来进行设置的,因此我创建了一个 ./apache 文件夹,在其中放置了此文件 django.wsgi:

import os
import sys
os.environ['DJANGO_SETTINGS_MODULE'] = '/var/www/mysite/mysite.settings'

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

然后我将此配置行添加到 Apache 配置中:

WSGIScriptAlias / /var/www/mysite/apache/django.wsgi

当我尝试访问该网站时,没有返回任何内容。连接只是挂起。这是在我的 access.log 中:

192.168.2.116 - - [15/Aug/2010:14:09:02 -500] "GET / HTTP/1.1" 500 639 "-" "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8

所以它正在访问该网站。但有人什么也没做。 error.log 中没有错误。

有人有什么想法吗?

This is probably really simple. But I guess I'm too new to WSGI and Django to get it on my own. I have a brand new shiny Django project on a Ubuntu virtual machine hosted in /var/www/mysite. The project was created with

django-admin startproject mysite

I'm following a WSGI tutorial to get it set up, so I created an ./apache folder, inside of which I placed this file, django.wsgi:

import os
import sys
os.environ['DJANGO_SETTINGS_MODULE'] = '/var/www/mysite/mysite.settings'

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

I then added this config line to the Apache configuration:

WSGIScriptAlias / /var/www/mysite/apache/django.wsgi

When I try to hit the site, nothing is returned. The connection just hangs. This is in my access.log:

192.168.2.116 - - [15/Aug/2010:14:09:02 -500] "GET / HTTP/1.1" 500 639 "-" "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8

So it's hitting the site. But someone isn't doing anything. There are no errors in the errors.log.

Anyone have any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅忆 2024-09-21 14:11:58

原始问题中的明显错误是:

os.environ['DJANGO_SETTINGS_MODULE'] = '/var/www/mysite/mysite.settings'

需要是:

sys.path.append('/var/www')
sys.path.append('/var/www/mysite')

os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'

也就是说,Django 设置模块必须是 Python 包路径,而不是绝对文件系统路径。接受的答案甚至没有指出这一点,而只是提供了另一组可供使用的代码,没有任何解释。

此外,您需要设置Python模块搜索路径,即sys.path,以便Python可以找到该环境变量中列出的设置文件。

最后,如果您的浏览器挂起,您也做了其他错误的事情,因为您所做的应该导致返回错误,而不是浏览器挂起。 Apache 错误日志中也一定有错误,因此您要么查找错误的位置,要么不知道要查找什么。

The blatant mistake in original question is that:

os.environ['DJANGO_SETTINGS_MODULE'] = '/var/www/mysite/mysite.settings'

would need to be:

sys.path.append('/var/www')
sys.path.append('/var/www/mysite')

os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'

That is, Django settings module must be a Python package path, NOT an absolute file system path. The accepted answer doesn't even point that out and rather just gives another set of code to use with no explanation.

Further, you need to set Python module search path, ie., sys.path, so that Python can find your settings file listed in that environment variable.

Finally, if your browser was hanging you have done something else wrong as well, as what you did should have resulted in an error being returned and not the browser hanging. There would also have had to be an error in the Apache error log, so you are either looking in wrong place or don't know what to look for.

晒暮凉 2024-09-21 14:11:58

您是否缺少 Django 应用程序的路径?我的 app.wsgi 有这个:

import os, sys
sys.path.append('/usr/local/src/django-1.2') # django is outside default path
sys.path.append('/usr/local/src/django-photologue-2.2') # app i'm using
sys.path.append('/var/www/mysite')

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
# this is regular python import, so my settings are physically
# here: /var/www/mysite/settings.py

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

Aren't you missing path to your Django application? My app.wsgi has this:

import os, sys
sys.path.append('/usr/local/src/django-1.2') # django is outside default path
sys.path.append('/usr/local/src/django-photologue-2.2') # app i'm using
sys.path.append('/var/www/mysite')

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
# this is regular python import, so my settings are physically
# here: /var/www/mysite/settings.py

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文