Django的与Apache和WSGI抛出导入错误(Django with apache and w

2019-10-20 04:28发布

我想我的Django应用程序部署到没有运气的Apache服务器。 我成功与WSGI示例应用程序,并尝试举办一个空的Django项目。 虽然它与manage.py runserver命令正常工作,它使用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()

我在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

[解决]谢谢,我又从头开始,取得了建议修改我的配置文件,现在它的工作。 我不能标志两项建议是正确的,但我认为他们两个是必要的,我还有第三个(第四,第五......)臭虫,其中又以重新安装后离开。

Answer 1:

它看起来像你一直使用的是旧指南建立的Apache2 / WSGI。 我推荐使用官方指南在https://code.google.com/p/modwsgi/wiki/InstallationInstructions

总之,您的具体问题是WSGI应用程序不正确拿起蟒蛇路径。 你虚拟主机的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>


Answer 2:

你的设置文件是在/usr/local/www/django/myapp/settings.py,但你已经设置的PYTHONPATH到/ usr /本地/网络/ Django的/ MyApp的,然后设置DJANGO_SETTINGS_MODULE为“myapp.settings” - 这只会是合适的,如果设置在MYAPP / MYAPP /设置。 拖放到“的myapp”的参考之一。



文章来源: Django with apache and wsgi throws ImportError