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
(Applications->Accessories->Terminal):
ian@lawrence:~$ sudo apt-get install python-psycopg2 python-psycopg postgresql-8.2 postgresql-client-8.2 apache2 libapache2-mod-python subversion pgadmin3 pgadmin3-dataCreate a folder in the home directory called
Web and move into that folder:
ian@lawrence:~$ mkdir Web;cd WebInstall the latest Django code from svn:
ian@lawrence:~/Web$ svn co http://code.djangoproject.com/svn/django/trunk/ django_srcPython 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/djangoThen copy the django-admin.py 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/django-admin.py /usr/local/bin
Create Django’s directories
ian@lawrence:~/Web$ mkdir django_projectsSo the folder structure will be like:
ian@lawrence:~/Web$ mkdir django_templates
ian@lawrence:~/Web$ mkdir media
Add the django_projects folder to
the PYTHONPATH so python knows to look here:
ian@lawrence:~/Web$ cd ..this opens .bashrc and at the end of the file add
ian@lawrence:~$ sudo gedit .bashrc
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/wwwMove into the Django projects directory and
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
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$ django-admin.py 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 ~Use pgAdmin to access the database server.
ian@lawrence:~$ sudo su postgres -c psql template1
template1=# ALTER USER postgres WITH PASSWORD 'password';
template1=# \q
It is easy to get a gnome menu entry for
pgAdmin by doing
ian@lawrence:~$ sudo gedit /usr/share/applications/pgadmin.desktopand pasting this
[Desktop Entry] Comment= PostgreSQL Administrator IIIinto the file.
Name=pgAdmin III
Encoding=UTF-8
Exec=pgadmin3
Terminal=false
Comment[en_GB]=PostgreSQL Administrator III
Icon=/usr/share/pixmaps/pgadmin3.xpm
Type=Application
Categories=GNOME;Application;Database;System;
Name[en_GB]=pgAdmin III
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 vernand 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'to
listen_addresses = '*'and also change the line:
#password_encryption = onto
password_encryption = onFor a development database on
localhost (127.0.0.1) 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.confThis bottom part of this file should look like:
# DO NOT DISABLE!and restart the server:
# 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
# TYPE DATABASE USER CIDR-ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all md5
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
ian@lawrence:~$ sudo /etc/init.d/postgresql-8.2 restartCheck with pgAdmin III the log-in with the
new user account.
Django Settings
Edit the projects settings.py file:
ian@lawrence:~$ cd Web/django_projects/osmrc_test_suite/and change it to look something like:
ian@lawrence:~/Web/django_projects/osmrc_test_suite$ gedit settings.py
# Django settings for osmrc_test_suite project.Synchronize the Django database.
DEBUG = True #turn this off for a production site
TEMPLATE_DEBUG = DEBUG
ADMINS = (
('Ian Lawrence', 'root@ianlawrence.info'),
)
MANAGERS = ADMINS
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:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# 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:
# http://www.w3.org/TR/REC-html40/struct/dirlang.html#langcodes
LANGUAGE_CODE = 'en-us'
SITE_ID = 1
# 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/media.lawrence.com/"
# 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: "http://media.lawrence.com", "http://example.com/media/"
MEDIA_URL = 'http://127.0.0.1/media/'
# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
# trailing slash.
# Examples: "http://foo.com/media/", "/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.
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.load_template_source',
'django.template.loaders.app_directories.load_template_source',
# 'django.template.loaders.eggs.load_template_source',
)
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.middleware.doc.XViewMiddleware',
)
#CACHE_BACKEND = 'memcached://127.0.0.1:11211/'
#CACHE_MIDDLEWARE_SECONDS = 300
#CACHE_MIDDLEWARE_KEY_PREFIX = 'osmrc_test_suite_'
#CACHE_MIDDLEWARE_ANONYMOUS_ONLY = True
ROOT_URLCONF = 'osmrc_test_suite.urls'
TEMPLATE_DIRS = (
"/home/ian/Web/django_templates"
# 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.
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
'osmrc_test_suite.lkrt'
)
ian@lawrence:~/Web/django_projects/osmrc_test_suite$ django-admin.py syncdbEdit 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 urls.py
# 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.confand 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>
<location "/admin_media">
SetHandler None
</location>
<location "/media">
SetHandler None
</location>
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 http://127.0.0.1/admin/ and you seetags:
nice
Building Django with postgresql has extracted a lot of my time.
It is useful to me.
Maybe it is useful to my lecture too.
DJANGO_SETTINGS_MODULE error
DJANGO_SETTINGS_MODULE.
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
Thanks
JH





Great!
The .bashrc step was driving me nuts.
Thnx Ian!