Mac上的Django:Apache有mod_wsgi配置问题

时间:2012-02-27 12:47:43

标签: django apache mod-wsgi

我正在尝试使用mod_wsgi在Apache上运行本地Django服务器。我在Mac上运行开箱即用的Apache。

hobbes3@hobbes3:~/Sites/mysite$ apachectl -v
Server version: Apache/2.2.21 (Unix)
Server built:   Nov 15 2011 15:12:57

Apache正确加载mod_wsgi。

hobbes3@hobbes3:~/Sites/mysite$ apachectl -t -D DUMP_MODULES | grep wsgi
Syntax OK
wsgi_module (shared)

在我的httpd.conf文件中,我加载了apache_django_wsgi.conf

WSGIDaemonProcess django
WSGIProcessGroup django

Alias /mysite/ "/Users/hobbes3/Sites/mysite/"
<Directory "/Users/hobbes3/Sites/mysite">
Order allow,deny
Options Indexes
Allow from all
IndexOptions FancyIndexing
</Directory>

WSGIScriptAlias /mysite "/Users/hobbes3/Sites/mysite/apache/django.wsgi"

<Directory "/Users/hobbes3/Sites/mysite/apache">
Allow from all
</Directory>

我的django.wsgi文件是

import os
import sys

paths = [ '/Users/hobbes3/Sites/mysite',
          '/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages',
]

for path in paths:
    if path not in sys.path:
        sys.path.append(path)

sys.executable = '/usr/local/bin/python'
os.environ['DJANGO_SETTINGS_MODULE'] = 'mysite.settings'

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

我也可以重启Apache而不会出现任何错误。但是当我尝试访问http://localhost/mysite时,我的浏览器显示我有500内部服务器错误。

我的Apache错误日志说(我截断了日期和时间):

mod_wsgi (pid=73970): Exception occurred processing WSGI script '/Users/hobbes3/Sites/mysite/apache/django.wsgi'.
Traceback (most recent call last):
  File "/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 250, in __call__
    self.load_middleware()
  File "/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/django/core/handlers/base.py", line 39, in load_middleware
    for middleware_path in settings.MIDDLEWARE_CLASSES:
  File "/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/django/utils/functional.py", line 276, in __getattr__
    self._setup()
  File "/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/django/conf/__init__.py", line 42, in _setup
    self._wrapped = Settings(settings_module)
  File "/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/django/conf/__init__.py", line 89, in __init__
    raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'mysite.settings' (Is it on sys.path?): No module named mysite.settings

我不知道为什么它无法导入mysite.settings,我是不是将其包含在sys.path中?

settings.py的完整路径为/Users/hobbes3/Sites/mysite/

1 个答案:

答案 0 :(得分:2)

您已在路径中添加了mysite,但您引用了mysite.settings。因此,Python正在中找到它在路径中找到的目录中的mysite模块。仅使用settings代替。