Roshan Book

My Tech Notebook

Installing Django on Ubuntu -worked for me

Original post at : sudo ln -s /usr/share/django/django /usr/lib/python2.7/dist-packages/django

I’ve been toying around with some small ideas for websites, and I’ve been looking for some straightforward frameworks that would allow me to quickly prototype a site that could still be used, at least for moderate levels of traffic, in a production environment.  I decided to check out Django, a Python based web framework that supports a lot of out-of-the-box functionality and is easily extensible.

This post outlines the steps I took to install Django on test web server, which is currently running Ubuntu 10.10 (Maverick Meerkat).


As is often the case, I like to configure my test system so that it is as close as possible to the production environment I eventually deploy to.  In many cases, that means deviating from Ubuntu’s distribution repositories and installing some packages from source.  In the case of Django, there is an appliance version of Ubuntu that supports Django, but nothing official in the repositories.  So as usual, I’m back to installing my own version.  Before I started, I laid out a few of the requirements I would like to achieve.

  • Use the latest stable version of Django (1.2.3)
  • Install Django in a shared location, but Django sites should be independent.
  • Each Django site should be in its own Apache VirtualHost, so as not to disturb the other sites I have running on my test server.
  • Each Django site should be easily maintained in source control and should not include any major artifacts from the Django library.  This will make the application portable, and will be helpful when I move it to other servers, such as production.

I based most of my work on an article on,  but made some adjustments along the way to suit my needs, so I’m posting my steps for anyone else that might be following the same path.

Conceptually, you can break down installing Django into the following steps:

  1. Retrieve and Install Django
  2. Create and Configure a Django project
  3. Configure Apache
  4. Get coding on your new Django installation

Retrieve and Install Django

In order to retrieve and install Django, I directly checked out the stable branch from Django’s SVN repository.  I put it in my /usr/share directory, as it is a shared library used by the system.  In addition to checking out the library, I set permissions so that it was owned by the www-data user (my apache user) and linked the Django package into my python library.  I also linked the script into my bin directory so that it is always available on the path.  The steps for doing this are illustrated below.

1 # check out version 1.2.3 of Django into my /usr/share directory and update permissions
3 sudo chown -R www-data:www-data /usr/share/django
5 # link Django package into the python library
6 sudo ln -s /usr/share/django/django /usr/lib/python2.6/dist-packages/django
8 # make available on the path
9 sudo ln -s /usr/share/django/django/bin/ /usr/local/bin/

If you are unsure of where Python is installed on your system, you can get this by running the following line.

1 python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"

That’s all that’s needed.  You’re now ready to move on to creating your first Django project.

Create and Configure a Django project

Set up the Database

Before you start, you should set up a database for Django to use.  I use MySql, but there are a number of compatible databases and non-database backends that can be used with Django.  My database setup is pretty simple.  Just log into mysql as root

1 mysql -u root - p

and then run the following commands

2 CREATE USER 'mydjango'@'localhost' IDENTIFIED BY 'mydjango'
3 CREATE USER 'mydjango'@'%' IDENTIFIED BY 'mydjango'
4 GRANT ALL PRIVILEGES ON mydjango.* to 'mydjango'@'%' WITH GRANT OPTION;
5 GRANT ALL PRIVILEGES ON mydjango.* to 'mydjango'@'localhost' WITH GRANT OPTION;

Creating the Django Site

Next up is creating the Django site.  Django provides an admin script for creating the project for you, so I only had to choose a directory for where the site would live, and set up the directory structure that I wanted to use.

For my purposes, I wanted my site to live under /var/www, but still be owned by me.  This would allow me eventually to check the site directory under /var/www directly into SVN.  That way, on my test system, I can work directly on the files on the server and check in changes directly to SVN.  Additionally, I can work remotely, and synch the latest changes onto the test server easily via an SVN up.

The directory structure I chose was a follows:

  • /var/www/mydjango
    • django_projects – The directory I will create my projects in.  Using this model, my site can actually be made up of multiple django projects if needed.
    • django_templates – A directory of templates that all Django projects in my site will use.
    • media – A directory where common media (jpgs, gifs, pngs, etc.) will be stored.  These eventually will be accessed outside of the Django framework to reduce overhead.
    • admin_media – A directory that is actually a symbolic link back to the admin_media directory in the main Django library.  This allows the auto-generated admin module to use its resources.

