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:

[bash light=”true”]
$ sudo rpm -Uhv http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
[/bash]

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

[bash light=”true”]
$ sudo yum install python Django mod_python MySQL-python
[/bash]

Test Django

[python light=”true”]
$ 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()
1.4.5
>>>
[/python]

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 0.0.0.0 tells Django to accept external connections.
  2. Ensure the test port is open on the firewall.

[bash light=”true”]
$ cd /opt/foo
$ python manage.py runserver 0.0.0.0:8000
[/bash]

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.

[bash light=”true”]
$ cd /opt/foo
$ mkdir apache
$ vim apache/django.wsgi
[/bash]

and fill the file with something like:

[python]
import os
import sys

sys.path.append(‘/opt/foo’)
sys.path.append(‘/opt/foo/foo’)

os.environ[‘DJANGO_SETTINGS_MODULE’] = ‘foo.settings’

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

[/python]

Inside of the Apache virtual host:

[php]
<VirtualHost *:80>
ServerName foo.bar.com

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>

<Directory /opt/food/static>
Order deny,allow
Allow from all
</Directory>
</VirtualHost>
[/php]

Restart the Apache server.

Test to ensure that you can connect.

Credits

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

2 thoughts on “Installing Django on CentOS

  1. karimchedid

    THanks for the tutorial , but i still didnt get where to create the file “Create an Apache WSGI file.”
    I am using centos 7 on Media temple
    thanks a lot

Leave a Reply