David Cramer's Blog

Python, Django, and Scale.

Setup Mod_wsgi for Django and Shared Hosting

Recently I took the opportunity to configure my server for per-user hosting (to offer some free hosting to people who needed it). Doing this I had to configure mod_wsgi per-user as well. First, let me say that the documentation is excellent, but real world examples are always very helpful. Below is the output of a script which runs per-user on my server.

/etc/apache2/users/username.conf


<VirtualHost *>
ServerAdmin username@localhost
ServerName userdomain.com
ServerAlias userdomain.com www.userdomain.com

WSGIScriptAlias / "/home/username/.wsgi/project_name"
WSGIDaemonProcess userdomain user=username threads=15 display-name=%{GROUP}
WSGIProcessGroup userdomain

DocumentRoot "/home/username/project_name"

Alias /media/ "/home/username/project_name/media/"
<Directory "/home/username/project_name/media">
Order allow,deny
Options Indexes
Allow from all
IndexOptions FancyIndexing
</Directory>

Alias /admin-media/ "/usr/local/django-dev/django/contrib/admin/media/"
<Directory "/usr/local/django-dev/django/contrib/admin/media">
Order allow,deny
Options Indexes
Allow from all
IndexOptions FancyIndexing
</Directory>

<Directory "/home/username/project_name/">
Options +ExecCGI
Allow from all
</Directory>

ErrorLog /home/username/logs/userdomain.com/error.log
CustomLog /home/username/logs/userdomain.com/access.log combined
</VirtualHost>

/home/username/.wsgi/project_name

1
2
3
4
5
6
7
8
9
10
11
12
13
import os, sys

# Add the project to the python path
sys.path.append('/usr/local/django-dev/django')
sys.path.append('/home/username')

# Set our settings module
os.environ['DJANGO_SETTINGS_MODULE']='project_name.settings'

import django.core.handlers.wsgi

# Run WSGI handler for the application
application = django.core.handlers.wsgi.WSGIHandler()

Comments