The steps I followed for setting this up were

01 #create directories
02 sudo mkdir /var/www/mydjango/django_projects
03 sudo mkdir /var/www/mydjango/django_templates
04 sudo mkdir /var/www/mydjango/media
06 #link in the admin media
07 sudo ln -s /usr/share/django/django/contrib/admin/media /var/www/mydjango/admin_media
09 #set up ownership so I own the directory.  Make sure www-data still has rx access via group permissions
10 sudo chown -R tom:tom /var/www/mydjango

The next step is to create a new django project.  Django comes with a script (which I’ve already added to the path) that allows you to easily setup a project.  This is just a two-liner, switch to right directory and create the project.

1 cd /var/www/mydjango/django_projects
2 startproject mydjango

Configuring Django

With the script all our files have been created, the next step is to configure Django. First you will modify the /var/www/mydjango/django_projects/mydjango/ file. I’ve included my file below, but the key areas you want to modify are:

  • Add yourself in the ADMINS section
  • Add the correct database to the DATABASES section (my example shows mysql)
  • Set the TIME_ZONE for your server
  • Change MEDIA_ROOT to be the location on disk where your media will reside (the media directory we set up before)
  • Change MEDIA_URL to be the URL where media will be accessed from.
  • Change ADMIN_MEDIA_PREFIX to be the relative URL from the site root where the admin media will be accessed from.
  • Update the SECRET_KEY to something random (this is to randomize any hash and crypto functions for security purposes)
  • Update ROOT_URLCONF to reference the file in your project directory.
  • Add your templates directory (the one created earlier) to the TEMPLATE_DIRS list
  • Enable the admin interface in INSTALLED_APPS by uncommenting ‘django.contrib.admin’

My final is shown below for reference.

01 # Django settings for mydjango project.
03 DEBUG = True
06 ADMINS = (
07     ('TomS', ''),
08 )
13     'default': {
14         'ENGINE': 'mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
15         'NAME': 'mydjango',                      # Or path to database file if using sqlite3.
16         'USER': 'mydjango',                      # Not used with sqlite3.
17         'PASSWORD': 'mydjango',                  # Not used with sqlite3.
18         'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
19         'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
20     }
21 }
23 # Local time zone for this installation. Choices can be found here:
25 # although not all choices may be available on all operating systems.
26 # On Unix systems, a value of None will cause Django to use the same
27 # timezone as the operating system.
28 # If running in a Windows environment this must be set to the same as your
29 # system time zone.
30 TIME_ZONE = 'America/NewYork'
32 # Language code for this installation. All choices can be found here:
34 LANGUAGE_CODE = 'en-us'
36 SITE_ID = 1
38 # If you set this to False, Django will make some optimizations so as not
39 # to load the internationalization machinery.
40 USE_I18N = True
42 # If you set this to False, Django will not format dates, numbers and
43 # calendars according to the current locale
44 USE_L10N = True
46 # Absolute path to the directory that holds media.
47 # Example: "/home/media/"
48 MEDIA_ROOT = '/var/www/mydjango/media/'
50 # URL that handles the media served from MEDIA_ROOT. Make sure to use a
51 # trailing slash if there is a path component (optional in other cases).
55 # URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
56 # trailing slash.
57 # Examples: "", "/media/".
58 ADMIN_MEDIA_PREFIX = '/admin_media/'
60 # Make this unique, and don't share it with anybody.
61 SECRET_KEY = 'hg=dyltl9)pq$4n+aabdd&+6o1yf3ipg1@@8wm(a9-e!ype&d2'
63 # List of callables that know how to import templates from various sources.
65     'django.template.loaders.filesystem.Loader',
66     'django.template.loaders.app_directories.Loader',
67 #     'django.template.loaders.eggs.Loader',
68 )
71     'django.middleware.common.CommonMiddleware',
72     'django.contrib.sessions.middleware.SessionMiddleware',
73     'django.middleware.csrf.CsrfViewMiddleware',
74     'django.contrib.auth.middleware.AuthenticationMiddleware',
75     'django.contrib.messages.middleware.MessageMiddleware',
76 )
78 ROOT_URLCONF = 'mydjango.urls'
81     # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
82     # Always use forward slashes, even on Windows.
83     # Don't forget to use absolute paths, not relative paths.
84     "var/www/mydjango/django_templates/"
85 )
88     'django.contrib.auth',
89     'django.contrib.contenttypes',
90     'django.contrib.sessions',
91     'django.contrib.sites',
92     'django.contrib.messages',
93     # Uncomment the next line to enable the admin:
94     'django.contrib.admin',
95     # Uncomment the next line to enable admin documentation:
96     # 'django.contrib.admindocs',
97 )

