Django mod_wsgi: Exception occurred processing wsgi script

24,624

Solution 1

I have a checklist in these kind of scenarios:

  1. Execute a script testing the index.wsgi interface directly with python, correct errors until the output shows that all is ok. (see update)

  2. Check for permissions.

  3. Check for apache configuration.

Update:

Executing python my_project/index.wsgi directly shows nothing in console, this approach works on fcgi configuration but no in wsgi, you will need a script that create a test server and show posible errors on console, the example script is this:

test_server.py

#!/usr/bin/env python

from wsgiref.util import setup_testing_defaults
from wsgiref.simple_server import make_server
from my_project.index import application #importing the project's index.wsgi file
httpd = make_server('', 8000, application)
httpd.serve_forever()

Steps:

  • Run the test in console (even local)
  • Go to the browser and navigate to this server:port
  • see the logs in console

Recommendations:

  1. Check for you __init__.py either in the project folder and the settings folder

  2. Try to use relative paths in the settings and in the .wsgi file (see example)

  3. Verify the uppercase/lowecase in your names (probably changing "MY_PROJECT.settings" for "my_project.settings"

example:

import os, sys, site

CURRENT_DIRECTORY = os.path.dirname(os.path.realpath(__file__))
WORK_DIRECTORY = os.path.join(CURRENT_DIRECTORY, '..')

#Add the site-packages
site.addsitedir('/home/SERVER_USERNAME/.virtualenvs/histology_env/lib/python2.6/site-packages')

#activate_env=os.path.expanduser("~/.virtualenvs/histology_env/bin/activate_this.py")
#execfile(activate_env, dict(__file__=activate_env))

#adding the project to the python path
sys.path.append(WORK_DIRECTORY)
#adding the parent directory to the python path
sys.path.append(os.path.join(WORK_DIRECTORY, '..'))

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "my_project.settings")

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

Solution 2

I had this same thing happen to me a few days ago. I was reading all about it but this is how I finally fixed it all.

  1. Your django wsgi file should be a python file (wsgi.py), not an index file (index.wsgi). This is what mine looked like, obviously replace usernames and folders.

    import os, sys, site
    
    #Add the site-packages
    site.addsitedir('/home/SERVER_USERNAME/.virtualenvs/histology_env/lib/python2.6/site-packages')
    
    
    #activate_env=os.path.expanduser("~/.virtualenvs/histology_env/bin/activate_this.py")
    #execfile(activate_env, dict(__file__=activate_env))
    
    sys.path.append('/opt/')
    sys.path.append('/opt/MY_PROJECT')
    sys.path.append('/opt/MY_PROJECT/MY_PROJECT')
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "MY_PROJECT.settings")
    
    from django.core.wsgi import get_wsgi_application
    application = get_wsgi_application()
    
  2. DON"T ADD THE PROJECT TO THE /var/www/html/ FOLDER! I didn't realize that was a problem until where I got to you are. I chose to put it into the /opt/ directory.

  3. You don't need to activate the env to get this to work. You add the packages in the first line. This is why I have it commented out in mine (site works fine).

  4. In the virtual configuration

    NameVirtualHost *:80
    WSGISocketPrefix /var/run/wsgi
    WSGIPythonPath /home/SERVER_USERNAME/.virtualenvs/histology_env/lib/python2.6/site-packages
    <VirtualHost *:80>
    
            WSGIDaemonProcess safe python-path=/opt/virtual_microscope:/home/SERVER_USERNAME/.virtualenvs/histology_env/lib/python2.6/site-packages
            WSGIProcessGroup safe
            WSGIScriptAlias / /opt/MY_PROJECT/MY_PROJECT/wsgi.py
    
            Alias /static/ /var/www/html/home/static/
            Alias /slides/ /var/www/html/pictures/slides/
    
            <Directory /opt/MY_PROJECT/MY_PROJECT/>
                    Order deny,allow
                    Allow from all
            </Directory>
    </VirtualHost>
    
  5. After you change these settings in the host file, make sure you sudo service httpd reload

Good luck, I know what you're going through and it is frustrating to not have the answers. Hope this works for you, it did for me.

Share:
24,624
arnold
Author by

arnold

Updated on July 09, 2022

