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:
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):
This popup a window for the DB2 Setup Launchpad:
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
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.
Now get the sources and lets compile, take note that you are going to want to do this all as the root user
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
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
Now… moving on it’s time to configure your Django project to connect to DB2.
Now your project should have everything it needs to connect to the database and set everything, sync it up using:
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)
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.
Next make sure that you have created the /media and /admin_media symlinks in the /var/www folder
Both of these locations also have to be referenced in your prjects settings.py file:
Finally restart Apache and you’re good to go.
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
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.