Skip to content. Skip to navigation

Ian Lawrence

Personal tools
You are here: Home Blog Set up Django,Apache and PostgreSQL on Ubuntu Feisty

Set up Django,Apache and PostgreSQL on Ubuntu Feisty

A quick and secure installation and configuration of Django, mod_python and PostgreSQL 8.2 for application development and/or deployment.

First set up the applications we need by
opening a shell

ian@lawrence:~$ sudo apt-get install python-psycopg2 python-psycopg postgresql-8.2 postgresql-client-8.2 apache2 libapache2-mod-python subversion pgadmin3 pgadmin3-data
Create a folder in the home directory called
Web and move into that folder:
ian@lawrence:~$ mkdir Web;cd Web
Install the latest Django code from svn:
ian@lawrence:~/Web$  svn co django_src
Python won’t recognize Django unless it is
installed in the “site-packages” directory, so
create a symbolic link from this checked out
source code to "site-packages"
ian@lawrence:~/Web$ sudo ln -s `pwd`/django_src/django /usr/lib/python2.5/site-packages/django
Then copy the file into
/usr/local/bin so that we can use this tool
directly in a shell
ian@lawrence:~/Web$ sudo cp `pwd`/django_src/django/bin/ /usr/local/bin

Create Django’s directories

ian@lawrence:~/Web$ mkdir django_projects
ian@lawrence:~/Web$ mkdir django_templates
ian@lawrence:~/Web$ mkdir media
So the folder structure will be like:

Folder Structure Django

Add the django_projects folder to
the PYTHONPATH so python knows to look here:
ian@lawrence:~/Web$ cd ..
ian@lawrence:~$ sudo gedit .bashrc
this opens .bashrc and at the end of the file add
this line:
export	PYTHONPATH=".:$HOME/Web/django_projects"
Close the shell and open a fresh one to make
sure that bash knows about this new line then
create some apache symbolic links.
This is so that apache knows about the folders
we created earlier
ian@lawrence:~$ cd /var/www
ian@lawrence:/var/www$ sudo ln -s ~/Web/media media
ian@lawrence:/var/www$ sudo ln -s ~/Web/django_src/django/contrib/admin/media admin_media
Move into the Django projects directory and
start a new project using Django’s command
line utility.
This will create a basic directory structure and
the necessary configuration files
ian@lawrence:~$ cd ~/Web/django_projects
ian@lawrence:~/Web/django_projects$ startproject osmrc-test-suite

Here I called my project osmrc-test-suite

Set up PostgreSQL

