Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug basic issues configuring django to be served with apache and mod-wsgi?

It is common that people have trouble setting up django to be serves by apache and mod-wsgi. The common symptom is "Import error" ... for some reason (generally slightly different in each case) settings.py or similar doesn't import (see "Related" in the right column on this page for numerous examples!).

I have read through the other questions on this topic, and none appear to have a solution that works for my situation (one was a basic misconfig by the poster identified by an answerer - I don't appear to have this issue, others apply to using wsgi from other modules etc).

When apache/mod-wsgi fails to load, how can you debug it?

What can you do to make something give you a better message than "Import error"?

Obviously, I'm looking for a method that will identify what's wrong in my case. But I really would like to know how to approach debugging this sort of failure: there seems to be no way that I can find to get information about what it is causing it to fail.


In my case, I am trying to do what appears to be a straightforwards django deploy with mod-wsgi - really by the book, the same as the doc, but get the error:

ImportError: Could not import settings 'cm_central.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named cm_central.settings

I can't see why it can't find this module.

/home/cmc/src/cm_central/cm_central/settings.py exists, can be loaded by pythonn without error, and in fact works OK with ./manage.py runserver.

Is it possible that there is some import error occurring in the context of apache, that doesn't occur when I load it myself? I'm wondering because of the words "Is there an import error in the settings file?" ... why does it ask that? If there were an import error, how would I debug it?

I have this in /etc/apache2/sites-enabled/cm-central:

<VirtualHost *:80>

  WSGIScriptAlias / /home/cmc/src/cm_central/cm_central/wsgi.py
  WSGIDaemonProcess cm-central.johalla.de python-path=/home/cmc/src/cm_central:/home/cmc/virtualenvs/cmc/lib/python2.7/site-packages
  WSGIProcessGroup cm-central.johalla.de

  <Directory /home/cmc/src/cm_central/cm_central>
    <Files wsgi.py>
      Order deny,allow
      Allow from all
    </Files>
  </Directory>

</VirtualHost>

And this in wsgi.py: (it I haven't modified it from what django generated)

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

from django.core.wsgi import get_wsgi_application
from dj_static import Cling

application = Cling(get_wsgi_application())

The full traceback is:

[Sun Nov 09 12:00:01 2014] [error] [client 192.168.122.40] mod_wsgi (pid=10273): Target WSGI script '/home/cmc/src/cm_central/cm_central/wsgi.py' cannot be loaded as Python module.
[Sun Nov 09 12:00:01 2014] [error] [client 192.168.122.40] mod_wsgi (pid=10273): Exception occurred processing WSGI script '/home/cmc/src/cm_central/cm_central/wsgi.py'.
[Sun Nov 09 12:00:01 2014] [error] [client 192.168.122.40] Traceback (most recent call last):
[Sun Nov 09 12:00:01 2014] [error] [client 192.168.122.40]   File "/home/cmc/src/cm_central/cm_central/wsgi.py", line 16, in <module>
[Sun Nov 09 12:00:01 2014] [error] [client 192.168.122.40]     application = Cling(get_wsgi_application())
[Sun Nov 09 12:00:01 2014] [error] [client 192.168.122.40]   File "/home/cmc/virtualenvs/cmc/lib/python2.7/site-packages/django/core/wsgi.py", line 14, in get_wsgi_application
[Sun Nov 09 12:00:01 2014] [error] [client 192.168.122.40]     django.setup()
[Sun Nov 09 12:00:01 2014] [error] [client 192.168.122.40]   File "/home/cmc/virtualenvs/cmc/lib/python2.7/site-packages/django/__init__.py", line 20, in setup
[Sun Nov 09 12:00:01 2014] [error] [client 192.168.122.40]     configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
[Sun Nov 09 12:00:01 2014] [error] [client 192.168.122.40]   File "/home/cmc/virtualenvs/cmc/lib/python2.7/site-packages/django/conf/__init__.py", line 46, in __getattr__
[Sun Nov 09 12:00:01 2014] [error] [client 192.168.122.40]     self._setup(name)
[Sun Nov 09 12:00:01 2014] [error] [client 192.168.122.40]   File "/home/cmc/virtualenvs/cmc/lib/python2.7/site-packages/django/conf/__init__.py", line 42, in _setup
[Sun Nov 09 12:00:01 2014] [error] [client 192.168.122.40]     self._wrapped = Settings(settings_module)
[Sun Nov 09 12:00:01 2014] [error] [client 192.168.122.40]   File "/home/cmc/virtualenvs/cmc/lib/python2.7/site-packages/django/conf/__init__.py", line 98, in __init__
[Sun Nov 09 12:00:01 2014] [error] [client 192.168.122.40]     % (self.SETTINGS_MODULE, e)
[Sun Nov 09 12:00:01 2014] [error] [client 192.168.122.40] ImportError: Could not import settings 'cm_central.settings' (Is it on sys.path? Is there an import error in the settings file?): No module named cm_central.settings
[Sun Nov 09 12:04:06 2014] [notice] Graceful restart requested, doing restart
[Sun Nov 09 12:04:06 2014] [notice] Apache/2.2.22 (Debian) PHP/5.4.4-14+deb7u14 mod_wsgi/3.3 Python/2.7.3 configured -- resuming normal operations
[Sun Nov 09 21:34:15 2014] [error] Not Found: /

If an answer could help me spot what's wrong, that'd be great - but even better is an answer to "how to debug this". How does one find out why the settings.py won't load?

like image 540
GreenAsJade Avatar asked Nov 09 '14 12:11

GreenAsJade


People also ask

How do I create a WSGI in Django?

The startproject command creates a file <project_name>/wsgi.py that contains such an application callable. It's used both by Django's development server and in production WSGI deployments. WSGI servers obtain the path to the application callable from their configuration.

Does Apache support WSGI?

mod_wsgi is an Apache module which can host any Python WSGI application, including Django. Django will work with any version of Apache which supports mod_wsgi. The official mod_wsgi documentation is your source for all the details about how to use mod_wsgi.

What is debug mode in Django?

One of the main features of debug mode is the display of detailed error pages. If your app raises an exception when DEBUG is True, Django will display a detailed traceback, including a lot of metadata about your environment, such as all the currently defined Django settings (from settings.py).


1 Answers

You could try adding this to your wsgi.py:

path = os.path.join(os.path.dirname(__file__), "..") # Adapt the path here to match the root of your django project
if path not in sys.path:
    sys.path.append(path)

to ensure that your Django project is in the python path ?

If an answer could help me spot what's wrong, that'd be great - but even better is an answer to "how to debug this". How does one find out why the settings.py won't load?

It's hard to answer that question if we don't know exactly what you have already tried. Based on the traceback (Is it on sys.path? Is there an import error in the settings file?), I'd say :

  • Check that your django project base folder is in the python path (which I haven't read you have done, that's why I made the suggestion above)

  • Check that there are no exeptions raised when loading your settings.py file (which you already did)

like image 181
rparent Avatar answered Oct 15 '22 03:10

rparent