Django with apache and wsgi throws ImportError

2019-08-15 04:29发布

问题:

I'm trying to deploy my Django app to an Apache server with no luck. I succeeded with the WSGI sample application, and tried to host an empty Django project. While it works properly with the manage.py runserver, it throws the following error when using 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

My wsgi.py is the following:

"""
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()

I have a wsgi.conf in the conf.d library for apache:

<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

[SOLVED] Thanks, I started all over again, made the suggested modifications to my configuration files, and now it's working. I couldn't flag both suggestions correct, but I think both of them were necessary and I had a third (fourth, fifth...) bug to, which went away after reinstallation.

回答1:

It looks like you've been using an old guide for setting up apache2 / wsgi. I'd recommend using the official guide at https://code.google.com/p/modwsgi/wiki/InstallationInstructions

Anyway, your specific problem is that the wsgi application isn't picking up the python path correctly. Change you VirtualHost conf to something like this

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


回答2:

Your settings file is at /usr/local/www/django/myapp/settings.py, but you've set the PythonPath to /usr/local/www/django/myapp and then set DJANGO_SETTINGS_MODULE to "myapp.settings" - that would only be suitable if settings was in myapp/myapp/settings. Drop one of those references to "myapp".