• jekyll
  • development

For the purposes of this demonstration i’m going to assume you have Django already installed.

Ok, first things first you need to download a copy of either DB2 Express-C or get the client for your current install of DB2. In my case I will be using DB2 ESE V9.7 to connect to our main database. So in my case I have a file similar to DB2_ESE_97_Linux_x86-64.tar.gz

But first lets get some prerequisites that you need:

apt-get install libaio1 libaio-dev libstdc++6

So now it’s time to unpack the archive and install the client. If you read some of the documentation it will instruct you to use the db2setup command, which will launch a wonderfully pretty GUI based install. UPDATE. Although the GUI installer does not install all of the libraries needed, it does provide the helpful job of setting up an instance for you and the db2inst1 user account.

First run the db2 GUI installer, (as root):

tar xf DB2_ESE_97_Linux_x86-64.tar.gz
cd ese

This popup a window for the DB2 Setup Launchpad:

1. Install a Product
2. Scroll down to IBM Data Server Client Version 9.7 and click Install New
3. Follow through the wizzard to setup your client instance and db2inst1 user.

It took me a long time to realize that the GUI based install in fact leaves out many key libraries that are integral to getting all of this working with Python/Django. So you will also need to use the db2_install script

cd ese

Run through the following text based wizzard and enjoy the fact that you weren’t smoten by the GUI. Note that you will have to type in “CLIENT” to specify what type of install you want.

You will now have the all important /home/db2inst1 user, home directory and the /home/db2inst1/sqllib/db2profile script, you will be using that one a lot

Next we will install the ibm_db and ibm_db_django modules available at the ibm-db Project. I will be grabbing the source from the repository and compiling everything myself. At this point you will notice that there are pre-packaged .egg files available for download and installation via easy_install. I personally tried each one of these and can say that it is a gateway to many strange errors. Just compile your own.

If you don’t already have Subversion, get it now.

sudo apt-get install subversion

Now get the sources and lets compile, take note that you are going to want to do this all as the root user

sudo -i
svn checkout ibm-db-read-only
cd ibm-db-read-only/IBM_DB

Note: You should check with their google code site to make sure the above link has not changed

First we will compile the ibm_db module for python as the root user

cd ibm_db
. /home/db2inst1/sqllib/db2profile
export DB2_DB_DIR=/home/db2inst1/sqllib
export DB2_DB_LIB=/home/db2inst1/sqllib/lib
python build
python install

Take note of the ./home/db2inst1/sqllib/db2profile. The . and character are important and not a typo. So that should take care of the ibm_db module, now onto the ibm_db_django module

cd ..
cd ibm_db_django
python build
python install

Now… moving on it’s time to configure your Django project to connect to DB2.

	'default': {
		'ENGINE': 'ibm_db_django',
		'PORT': '50001',

Now your project should have everything it needs to connect to the database and set everything, sync it up using:

python syncdb

Hopefully that went well for you. Now that Django is ready lets setup Apache to serve up our project by editing /etc/apache2/sites-enabled/000-default. Take note that this is the lazy way, I will be editing my global apache config for my system since I am only hosting one project at the moment. It would be much better to setup virtual hosts (a topic for another day perhaps)

<VirtualHost *:80>
ServerAdmin webmaster@localhost

DocumentRoot /var/www
<Directory >
Options FollowSymLinks
AllowOverride None

<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all

<location "/MyProject">
SetHandler python-program

SetEnv DJANGO_SETTINGS_MODULE MyProject.settings
PythonOption DJANGO_SETTINGS_MODULE MyProject.settings
PythonOption DjangoRequireStaffStatus off

PythonHandler django.core.handlers.modpython
PythonDebug On
PythonPath "['/PATH/TO/YOUR/django_projects/MyProject'] + sys.path"

<location "/media">
SetHandler None

<location "/admin_media">
SetHandler None


Be sure to change all the paths to reflect your Django setup.

Next you will have to add all of the DB2 environment variables to the user account that is running Apache by editing, /etc/apache2/envvars and appending the following to the very end of the file.

. /home/db2inst1/sqllib/db2profile

Next make sure that you have created the /media and /admin_media symlinks in the /var/www folder

sudo ln -s /path/to/my/projects/media /var/www/media
sudo ln -s /path/to/django_src/django/contrib/admin/media /var/www/admin_media

Both of these locations also have to be referenced in your prjects file:

# Absolute filesystem path to the directory that will hold user-uploaded files.
# Example:"/home/media/"
MEDIA_ROOT = '/path/to/your/django_project/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:"", ""
MEDIA_URL = '/media/'

# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
# trailing slash.
# Examples:"", ";/media/".
ADMIN_MEDIA_PREFIX = '/admin_media/'

Finally restart Apache and you’re good to go.

sudo /etc/init.d/apache2 rstart

Nnavigate to http://localhost/MyProject and everything should be working.

I have found with this that if you run into any DB2 related errors it is generally caused by environment variables being set incorrectly, and can almost always be remedied by running

. /home/db2inst1/sqllib/db2profile

If I were you I would add it to my /home/user/.bashrc or /home/user/.profile so that it is loaded up every time you open a terminal. Alternatively you can add it to the end of /etc/environment also if you want it to be uses system wide.