views:

52

answers:

3

This is probably really simple. But I guess I'm too new to WSGI and Django to get it on my own. I have a brand new shiny Django project on a Ubuntu virtual machine hosted in /var/www/mysite. The project was created with

django-admin startproject mysite

I'm following a WSGI tutorial to get it set up, so I created an ./apache folder, inside of which I placed this file, django.wsgi:

import os
import sys
os.environ['DJANGO_SETTINGS_MODULE'] = '/var/www/mysite/mysite.settings'

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

I then added this config line to the Apache configuration:

WSGIScriptAlias / /var/www/mysite/apache/django.wsgi

When I try to hit the site, nothing is returned. The connection just hangs. This is in my access.log:

192.168.2.116 - - [15/Aug/2010:14:09:02 -500] "GET / HTTP/1.1" 500 639 "-" "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8

So it's hitting the site. But someone isn't doing anything. There are no errors in the errors.log.

Anyone have any ideas?

+1  A: 

Aren't you missing path to your Django application? My app.wsgi has this:

import os, sys
sys.path.append('/usr/local/src/django-1.2') # django is outside default path
sys.path.append('/usr/local/src/django-photologue-2.2') # app i'm using
sys.path.append('/var/www/mysite')

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
# this is regular python import, so my settings are physically
# here: /var/www/mysite/settings.py

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
che
Well, it looked like you have your app.wsgi file inside the project folder and not in a apache subfolder, so I moved mine there and changed the apache config accordingly. Ha. I got it! Thanks much.
yodaj007
+2  A: 

The blatant mistake in original question is that:

os.environ['DJANGO_SETTINGS_MODULE'] = '/var/www/mysite/mysite.settings'

would need to be:

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

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

That is, Django settings module must be a Python package path, NOT an absolute file system path. The accepted answer doesn't even point that out and rather just gives another set of code to use with no explanation.

Further, you need to set Python module search path, ie., sys.path, so that Python can find your settings file listed in that environment variable.

Finally, if your browser was hanging you have done something else wrong as well, as what you did should have resulted in an error being returned and not the browser hanging. There would also have had to be an error in the Apache error log, so you are either looking in wrong place or don't know what to look for.

Graham Dumpleton
When I moved the wsgi file into the project root and added the path, it all worked.
yodaj007
Having the WSGI file in the project root is a bad idea as it implies that you would technically need to allow Apache to be able to serve up any files to a remote user from your project directory, including your settings file including database passwords. Now, you may not have any URLs mapped in Apache which mean files in project directory can actually be downloaded as static files, but you still have removed one underlying level of security. So, go back to using 'apache' subdirectory and only have 'Allow from all' for that 'apache' subdirectory containing the WSGI script file.
Graham Dumpleton
For further information about 'apache' subdirectory, see 'http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango'.
Graham Dumpleton
A: 

Hi yoda.

I wrote an article on getting django setup with mod_wsgi, apache, git, mysql, libjpeg, PIL and many other goodies.

Take a look, maybe this will help you.

http://appelfreelance.com/2010/07/a-working-copy-django-pil-imaging-libjpeg-mysql-git-apache-mod_wsgi-easy_install-pip-django-imagekit-cdn-storage-on-centos-5-4/

ApPeL