You’ll also want to update your to enable the admin urls. Update your /var/www/mydjango/django_projects/mydjango/ to match the file I have below. The updates I made were to uncomment lines 4,5 and 15.

01 from django.conf.urls.defaults import *
03 # Uncomment the next two lines to enable the admin:
04 from django.contrib import admin
05 admin.autodiscover()
07 urlpatterns = patterns('',
08     # Example:
09     # (r'^mydjango/', include('')),
11     # Uncomment the admin/doc line below to enable admin documentation:
12     # (r'^admin/doc/', include('django.contrib.admindocs.urls')),
14     # Uncomment the next line to enable the admin:
15     (r'^admin/', include(,
16 )

The final step in configuring Django is to have Django auto-build your database to match all the default models that come with Django (users, groups, etc.). Django provides a management script that makes this exceedingly easly.

1 cd /var/www/mydjango/django_projects/mydjango/
2 ./ syncdb

Configuring Apache

The final step is to configure Apache.  As I mentioned previously, I wanted everything in a VirtualHost so as not to disturb the other web applications I have running on my test machine.  To do this, I created a new site descriptor as /etc/apache2/sites-available/mydjango.mydomain.home.  Some key setup notes to be aware of are below.  Everything else is pretty standard for an Apache vhost.

  • The <location “/”> element is used to set python and specifically Django as the handler for all requests.  Note that I’m adding the location of my site to the PythonPath and I’m setting the path to my settings file as part of the environment.
  • The alias and additional <location> elements are used to overrie Django and allow Apache to server up the files directly.  The aliases are set up point to the directories created earlier for the Django site

My final configuration file is shown below.

01 <VirtualHost *>
03         RewriteEngine On
05         ServerAdmin
07         ServerName mydjango.mydomain.home
09         #set up Django as the default handler for the site
10         <location "/">
11             SetHandler python-program
13             PythonHandler django.core.handlers.modpython
15             SetEnv DJANGO_SETTINGS_MODULE mydjango.settings
17             PythonPath "['/var/www/mydjango/django_projects'] + sys.path"
18         </location>
20         Alias /media /var/www/mydjango/media
21         <location "/media">
22             SetHandler None
23         </location>
25         Alias /admin_media /var/www/mydjango/admin_media
26         <location "/admin_media">
27             SetHandler None
28         </location>
30         <locationmatch ".(jpg|gif|png)">
31             SetHandler None
32         </locationmatch>
34         ErrorLog /var/log/apache2/mydjango.mydomain.home-error.log
35         # Possible values include: debug, info, notice, warn, error, crit,
36         # alert, emerg.
37         LogLevel warn
38         CustomLog /var/log/apache2/mydjango.mydomain.home-access.log combined
40 </VirtualHost>

After that, I made sure that my domain name was correctly set up on my local DNS server (or you can use a local hosts file), then I enabled the site and reloaded Apache.

1 sudo a2ensite mydjango.mydomain.home
2 sudo /etc/init.d/apache2 reload

I then navigated to http://mydjango.mydomain.home. I immediately got the 404 page you see below, which means that Django is working, I just haven’t set up any views yet.

Django 404 PageDjango is up and working, serving up a 404 page.

As a secondary test, I also went to http://mydjango.mydomain.home which showed the admin login screen below.

Django Admin LoginDjango Admin module is also working

And that pretty much wraps it up.  Django is up and running and ready for development.  I’ll be giving Django a test drive.  If things go well, there will likely be some more blog posts on the topic soon.


2 responses to “Installing Django on Ubuntu -worked for me

  1. Kylie Leipert December 19, 2011 at 3:26 am

    I was very pleased to get this site.I would like to thank you for this particular wonderful article!! Completely taking pleasure in each and every word of it and i also book marked to follow latest stuff you put up.

  2. albahala December 2, 2012 at 10:40 am

    Great article, thanks

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: