【问题标题】:Django with apache and wsgi throws ImportError带有 apache 和 wsgi 的 Django 抛出 ImportError
【发布时间】:2014-05-23 11:36:52
【问题描述】:

我正在尝试将我的 Django 应用程序部署到 Apache 服务器,但没有成功。我成功地使用了 WSGI 示例应用程序,并尝试托管一个空的 Django 项目。虽然它与 manage.py 运行服务器一起正常工作,但在使用 apache 时会引发以下错误:

[notice] Apache/2.2.22 (Debian) PHP/5.4.4-14+deb7u9 mod_python/3.3.1 Python/2.7.3 mod_wsgi/2.7 configured -- resuming normal operations
[error] [client x.x.x.x] mod_wsgi (pid=8300): Exception occurred processing WSGI script '/usr/local/www/django/myapp/wsgi.py'.
[error] [client x.x.x.x] Traceback (most recent call last):
[error] [client x.x.x.x]   File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 187, in __call__
[error] [client x.x.x.x]     self.load_middleware()
[error] [client x.x.x.x]   File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 44, in load_middleware
[error] [client x.x.x.x]     for middleware_path in settings.MIDDLEWARE_CLASSES:
[error] [client x.x.x.x]   File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 54, in __getattr__
[error] [client x.x.x.x]     self._setup(name)
[error] [client x.x.x.x]   File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 49, in _setup
[error] [client x.x.x.x]     self._wrapped = Settings(settings_module)
[error] [client x.x.x.x]   File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 132, in __init__
[error] [client x.x.x.x]     % (self.SETTINGS_MODULE, e)
[error] [client x.x.x.x] ImportError: Could not import settings 'myapp.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named myapp.settings

我的 wsgi.py 如下:

"""
WSGI config for myapp project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/1.6/howto/deployment/wsgi/
"""

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

我在 apache 的 conf.d 库中有一个 wsgi.conf:

<VirtualHost *:80>
ServerName myapp.example.com
ServerAlias myapp
ServerAdmin admin@example.com
DocumentRoot /var/www

<Directory /usr/local/www/django>
    Order allow,deny
    Allow from all
</Directory>

WSGIDaemonProcess myapp processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup myapp

WSGIScriptAlias /myapp /usr/local/www/django/myapp/wsgi.py
LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so

</VirtualHost>

WSGIPythonPath /usr/local/www/django/myapp

[已解决] 谢谢,我重新开始,对我的配置文件进行了建议的修改,现在它可以工作了。我无法将这两个建议都标记为正确,但我认为它们都是必要的,而且我遇到了第三个(第四个、第五个......)错误,重新安装后就消失了。

【问题讨论】:

  • 你的应用布局是什么?设置文件到底在哪里?
  • 应用布局是django生成的。
  • 那么 settings.py 的完整文件路径到底是什么?
  • 如果您没有使用 mod_python,请将其卸载。它会阻止 mod_wsgi 中的某些东西正常工作/
  • @AndrasBalázsLajtha Google 搜索“apache2 卸载 mod_python”

标签: python django apache wsgi


【解决方案1】:

您似乎一直在使用旧指南来设置 apache2 / wsgi。我建议使用https://code.google.com/p/modwsgi/wiki/InstallationInstructions的官方指南

无论如何,您的具体问题是 wsgi 应用程序没有正确选择 python 路径。将您的 VirtualHost conf 更改为类似的内容

<VirtualHost *:80>
    ServerName myapp.example.com
    ServerAlias myapp
    ServerAdmin admin@example.com

    DocumentRoot /usr/local/www/django/myapp
    WSGIDaemonProcess myapp processes=2 threads=15 display-name=%{GROUP} python-path=/usr/local/www/django/myapp:/path/to/system/python/site-packages
    WSGIProcessGroup myapp
    WSGIScriptAlias / /usr/local/www/django/myapp/wsgi.py

    <Directory /usr/local/www/django/myapp>
        <Files wsgi.py>
            Order allow,deny
            Allow from all
        </Files>
    </Directory>
</VirtualHost>

【讨论】:

  • 我决定重新开始,将添加您建议的修改,看看它是如何工作的。
  • 试试 Graeme Dumpleton 在他对您的问题的评论中提出的建议。他是 mod_wsgi 的开发者,所以他比任何人都知道。
【解决方案2】:

您的设置文件位于 /usr/local/www/django/myapp/settings.py,但您已将 PythonPath 设置为 /usr/local/www/django/myapp,然后将 DJANGO_SETTINGS_MODULE 设置为“myapp.settings " - 只有当设置在 myapp/myapp/settings 中时才适用。删除其中一个对“myapp”的引用。

【讨论】:

  • 试过但没有成功:(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-03
  • 2017-03-31
  • 1970-01-01
  • 2016-12-23
  • 2016-12-19
  • 2013-08-23
  • 2012-10-14
相关资源
最近更新 更多