Installing Django on CentOS

These are notes for what I did to get Django working on CentOS.

Activate EPEL.

Use the appropriate version, of course. I used:

$ sudo rpm -Uhv

Install Prerequisites

  1. Install Apache.
    1. Ensure it’s working, including ensuring the firewall lets traffic through.
  2. Install MySQL
  3. Install revision control or however you pull software, e.g. git.
    1. Create a non-priviledged user, if that’s you install your software.
    2. Ensure Apache can access the software directory.

Install Django

$ sudo yum install python Django mod_python MySQL-python

Test Django

$ python
Python 2.6.6 (r266:84292, Feb 22 2013, 00:00:18)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> print django.get_version()

Install and Test the Software

I create a separate user for managing the code via git.
Put the code somewhere appropriate, e.g. /opt.

  1. The address tells Django to accept external connections.
  2. Ensure the test port is open on the firewall.
$ cd /opt/foo
$ python runserver

Attempt to connect to your application. Ensure, for example, that the settings allow database access.

Running Django from Apache WSGI

Create an Apache WSGI file.

$ cd /opt/foo
$ mkdir apache
$ vim apache/django.wsgi

and fill the file with something like:

import os
import sys


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

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

Inside of the Apache virtual host:

<VirtualHost *:80>

    WSGIScriptAlias / /opt/foo/apache/django.wsgi

    # Alias /robots.txt /opt/foo/static/robots.txt
    # Alias /favicon.ico /opt/foo/static/favicon.ico
    Alias /static/admin/ /usr/lib/python2.6/site-packages/django/contrib/admin/media/
    Alias /static/ /opt/foo/static/

    <Directory /opt/foo>
        Order allow,deny
        Allow from all

    <Directory /opt/food/static>
        Order deny,allow
        Allow from all

Restart the Apache server.

Test to ensure that you can connect.


A big shout out to ITek Blog’s article Installing Python / Django on Centos 6.3 is Easy!.

This entry was posted in SysAdmin, Uncategorized and tagged , , , . Bookmark the permalink.

Leave a Reply