Getting IBM DB2/Apache 1.x/PHP4.x to work together on Linux

by Shawn M. Drew (Mar 18, 2001)

Introduction, Resources and Credits

This document will cover configuring php to work with DB2. This expands on Apache and PHP installation instructions given by Nathan Wallace and John Brett from Synop. I have written these instructions while performing the installation on RedHat 7.0 If something is unclear, or you need help with a step because your distribution is different, email me. I have performed this with Apache 1.3.19, PHP 4.0.4p1, and DB2 7.1

Links with related information:

Assumptions:

1. The instructions assume you have installed Linux and have gcc installed and ready to go. If not, head on over to LinuxISO and get a copy (You do have access to a burner right?). I have done this successfully with the default "workstation" install of Redhat 7.0 and "developement" on Mandrake 7.2

2. You have an Internet connection configured

3. You have the DB2 server configured and running. If not, refer to the DB2 Version 7.1 for Linux HOWTO.

Note: DB2 does not need to be on the same machine as Apache/PHP4. While writing this, I have DB2 installed on a seperate machine. All that matters is that the DB2 server is reachable through the network with no firewall in between. The scope of this document does not cover firewall configuration to allow DB2 traffic

4. You have the media and licensing for the DB2 Connect package if the DB2 service is installed on a remote machine, or the DB2 Server media if installed locally. While writing this I am using an untar'd DB2 Connect EE.

Preparing your system for installation

1. Log in as root if not already

su root

2. I like to have a "clean" install so I dont want multiple copies of Apache and PHP floating around. Check if you have any components installed.

rpm -qa | grep php

rpm -qa | grep -i apache

If it shows any packages, they should be removed with:

rpm -e <package_name>

These packages may have to be removed in a specific order because of their dependencies. Basically if one fails, come back to it later. Note: this is only a preference of mine, and they dont have to be removed.

3. ksh needs to be installed for the db2 installation script to run.

If you are running RedHat 7.0: insert disc2

