views:

196

answers:

2

Hi,

I am trying to get my Django project running on the production server.

I setup the environment using pip, so it is identical to the development environment where everything is running fine. The only difference is that I don't use virtualenv on production, because this project is the only one that is going to run on production. Also on production, there is an Nginx reverse proxy to serve static content, and passes dynamic requests to Apache2.

The Apache wsgi file is as follows:

import sys, os

sys.path.append('/home/project/src')

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

When I access the server, I get an import error:

ImproperlyConfigured: Error importing middleware middleware: "cannot import name UserProfile"

Which refers to the middleware.py under src/ folder which is referred by the settings. But I can import both the middleware and the UserProfile class from within ./manage.py shell prompt.

It seems like a problem with paths in wsgi file but I cannot see what. The directory structure is:

/home/project
/home/project/src (which contains the settings.py, middleware.py and app folders)
/home/apache/apache.wsgi

Any help is greatly appreciated.

Thanks, oMat

+2  A: 

Ensure all directories/files are readable by others so that Apache user can access them, unless that is you are running daemon mode with different user in case that user has to be able to read them. Also read:

http://blog.dscpl.com.au/2010/03/improved-wsgi-script-for-use-with.html

It may not help, but the WSGI script file contents described at end of that will ensure process environment is setup closer to what Django development server does in case it is related to those differences.

Graham Dumpleton
thanks a lot. i am not sure why and how but your wsgi script solved my issue. btw, the link to your post was not accessible, thanks to google's cached copy.
omat
A: 

Looking at my own file, I add the directory that contains the projects, and I specify project.settings, not settings

Try:

import sys, os

sys.path.append('/home/')
sys.path.append('/home/project/')

os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings'
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

It would be better if you made something like ~/django-sites in order you specify that instead of /home

meder