How to debug the main problems associated with configuring django to serve apache and mod-wsgi?

Usually, people have a problem setting up django to serve apache and mod-wsgi. A common symptom is “Import Error” ... for some reason (usually slightly different in each case) settings.py or similar does not import (see “Related” in the right column on this page for numerous examples!).

I read other questions on this topic, and none of them has a solution that works for my situation (one of them was the main incorrect description of the poster identified by the responder - I seem to have no this problem, others apply to using wsgi from other modules, etc.).

When apache / mod-wsgi fails to load, how can it be debugged?

What can you do to give you a better message than “Import Error”?

Obviously, I'm looking for a method that will determine what is wrong in my case. But I really would like to know how to approach debugging this kind of crashes: I seem to be unable to find information that this makes it fail.


In my case, I am trying to do what seems like a direct deployment of django with mod-wsgi - indeed from a book, just like a document, but getting an 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 do not understand why he can not find this module.

/home/cmc/src/cm_central/cm_central/settings.py exists, pythonn can be loaded without errors, and actually works OK with ./manage.py runserver .

Is it possible that there is some import error that occurs in the apache context that does not occur when I load it myself? I am wondering because of the words “Is there an import error in the settings file?” ... why is he asking about this? If an import error occurs, how would I debug it?

I have this in / etc / apache 2 / 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 is in wsgi.py: (I did not change it from what django created)

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

Full trace:

 [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 the answer can help me determine what is wrong, that would be great, but it's even better to answer the question "how to debug this." How to find out why settings.py is not loading?

+6
source share
6 answers

Finally, it will answer the high-level question: how to debug?

And it is so obvious:

print everything that matters from settings.py and look in the apache log to find out what you have and then compare it with what you get in development!

A related article offers this set of fingerprints in settings.py:

import sys, os

 print "__name__ =", __name__ print "__file__ =", __file__ print "os.getpid() =", os.getpid() print "os.getcwd() =", os.getcwd() print "os.curdir =", os.curdir print "sys.path =", repr(sys.path) print "sys.modules.keys() =", repr(sys.modules.keys()) print "sys.modules.has_key('mysite') =", sys.modules.has_key('mysite') if sys.modules.has_key('mysite'): print "sys.modules['mysite'].__name__ =", sys.modules['mysite'].__name__ print "sys.modules['mysite'].__file__ =", sys.modules['mysite'].__file__ print "os.environ['DJANGO_SETTINGS_MODULE'] =", os.environ.get('DJANGO_SETTINGS_MODULE', None) 

For my purposes, I also added DATABASE_URL and then EMAIL_HOST_USER etc.

The article also has a good explanation of how the whole startup process is going, what it costs.

Please note that it is a bit dated with django itself, so the last “alternative wsgi.py” that he came up with may not be as applicable, but of course, the debugging method.

+1
source

There may be different reasons for your problem.

- perhaps your wsgi daemon is running under a different user / group than during an interactive attempt using python manage.py runningerver. perhaps then the rights to your files will be set. perhaps try to use the same user online as for the daemon or vice versa. mod_wsgi has nice options, so you can run your daemons under individual users, which is good for security also because of the separation.

sys.path may be different (although it somehow looks correct in the configuration of your daemon), cwd may be different.

since you seem to be using virtualenv, you might also want to follow the special instructions for configuring Mod-wsgi and Virtualenvs

+1
source

You can 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) 

so that your Django project is in a python path?

If the answer could help me determine what happened, it would be great - but even better - this is the answer to "how to debug it." How to find out why settings.py is not loading?

It is difficult to answer this question if we do not know exactly what you have already tried. Based on the trace ( Is it on sys.path? Is there an import error in the settings file? ), I would say:

  • Make sure your django project base folder is in the python path (which I didn't read, you did, so I made the suggestion above)

  • Make sure there are no exceptions when loading your settings.py file (which you already made)

+1
source

First, to the real question here. How to get additional information about what exactly fails? Maybe I'm wrong, but I'm pretty sure that there are no tools / packages / logs or anything else that will give you more information about the problem than what you already have: tracing. Therefore, I assume that the only way to debug these errors is with the "trace" method ;-)

--- Now to the problem:

Perhaps this is a resolution problem. In WSGIDaemonProcess you do not specify any user or group, and perhaps apache is not allowed to read / execute files. Can you try adding the user and group to this line as follows:

  WSGIDaemonProcess cm-central.johalla.de user=<username> group=<username> python-path=/home/cmc/src/cm_central:/home/cmc/virtualenvs/cmc/lib/python2.7/site-packages 

Also, you are not using DocumentRoot. Maybe why he did not find the right path? In my vhosts, I always include DocumentRoot, which has the same path as Directory, but with a leading slash, in your case:

 <VirtualHost *:80> DocumentRoot /home/cmc/src/cm_central/cm_central/ 

Therefore, perhaps this solves your problem.

+1
source

The error message is already pretty obvious - mod_wsgi cannot find your settings file (line 2, os.environ.setdefault ), check your system path (I understand that Django can find your settings file when using runserver , but mod_wsgi cannot). To check your system path, you can modify the wsgi.py file and print / write sys.path at the very top of the file:

 import sys print sys.path 

In addition to the virtual applications pointed out by @Thomas Waldmann, you may also need to manually add the project directory to sys.path, as indicated here in the Django config docs . An example of the code I use is given below:

 ALLDIRS = ['/usr/local/pythonenv/assessments/lib/python2.6/site-packages'] import os import sys import site # from https://code.google.com/p/modwsgi/wiki/VirtualEnvironments sys.path.insert(0, '/var/www/assessments/assessments-main/') # settings.py file here sys.path.insert(1, '/var/www/assessments/') prev_sys_path = list(sys.path) for directory in ALLDIRS: site.addsitedir(directory) new_sys_path = [] for item in list(sys.path): if item not in prev_sys_path: new_sys_path.append(item) sys.path.remove(item) sys.path[:0] = new_sys_path os.environ['DJANGO_SETTINGS_MODULE'] = 'assessments-main.settings' import django.core.handlers.wsgi application = django.core.handlers.wsgi.WSGIHandler() 
+1
source

Can you try adding an empty __init__.py file to the cm_central directory? Depending on how the parameters are imported, this is necessary.

0
source

Source: https://habr.com/ru/post/977940/


All Articles