mount /mnt/cdrom (if the cdrom doesn't mount automatically)

rpm -Uvh /mnt/cdrom/RedHat/RPMS/pdksh-5.2.14-8.i386.rpm

4. The ncurses library needs to be linked so that the DB2 installer can find its version. Mandrake does this by default, but Redhat doesn't.

on Redhat 7.0:

cd /usr/lib

ls libncurses* (you should see a number of "libncurses" files. if not, install the ncurses-5.1-2 package from Disc 1

ln -s libncurses.so.5.1 libncurses.so.4

Installing the DB2 client

1. Insert the DB2 Connect CD, mount it, and change to the directory

mount /mnt/cdrom (if the cdrom doesn't mount automatically)

cd /mnt/cdrom (or directory of the untar'd file if downloaded)

2. Run the DB2 installation program

./db2setup

Note: The DB2 installation has this annoying property of filling the screen with "old" characters. To work arround this, just hit <CTRL><L> to clear these characters out when you are getting annoyed.

3. Select "DB2 Administration Client" and "DB2 Application Development Client" then select <OK>

Note: The "Application Development Client" is only needed to compile PHP. It can be removed afterward if you do not plan on re-compiling php once everything works

4. Select "Create a DB2 instance"

The default settings that pop up should be fine, just enter a password under the specified sections then select <OK>

5. Begin the installation

select <OK> to go to the "Summary Report" then select <Continue> then <OK> to begin installation

6. Exit the installation program

Configuring the DB2 client

1. switch to the db2 instance user that was created during the installation (to get the environment)

su - db2inst1

3. Configure the connection to the DB2 server

db2 catalog tcpip node delilah remote delilah.shawndo.net server 50000

where:

"delilah" is the name you want to give to this connection. I always just use the hostname of the machine with the db2 service (localhost if local)

"delilah.shawndo.net" is the hostname of the machine with the db2 service (localhost if local)

Note: The DB2 documentation says that you can use an IP address for this value instead, but I have never done this successfully. When testing the connection, I always get a "SQL30081N Communication Error" So if you can not resolve this machine through DNS, add the hostname and IP address to the /etc/hosts file

"50000" is the tcp port that the instance containing the desired database is listening

Note: The port number can be obtained by starting the control center on the DB2 server, right-click the instance name (default is DB2) and select "Setup communications" then click on "Properties" next to "TCP/IP" to show the port number

db2 terminate (not sure what this does, but the db2 manual says to do it....

4. Configure the connection to the database. In these examples, I am connecting to the sample database that DB2 comes with.

db2 catalog database sample as sample at node delilah

where:

"sample"(1st occurance) is the name of the database as it is named on the db2 server

"sample"(2nd occurance) is the alias you want to give this database when referring to it (I just use the same name) The important thing is that this alias is unique on this client. This is the value you will use in your php code when referring to this database.

"delilah" is the node name that you specified in step 3

db2 terminate

5. Test your connection to the database

- db2 (To start the db2 command line processor)

- connect to sample user <user> using <password>

where:

"sample" is the alias you gave to the database in Step4

"<user>" is an account on the DB2 server that has access to this database

"<password" is the password for the account

- You should receive a message like the one below:

Database Connection Information

Database server = DB2/NT 7.1.0
SQL authorization ID = DB2
Local database alias = SAMPLE

- select firstnme from employee (Simple query)

Installing Apache

1. Install and configure Apache according to these instructions:

Installing Apache 1.x.x on Linux

Note: Before starting the Apache server (./apachectl start) the db2 profile needs to be sourced. this can be done with:

. /home/db2inst1/sqllib/db2profile

then you can start apache with ./apachectl start

Installing PHP4

1. Add library directories to /etc/ld/so/conf

su root

echo "/usr/IBMdb2/V7.1/lib" >> /etc/ld.so.conf

echo "/usr/IBMdb2/V7.1/include" >> /etc/ld.so.conf

ldconfig

Note: I am not a developer and do not know if this is the "correct" way to do things, but it is the only way I was able to get this to work.

2. Install and configure PHP4 according to these instructions:

Installing PHP 4.x for Apache 1.x.x on Linux (with the following modifications)

- Do not perform sections 2-5 under "Compiling PHP". Instead use this command:

./configure --with-apache=../httpd --with-config-file-path=/www/conf --with-ibm-db2 --enable-track-vars

Note: This is assuming that you let db2 create the default instance id as "db2inst1" in Step 4 of "Installing the DB2 client". If not, change the option "--with-ibm-db2" to read --with--ibm-db2=/home/<instanceUID>/sqllib for example:

./configure --with-apache=../httpd --with-config-file-path=/www/conf --with-ibm-db2=/home/db2inst2/sqllib --enable-track-vars

- Under step 2 under "Check that it Works", I was not able to get the ./apachectl status command to work without modifying the /www/conf/httpd.conf. I can tell that this isn't a good way to do it, but maybe just change it temporarily to see the status, or figure out the proper way to do it.

I had to change the following lines:

#<Location /server-status>
# SetHandler server-status
# Order deny,allow
# Deny from all
# Allow from .shawndo.net
#</Location>

to read:

<Location /server-status>
SetHandler server-status
Allow from All
</Location>

 

Checking that it all works together

1. First look at the php information

echo "<?php phpinfo(); ?>" > /www/servers/phpinfo.php

Bring up a web browser, and look at the page http://<hostname>/phpinfo.php

Look for the section beginning with "ODBC". If you see this, then php was successfully compiled.

2. Create a simple page that queries sample database (to make sure everything is working together.

You need to have the information that you used in step 5, under "Configuring the DB2 client. "database alias, username, and password" In the following example, I am using "sample, db2,db2" respectively.

Create a document called /www/servers/phpdbtest.php and insert the following (change the $dsn, $user, and $passwd to fit your setup):

<html>
<head>
<title></title>
</head>
<body

<?php

$dsn = sample;
$user = db2;
$passwd = db2;

$conn = odbc_connect($dsn,$user,$passwd );

echo "conn: $conn";
if ($conn <= 0) {
echo "Error in connection<BR>";
exit;
}
else {
echo "<P>Connection successful\n";
};

$query = "SELECT firstnme FROM EMPLOYEE";
$result = odbc_Exec($conn, $query);

odbc_result_all($result);


odbc_Close($conn);

?>


</body>
</html>

3. Bring up a web browser, and look at the page http://<hostname>/phpdbtest.php

- if you used the same sample database I used, your output should look something like this:

Connection successful
FIRSTNME
CHRISTINE
MICHAEL
SALLY
JOHN
IRVING
EVA
EILEEN
THEODORE
VINCENZO