Linode Library Home
Linode Library RSS Feed
Home :: Frameworks :: Django mod_wsgi
Print View View Source

Django, Apache and mod_wsgi on Fedora 14

Published: by

Warning

This document is an older, unmaintained guide. There may be a new guide available for this software.

Django is a web development framework for the Python programing language. It enables rapid development, while favoring pragmatic and clean design. Django was initially developed for use in a newspaper's website division, and as a result the Django framework is very well suited to developing content-centric applications.

This guide provides an introduction to getting started with the Django framework, using the mod_wsgi method of deploying python applications. Please complete the the getting started guide prior to beginning this guide on an up to date system. Furthermore, you will want a running Apache web server and a functional MySQL database system installed.

Contents

Set the Hostname

Before you begin installing and configuring the components described in this guide, please make sure you've followed our instructions for setting your hostname. Issue the following commands to make sure it is set properly:

hostname
hostname -f

The first command should show your short hostname, and the second should show your fully qualified domain name (FQDN).

Install Dependencies

Issue the following commands to ensure that your system's package repositories and installed programs are up to date and all required software is installed:

yum update
yum install python-setuptools httpd mod_wsgi

Additionally you will need to install a database system and a python driver for this database system. If you want to run the PostgreSQL database server issue the following command:

yum install postgresql python-psycopg2

If you want to use the SQLite embedded database, issue the following command:

yum install sqlite python-sqlite

If you want to run the MySQL database engine, download and install a more recent version of the MySQL-python package. Django requires at least version 1.2.1p2 of the Python MySQLdb adapter. We'll download and install a later version from the upstream project First, install the tools needed to build this package:

yum install python-devel mysql-devel gcc wget python-setuptools

Check the Python MySQLdb page for information regarding the latest release, then issue the following commands. This will download the archive of the source files, extract them from the archive, build the database adapter, and install the files on to your system.

cd /opt/
wget http://downloads.sourceforge.net/project/mysql-python/mysql-python/1.2.3/MySQL-python-1.2.3.tar.gz
tar -zxvf MySQL-python-1.2.3.tar.gz
cd MySQL-python-1.2.3/
python setup.py build
python setup.py install

Your application may require additional dependencies. You may install these either using the CentOS package tools or by using the easy_install command included in python-setuptools.

Install Django

There are two methods for installing Django. You may choose to install Django using packages from the Fedora repository. This will install version 1.2.5 of the Django software. Issue the following command:

yum install Django

If you want to install Django using the easy_install tool from the "setuptools" package, issue the following command:

easy_install Django

At the time of writing, this will install version 1.2.5 of the Django framework, but may install a later version when "Pepi` updates. Consider the package information for Django for more information.

Configure Django Applications for WSGI

In order for mod_wsgi to be able to provide access to your Django application, you will need to create a django.wsgi file inside of your application directory. For the purposes of this example, we assume that your application will be located outside of your DocumentRoot in the directory /srv/www/ducklington.org/application. Modify this example and all following examples to conform to the actual files and locations used in your deployment.

File:/srv/www/ducklington.org/application/django.wsgi

import os
import sys

sys.path.append('/srv/www/ducklington.org/application')

os.environ['PYTHON_EGG_CACHE'] = '/srv/www/ducklington.org/.python-egg'
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

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

You must append the path of your application to the system path as above. Additionally, declaration of the PYTHON_EGG_CACHE variable is optional but may be required for some applications when WSGI scripts are executed with the permissions of the web server. Finally, the DJANGO_SETTINGS_MODULE must refer to the Django settings.py file for your project. You will need to restart Apache after modifying the django.wsgi file.

Configure Apache

Consider the following example virtual host configuration:

File excerpt:Apache Virtual Host Configuration

<VirtualHost ducklington.org:80>
   ServerName ducklington.org
   ServerAlias www.ducklington.org
   ServerAdmin squire@ducklington.org

   DocumentRoot /srv/www/ducklington.org/public_html

   WSGIScriptAlias / /srv/www/ducklington.org/application/django.wsgi
   <Directory /srv/www/ducklington.org/application>
      Order allow,deny
      Allow from all
   </Directory>

   Alias /robots.txt /srv/www/ducklington.org/public_html/robots.txt
   Alias /favicon.ico /srv/www/ducklington.org/public_html/favicon.ico
   Alias /images /srv/www/ducklington.org/public_html/images
   Alias /static /srv/www/ducklington.org/public_html/static

   ErrorLog /srv/www/ducklington.org/logs/error.log
   CustomLog /srv/www/ducklington.org/logs/access.log combined
</VirtualHost>

In this example, the WSGIScriptAlias directive tells Apache that for this virtual host, all requests below / should be handled by the WSGI script specified. In the directory block that follows, we allow Apache to serve these requests. Finally, the series of four Alias directives allow Apache to serve the robots.txt and favicon.ico files as well as all resources beneath the /images and /static locations, directly from the DocumentRoot without engaging the WSGI application. You can add as many Alias directives as you need to.

Deploy Applications with Apache

When you have successfully configured your Apache virtual host, issue the following command to restart the web server:

/etc/init.d/httpd restart

Now issue the following command to ensure that Apache will start after a reboot cycle, if you have not already:

chkconfig httpd on

You will need to restart the web server every time the django.wsgi file changes. However, all other modifications to your application do not require a web server restart. Congratulations! You have now successfully deployed a Django application using mod_wsgi.

More Information

You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.

Creative Commons License

This guide is licensed under a Creative Commons Attribution-NoDerivs 3.0 United States License.

Last edited by Sharon Campbell on Thursday, September 26th, 2013 (r3710).