Python – Django, wsgi, py. what’s the difference

apache-2.2djangopython

I'm trying to get a django application up and running on my cpanel system. I've installed mod_wsgi, and am following the guide here:
http://www.nerdydork.com/setting-up-django-on-a-whm-cpanel-vps-liquidweb.html

However, I'm now confused as I don't know what to do next. The application has .py files, and I am able to run it via this:
python manage.py runserver 211.144.131.148:8000

However, that's via command line and binds to port 8000. I want to use Apache instead.

The question is, that tutorial doesn't go further into how to get apache to recognize .py files and run the application as I want it. What do I do next?

Best Answer

If you're using mod_wsgi, you don't want Apache to recognize your .py files. Confusing, no? Here, let me explain...

The WSGI module provides an interconnect of sorts between Apache and your Python processes. It's a standardized gateway interface (Web Server Gateway Interface), so to speak.

Here's the official Django documentation on configuring Django 1.1 with Apache & mod_wsgi.

Generally, you'll just need:

WSGIScriptAlias / /path/to/mysite/apache/django.wsgi

And then you'll need to define a WSGI application, which serves as front to your Django application:

import os
import sys

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

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

*Note that I took these examples from that document linked above.

This now wires up Apache to a WSGI app and then ties your application into that WSGI app. The WSGIScriptAlias line simply tells apache to send all requests at '/' or lower to your WSGI application, which will manage the URL routing.

I usually do deployments like this using FastCGI & the Python flup module, which is a little more complex, but worth checking out if you've the time.

Related Topic