4
votes

Ok, after 5-6 hours of trying, I give up. I have searched the web, tried all solutions suggested, but nothing is solving my problem.

Goal: Set up Django on my Ubuntu 12.04 VPS.

Problem: Exception occurred processing WSGI script [...] ImportError: No module named django.core.handlers.wsgi in /etc/log/apache2/error.log.

Solutions tried: 1) Appending the site-packages directory to the sys path in Djangos' wsgi.py file, 2) re-installing mod_wsgi, 3) making sure mod_wsgi is compiled for the same Python version as Django is installed with, 4) chmod 777 for the site-packages directory.

Environment: Ubuntu 12.04 VPS, Django installation in virtualenv, Python version 2.7.3, Django version 1.6.1, mod_wsgi built from mod_wsgi-3.4.tar.gz.

Full error message:

mod_wsgi (pid=23691): Exception occurred processing WSGI script '/var/www/mySite/djangoSite/djangoSite/wsgi.py'.
Traceback (most recent call last):
   File "/var/www/mySite/djangoSite/djangoSite/wsgi.py", line 7, in <module>
     import django.core.handlers.wsgi
ImportError: No module named django.core.handlers.wsgi
mod_wsgi (pid=23691): Target WSGI script '/var/www/mySite/djangoSite/djangoSite/wsgi.py' cannot be loaded as Python module.

mod_wsgi (pid=23691): Exception occurred processing WSGI script '/var/www/mySite/djangoSite/djangoSite/wsgi.py'.
Traceback (most recent call last):
   File "/var/www/mySite/djangoSite/djangoSite/wsgi.py", line 7, in <module>
     import django.core.handlers.wsgi
ImportError: No module named django.core.handlers.wsgi

Conf file from sites-available:

<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName mysite.com

    DocumentRoot /var/www/mysite.com/djangoSite

    WSGIDaemonProcess djangoSite python-path=/var/www/mysite.com/djangoSite:~/Envs/myEnv/lib/python2.7/site-packages
    WSGIProcessGroup djangoSite
    WSGIScriptAlias / /var/www/mysite.com/djangoSite/djangoSite/wsgi.py

    Alias /static/ /var/www/mysite.com/djangoSite/static/

    <Directory /var/www/mysite.com/djangoSite/static>
        Order deny,allow
        Allow from all
    </Directory>

    <Directory /var/www/mysite.com/djangoSite/djangoSite>
        <Files wsgi.py>
            Order deny,allow
            Allow from all
        </Files>
    </Directory>
</VirtualHost>

wsgi.py:

import os
import sys

sys.path.append('/var/www/mysite.com/djangoSite/')
sys.path.append('/var/www/mysite.com/djangoSite/djangoSite/')

activate_this = '/root/Envs/myenv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

os.environ['DJANGO_SETTINGS_MODULE'] = 'djangoSite.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

Site structure

/var/www/
-- mysite.com
  -- djangoSite
    -- manage.py
    -- djangoSite
      -- settings.py, wsgi.py etc.
3
What is your Apache site .conf?adityasdarma1
I haven't made any edits to it.user1781186
Not apache2.conf, but your site configuration, such as <VirtualHost> etc. Usually located at /etc/apache2/sites-available/<yourdomainname>.confadityasdarma1
If you have multiple python versions installed: does the python version you compiled mod_wsgi against match the python version of your virtualenv?sk1p
@adityasdarma1: Oh ok. I've updated my original post.user1781186

3 Answers

2
votes

In your wsgi.py, try adding this to activate virtual env:

import os
import sys

sys.path.append('/Path_To/Virtual_Env/Project_Dir/')

#This is important if multiple apps are running (instead of setdefault)
os.environ["DJANGO_SETTINGS_MODULE"] = "app_name.settings"

activate_this = '/Path_To/Virtual_Env/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

In you apache config, you mainly need only(No Deamon Process required):

WSGIScriptAlias / /var/www/mysite.com/djangoSite/djangoSite/wsgi.py
Alias /static/ /var/www/mysite.com/djangoSite/static/

See from the Create Virtual Host section of this link

0
votes

Make sure you are using absolute paths in your Apache2 config file. I assume you have been able to run python manage.py shell to get a working Django shell? I would tackle the Apache2 config file; I'm sure your issue is there.

0
votes
~/Envs/myEnv/lib/python2.7/site-packages

I think that's your problem right there: the ~ is, if it is expanded at all, expanded to the home of the www-data user. You can validate that by doing something like this:

import sys
print >> sys.stderr, sys.path

in your wsgi file; then check your error log. If it doesn't end up in the main error log, add an ErrorLog to your vhost to get per-vhost error logging.

Also, /root/ is not readable for the www-data user, you need to move your virtual env somewhere accessible.