Comments

  • arnold
    arnold almost 2 years

    I am deploying a django project and facing this error.

    My project structure like below:

    my_project
       my_project
          urls.py
          settings.py
          index.wsgi
          home
             views.py
             models.py
             .........
    
        requirements.txt
        manage.py
    

    And my index.wsgi looks like below:

    import os
    import sys
    import site
    
    # Add the site-packages of the chosen virtualenv to work with
    site.addsitedir('~/.virtualenvs/my_project/lib/python2.6/site-packages/')
    
    # Add the app's directory to the PYTHONPATH
    sys.path.append('/var/www/uni/my_project')
    sys.path.append('/var/www/uni/my_project/home')
    
    os.environ['DJANGO_SETTINGS_MODULE'] = 'my_project.settings'
    
    # Activate your virtual env
    activate_env=os.path.expanduser("/home/user/.virtualenvs/my_project/bin/activate_this.py")
    execfile(activate_env, dict(__file__=activate_env))
    
    import django.core.handlers.wsgi
    application = django.core.handlers.wsgi.WSGIHandler()
    

    And in my virtualhost, the configuration is like below :

        <Directory /var/www/uni/my_project/templates/static>
            Allow from all
         </Directory>  
      WSGIScriptAlias / /var/uni/news/my_project/my_project/index.wsgi 
    

    The apache error.log is shown as:

    mod_wsgi (pid=27330): Exception occurred processing WSGI script '/var/www/uni/my_project/my_project/index.wsgi'.
    [Mon Jun 09 14:23:53 2014] [error] [client ip] Traceback (most recent call last):
    [Mon Jun 09 14:23:53 2014] [error] [client ip]   File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/wsgi.py", line 219, in __call__
    [Mon Jun 09 14:23:53 2014] [error] [client ip]     self.load_middleware()
    [Mon Jun 09 14:23:53 2014] [error] [client ip]   File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py", line 39, in load_middleware
    [Mon Jun 09 14:23:53 2014] [error] [client ip]     for middleware_path in settings.MIDDLEWARE_CLASSES:
    [Mon Jun 09 14:23:53 2014] [error] [client ip]   File "/usr/local/lib/python2.6/dist-packages/django/utils/functional.py", line 184, in inner
    [Mon Jun 09 14:23:53 2014] [error] [client ip]     self._setup()
    [Mon Jun 09 14:23:53 2014] [error] [client ip]   File "/usr/local/lib/python2.6/dist-packages/django/conf/__init__.py", line 42, in _setup
    [Mon Jun 09 14:23:53 2014] [error] [client ip]     self._wrapped = Settings(settings_module)
    [Mon Jun 09 14:23:53 2014] [error] [client ip]   File "/usr/local/lib/python2.6/dist-packages/django/conf/__init__.py", line 95, in __init__
    [Mon Jun 09 14:23:53 2014] [error] [client ip]     raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
    [Mon Jun 09 14:23:53 2014] [error] [client ip] ImportError: Could not import settings 'my_project.settings' (Is it on sys.path?): No module named my_project.settings
    

    I went through the mod_wsgi and djnago docs. I know the project structure is not maintaining all the best practices. I will change it later but before that I need to go it live.

    I tried by changing file permissions and all the changes that mentioned in same questions.

    So, I am assuming I am doing something wrong.

    Where is the mis configuration in the above files?

    Thanks.

  • arnold
    arnold almost 10 years
    1. show nothing 2. permissions are ok 3.which configuration you are referring?
  • soloidx
    soloidx almost 10 years
    My fault, the 1 point works only if you have the fcgi configuration, you can change -for test- to fcgi ans test in console, i will edit the answer adding a script for test a wsgi script. I the 3 point (if both 1 and 2 passed) you have to check the virtualhost configuration, the .htaccess configuration and at least the wsgi module (is less probable)
  • user83039
    user83039 over 9 years
    What is this index.wsgi interface you speak of?
  • soloidx
    soloidx over 9 years
    It's the python script that serves as a bridge between the django project and the wsgi (Apache) server it's placed in the django project folder
  • ss7
    ss7 over 8 years
    Why not add it to the /var/www/html folder? I have mine added there but my problem is completely different. And if you don't add it there, why are media and static still in that directory? Could I just move everything to the /opt directory and change my aliases? Why is it even a problem at all?