CMPT 470, Fall 2012

Deploying Django

This information is provided to help you get (possibly several) Django applications running on your group's web server. The setup I have suggested is Apache 2 with mod_wsgi. That seems to be the best way in the modern world.

Installation and Setup

First of course, you need the relevant software installed:

sudo apt-get install apache2-mpm-prefork libapache2-mod-wsgi python-django python-mysqldb

If you haven't already, you will need to install the MySQL server as well: sudo apt-get install mysql-server

I'm going to assume a Django project already checked-out in the directory /home/userid/djangoproject/ and that you want to access that project at http://server/mysite/.

In /home/userid/djangoproject, create a file django.wsgi with contents like this:

#!/usr/bin/env python
import os, sys

# make sure app's modules can be found
sys.path.append('/home/userid')
sys.path.append('/home/userid/djangoproject')
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

# Switch to the directory of your project. (Optional.)
# os.chdir("/home/userid/djangoproject")

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

To alias this app to the URL, and start the setup process, edit /etc/apache2/sites-enabled/000-default and at the bottom of the <Virtualhost> section (right before the “</Virtualhost>”), add this:

WSGIScriptAlias /mysite /home/userid/djangoproject/django.wsgi
WSGIDaemonProcess mysite user=userid
<Directory /home/userid/djangoproject/>
  WSGIProcessGroup mysite
</Directory>

(With no slash immediately after mysite in WSGIScriptAlias. Only the first line here is strictly necessary: the rest make sure the process is running with your userid, not the web server's.) Restart the Apache server so it recognizes the config changes:

sudo /etc/init.d/apache2 restart

Settings

You will likely have to modify your settings.py file so that your app uses a MySQL database that you create, rather than the SQLite database you are using for development. See the MySQL database instructions for info on creating a database for your app, then do a python manage.py syncdb.

You will also probably have to add the template directory (e.g. /home/userid/djangoproject/templates) to the TEMPLATE_DIRS variable.

Working

You should be able to work on the Django application more-or-less without worrying about the setup.

If you make changes to the database, or code, or otherwise want to restart the process, the easiest and most surefire way is to restart Apache entirely:

sudo /etc/init.d/apache2 restart

Redirects and Links

Of course, any links within your system will have to take the new URL into account. For example, a link like this

<a href="/object/edit">

will have to become:

<a href="/mysite/object/edit">

Hopefully, you have used the {%url%} template tag and reverse function to generate all of the URLs in your app. If so, the changes should be automatic.

If you haven't used {%url%} and reverse, the easiest fix might be to go back to your code and use them wherever you need a URL. For example, in the Django tutorial, the index.html template contains this link:

<li><a href="/polls/{{ poll.id }}/">{{ poll.question }}</a></li>

… which will become:

<li><a href="{% url polls.views.detail poll_id=poll.id %}">{{ poll.question }}</a></li>

Static Media

For all but the simplest sites, you will have some static files that accompany the dynamically-generated code (stylesheets, images, Javascript code, etc.). While it's possible to serve these from within Django, that will be much slower than just letting the web server itself send the files as necessary.

These instructions assume that you have a static directory in your Django app. (i.e. djangoproject/static)

In your Apache config, add this line before the WSGIScriptAlias line you added above:

Alias /mysite/static "/home/userid/djangoproject/static"

Now, all of the files in that directory should be accessible below http://server/mysite/static. You can then set the MEDIA_URL variable in your settings.py file to '/mysite/static' and include things like this in your templates (if you render with context_instance=RequestContext(request)):

<link rel="stylesheet" href="{{MEDIA_URL}}/style.css" />

You can also configure caching or other HTTP features on your static directory in the Apache configuration file.

Admin Site Media

You may still need the Django admin site when your app is in production. (Or maybe not, but it's possible.) If so, you will find that the stylesheets and other media for the admin side likely aren't being served properly. You can get things working by setting the proper URL prefix in your settings.py:

ADMIN_MEDIA_PREFIX = '/mysite/admin-media/'

In your Apache config, add this line before the WSGIScriptAlias line you added above:

Alias /mysite/admin-media "/usr/share/pyshared/django/contrib/admin/media"

How to use Django with mod_wsgi in the Django docs, or Integration With Django in the mod_wsgi docs.

I also have instructions to deploy Django with FastCGI if mod_wsgi isn't working for you.


Copyright © , based on content created by Greg Baker.