Shihui Guo

Django on Apache with mod-WSGI in Ubuntu 12.04

Actually this is not that difficult. Suppose you already got your django and apache running separately.

First install the libapache2-mod-wsgi module

sudo apt-get install libapache2-mod-wsgi

Go to your django project folder, make a subfolder called apache, and generate a wsgi file

mkdir apache
cd apache

import os
import sys

sys.path.append('PATH_TO_DJANGO_PROJECT')

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

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

Now switch to apache side, add a new website config file under /etc/apache2/sites-available/

<VirtualHost *:80>

WSGIScriptAlias / <strong>PATH_TO_DJANGO_PROJECT</strong>/apache2/django.wsgi
<Directory <strong>PATH_TO_DJANGO_PROJECT</strong>/apache2>
Order allow,deny
Allow from all
</Directory>
</VirtualHost>

Now enable your website, and restart the apache, should be working now!

Note: please replace the variable PATH_TO_DJANGO_PROJECT with your own!

点击查看评论