apache django 和 mod_wsgi 的配置问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2587251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 19:00:29  来源:igfitidea点击:

Configuration problems with django and mod_wsgi

pythondjangoapachemod-wsgi

提问by Jimbo

I've got problems on getting django to work on apache 2.2 with mod_wsgi. Django is installed and mod_wsgi too. I can even see a 404 page when accessing the path and I can login to django admin. But if I want to install the tagging module I get the following error:

我在使用 mod_wsgi 让 django 在 apache 2.2 上工作时遇到了问题。安装了 Django 和 mod_wsgi。我什至可以在访问路径时看到 404 页面,并且可以登录到 django admin。但是如果我想安装标记模块,我会收到以下错误:

 Traceback (most recent call last):
   File "setup.py", line 49, in <module>
  version_tuple = __import__('tagging').VERSION
   File "/home/jim/django-tagging/tagging/__init__.py", line 3, in <module>
  from tagging.managers import ModelTaggedItemManager, TagDescriptor
   File "/home/jim/django-tagging/tagging/managers.py", line 5, in <module>
  from django.contrib.contenttypes.models import ContentType
   File "/usr/lib/python2.5/site-packages/django/contrib/contenttypes/models.py", line 1, in <module>
  from django.db import models
   File "/usr/lib/python2.5/site-packages/django/db/__init__.py", line 10, in <module>
  if not settings.DATABASE_ENGINE:
   File "/usr/lib/python2.5/site-packages/django/utils/functional.py", line 269, in __getattr__
  self._setup()
   File "/usr/lib/python2.5/site-packages/django/conf/__init__.py", line 40, in _setup
  self._wrapped = Settings(settings_module)
   File "/usr/lib/python2.5/site-packages/django/conf/__init__.py", line 75, in __init__
  raise ImportError, "Could not import settings '%s' (Is it on sys.path? Does it have syntax errors?): %s" % (self.SETTINGS_MODULE, e)
 ImportError: Could not import settings 'mysite.settings' (Is it on sys.path? Does it have syntax errors?): No module named mysite.settings

My httpd.conf:

我的 httpd.conf:

 Alias /media/ /home/jim/django/mysite/media/

 <Directory /home/jim/django/mysite/media>
  Order deny,allow
  Allow from all
 </Directory>

 Alias /admin/media/ "/usr/lib/python2.5/site-packages/django/contrib/admin/media/"

 <Directory "/usr/lib/python2.5/site-packages/django/contrib/admin/media/">
  Order allow,deny
  Allow from all
 </Directory>

 WSGIScriptAlias /dj /home/jim/django/mysite/apache/django.wsgi

 <Directory /home/jim/django/mysite/apache>
  Order deny,allow
  Allow from all
 </Directory>

My django.wsgi:

我的 django.wsgi:

import sys, os

sys.path.append('/home/jim/django')
sys.path.append('/home/jim/django/mysite')

os.chdir('/home/jim/django/mysite')
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'

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

I try to get this to work since a few days and have read several blogs and answers here on so but nothing worked.

几天以来,我尝试让它发挥作用,并且已经阅读了几个博客和答案,但没有任何效果。

Edit:

编辑:

Now I tried it with this blog postand my wsgi file now looks like this:

现在我用这篇博文尝试了它,我的 wsgi 文件现在看起来像这样:

import sys
sys.path.insert(0, '/home/jim/django/mysite')
sys.path.insert(0, '/home/jim/django')

import settings

import django.core.management
django.core.management.setup_environ(settings)
utility = django.core.management.ManagementUtility()
command = utility.fetch_command('runserver')

command.validate()

import django.conf
import django.utils

django.utils.translation.activate(django.conf.settings.LANGUAGE_CODE)

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

admin is still working, but I'm getting the same error when I try to install the tagging module.

admin 仍在工作,但是当我尝试安装标记模块时遇到了同样的错误。

采纳答案by Lakshman Prasad

First,

第一的,

  • Since your admin works, the setting with wsgi is good. Don't bother changing/editing it.
  • 由于您的管理员有效,因此 wsgi 的设置很好。不要打扰更改/编辑它。

To ensure that it is not a Apache/mod-wsgi setting problem, you can run the development server from the production machine

为确保不是Apache/mod-wsgi设置问题,可以从生产机器运行开发服务器

python manage.py runserver 0:8080

Then point your browser to

然后将您的浏览器指向

http://yoursite.com:8080/

You must see exactly the same behaviour.

您必须看到完全相同的行为。

Then,

然后,

For debugging this problem:

为了调试这个问题:

  • On the python shell on your server, try import tagging. Clearly, from your traceback, import taggingis where it is raising an error and thats why, settings cannot be imported.

  • Then, Just delete the package containing tagging, and do a fresh install by the following command, which knows how to install packages, well.

  • 在服务器上的 python shell 上,尝试import tagging. 显然,从您的回溯import tagging来看,它是引发错误的地方,这就是无法导入设置的原因。

  • 然后,删除包含标记的包,并通过以下命令进行全新安装,该命令知道如何安装包,好吧。

.

.

sudo pip install django-tagging

回答by Hank Gay

Have you read Graham Dumpleton's blog post on Django and WSGI? It does a pretty good job of describing some common configuration problems, and specifically touches on mysite.settingsvs. settings.

你读过 Graham Dumpleton关于 Django 和 WSGI博文吗?它在描述一些常见的配置问题方面做得非常好,特别涉及mysite.settingsvs. settings.

UPDATE: Please read Graham Dumpleton's excellent comments below.

更新:请阅读以下 Graham Dumpleton 的精彩评论。

UPDATE 2: As both Graham and becomingGuru have pointed out, the problem is not with WSGI at all. It is instead a problem with your installation of django-tagging. Take becomingGuru's advice and use pipto install django-tagging.

更新 2:正如 Graham 和 BeingingGuru 所指出的,问题根本不在于 WSGI。相反,这是您安装的问题django-tagging。接受成为Guru的建议并使用pip安装django-tagging.