Reset the password for the 'postgres' admin
account for the server
ian@lawrence:~/Web/django_projects$ cd ~
ian@lawrence:~$ sudo su postgres -c psql template1
template1=# ALTER USER postgres WITH PASSWORD 'password';
template1=# \q
Use pgAdmin to access the database server.
It is easy to get a gnome menu entry for
pgAdmin by doing
ian@lawrence:~$ sudo gedit /usr/share/applications/pgadmin.desktop
and pasting this
[Desktop Entry] Comment= PostgreSQL Administrator III
Name=pgAdmin III
Comment[en_GB]=PostgreSQL Administrator III
Name[en_GB]=pgAdmin III
into the file.
The launcher will now be in the System Tools
section of the Applications menu.
Add a new user into postgresql:
ian@lawrence:~$ sudo su postgres -c createuser vern
and answer the questions (the answer to the
first question for me was vern ;) and reply yes to the
superuser question!
For a production database edit the
file /etc/postgresql/8.2/main/postgresql.conf and
change the line:
#listen_addresses = 'localhost'
listen_addresses = '*'
and also change the line:
#password_encryption = on
password_encryption = on
For a development database on
localhost ( you do not edit this file.
Now define the access rules for the database:
ian@lawrence:~$ sudo gedit /etc/postgresql/8.2/main/pg_hba.conf
This bottom part of this file should look like:
# If you change this first entry you will need to make sure that the
# database
# super user can access the database using some other method.
# Noninteractive
# access to all databases is required during automatic maintenance
# (autovacuum, daily cronjob, replication, and similar tasks).
# Database administrative login by UNIX sockets
local all postgres ident sameuser


# "local" is for Unix domain socket connections only
local all all md5
# IPv4 local connections:
host all all md5
# IPv6 local connections:
host all all ::1/128 md5
and restart the server:
ian@lawrence:~$ sudo /etc/init.d/postgresql-8.2 restart
Check with pgAdmin III the log-in with the
new user account.

Django Settings

Edit the projects file:
ian@lawrence:~$ cd Web/django_projects/osmrc_test_suite/
ian@lawrence:~/Web/django_projects/osmrc_test_suite$ gedit
and change it to look something like:
# Django settings for osmrc_test_suite project.

DEBUG = True #turn this off for a production site

('Ian Lawrence', ''),


DATABASE_ENGINE = 'postgresql' # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
DATABASE_NAME = 'some_db_name' # Or path to database file if using sqlite3.
DATABASE_USER = 'vern' # Not used with sqlite3.
DATABASE_PASSWORD = 'some_password' # Not used with sqlite3.
DATABASE_HOST = '' # Set to empty string for localhost. Not used with sqlite3.
DATABASE_PORT = '' # Set to empty string for default. Not used with sqlite3.

# Local time zone for this installation. Choices can be found here:
# although not all choices may be avilable on all operating systems.
# If running in a Windows environment this must be set to the same as your
# system time zone.
TIME_ZONE = 'America/Manaus'

# Language code for this installation. All choices can be found here:


# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True

# Absolute path to the directory that holds media.
# Example: "/home/media/"
# This is the folder we created
MEDIA_ROOT = '/home/ian/Web/media'

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash if there is a path component (optional in other cases).
# Examples: "", ""

# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
# trailing slash.
# Examples: "", "/media/".
# this is the apache synlink we created
ADMIN_MEDIA_PREFIX = '/admin_media/'

# Make this unique, and don't share it with anybody.
SECRET_KEY = '^1z1!!@@@@100ej*_fd8(md421j_vj9c)4r*=4i!+-phvtk5a7$'

# List of callables that know how to import templates from various sources.
# 'django.template.loaders.eggs.load_template_source',


#CACHE_BACKEND = 'memcached://'
#CACHE_MIDDLEWARE_KEY_PREFIX = 'osmrc_test_suite_'

ROOT_URLCONF = 'osmrc_test_suite.urls'

# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.

Synchronize the Django database.
ian@lawrence:~/Web/django_projects/osmrc_test_suite$ syncdb
Edit the URL configuration file and
uncomment the admin line.
This will allow access to the site admin section.
ian@lawrence:~/Web/django_projects/osmrc_test_suite$ gedit
# Uncomment this for admin:

(r'^admin/', include('django.contrib.admin.urls')),

Configure Apache and mod_python

In Apache2 on Ubuntu, changes to the
Apache configuration are done to
the /etc/apache/httpd.conf file.
ian@lawrence:~/Web/django_projects/osmrc_test_suite$ sudo gedit /etc/apache2/httpd.conf
and edit it to look like:
MaxRequestsPerChild 1
<location "/">

SetHandler python-program

PythonHandler django.core.handlers.modpython

SetEnv DJANGO_SETTINGS_MODULE osmrc_test_suite.settings

PythonPath "['/home/ian/Web/django_projects'] + sys.path"

PythonDebug On


<location "/admin_media">
SetHandler None

<location "/media">
SetHandler None

SetHandler None

When deploying Django sites on

mod_python, you’ll need to restart Apache each

time you make changes to your Python code.

However, since this as a development server at

the top of the httpd.conf, there is the

line MaxRequestsPerChild 1. This forces Apache

to reload everything for each request.

Do not use this setting on a production server!

The only other lines to change are in the

first block. Change “osmrc_test_suite.settings”,

to the name of the project. Then below that

change the PythonPath to point to

the django_projects folder in the home directory.

Access the Site Admin

Go to and you see

Django Admin

Wednesday, August 22, 2007 in CodeWork  | Permalink |  Comments (4)   Digg   Yahoo   Google   Spurl


Posted by trax76 at 2007-09-05 08:14
Searched a lot and your article about setting up Django,Apache and PostgreSQL on Ubuntu Feisty is the best I've read. Thank you very much. Really appreciated.
The .bashrc step was driving me nuts.
Thnx Ian!


Posted by beatuxlee at 2007-09-13 16:03
It is very appreciate.
Building Django with postgresql has extracted a lot of my time.
It is useful to me.
Maybe it is useful to my lecture too.


Posted by vern at 2007-09-26 19:36
Another common problem is on the

If you get a problem like this:
EnvironmentError: Environment variable DJANGO_SETTINGS_MODULE is undefined.

do this:
move into your project folder and

export DJANGO_SETTINGS_MODULE=<name_of_your_project>.settings


Posted by jhdunant at 2008-03-12 22:16
Thanks a lot for the tutorial. It helped me a lot.

« July 2020 »
Su Mo Tu We Th Fr Sa
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Borala (4)
Bricolabs (12)
Code (57)
Estudiolivre (12)
Life (26)
MetaReciclagem (9)
Thoughts (16)
Work (41)