If you are planning to use PHP with DB2 Express-C on Linux, you will need PHP extension for the DB2. The same extension will also work for IBM Cloudscape or Apache Derby databases.

[Note: The instructions here are for the Redhat Linux and you will need slight modification in RPM SPEC file should you need to use it on other Linux distributions.]

As of writing today, the IBM_DB2 extension is not enabled by default for the Linux distributions when you install PHP. This may change in the future and until then, take help from this article for building RPM from the source or download two RPMs that I built for 32 bit RHEL 4 and 64 bit Fedora Core 5. You can also download the source RPM or source tarball and build the RPM for your Linux distribution after you make required changes to the SPEC file. I think that the changes will be very minimum.

I did not know how to create RPM from a source so these are the steps that I followed after learning it by looking how other people build the RPM. You might not want to go through this whole article if you arrived here to look only for a ready made binary RPM for the IBM_DB2 PHP extension. Read software requirement section and just go to the end of this article to download the binary RPM if it matches to your platform. If not, download the source RPM or source tarball and build it yourself. If you do not know how to build a binary RPM from a source tarball, read the last section.

1. Software requirement before you start

  • You should already have either DB2 server or DB2 client installed on your machine.
  • You should already have Apache or IBM httpd installed and running with PHP module enabled.
  • You should already have PHP (> 4.3) installed
  • If you need to build the binary RPM from the source RPM, you will also need to install php-devel. If you will be using binary RPM available from this site, you will not need php-devel.

2. How to install DB2 on your system

Did you know that you can install a free license (permanent with no expiry) of DB2 from IBM for your use? This version of DB2 is called DB2 Express-C and it can be downloaded from the IBM site at http://www-306.ibm.com/software/data/db2/express/. You can use up to 4GB of memory with unlimited database size on a dual core processor or 2 processor machine. This does not mean that you can not install DB2 Express-C on a bigger machine but it will not use more than 2 processor and 4GB of the memory.

3. Where can I find PHP connector?

This part is easy and probably you already know this link http://pecl.php.net/package/ibm_db2.

4. Do I really need a RPM to connect to DB2 from PHP

No. If you have PHP working for you, you can use PECL to install it directly from the PHP extension site. Check if you have pecl installed.

$ which pecl
/usr/bin/pecl

If yes, just run this simple command and you are ready to go.

$ pecl install ibm_db2

Or, download the source from the link and compile it yourself.

But, hang on. PECL will not modify your php.ini file and it will not restart httpd server. The RPM will do those things for you. If you will need to roll out this on many machines, you will want RPM to do this automatically for you.

Using PECL is advantageous as it will use the latest driver but you should have right environment available for PECL to work properly. I recommend to use PECL approach as it is simple.

5. How do I build RPM from the source?

Run the following command.

# rpm --eval %_topdir

I have different output on my 2 machines as shown below.

# rpm --eval %_topdir
/usr/src/redhat

# rpm --eval %_topdir
/root/rpm

Go to the above directory to check if you have following folders in it. If not, create them.

# cd `rpm --eval %_topdir`
# ls -l
drwxr-xr-x 3 root root 4096 Feb 15 20:58 BUILD
drwxr-xr-x 3 root root 4096 Feb 14 00:28 RPMS
drwxr-xr-x 2 root root 4096 Feb 13 21:42 SOURCES
drwxr-xr-x 2 root root 4096 Feb 15 21:01 SPECS
drwxr-xr-x 2 root root 4096 Feb 15 19:34 SRPMS

Get the latest or desired version of source from PHP PECL site and put it in the SOURCES directory.

# cd `rpm --eval %_topdir`/SOURCES
# wget http://pecl.php.net/get/ibm_db2-1.6.0.tgz

[Note: Replace ibm_db2-1.6.0.tgz with the actual or latest file name.]

Go to SPECS directory and download the SPECS files from this site.

# cd `rpm --eval %_topdir`/SPECS
# wget http://www.db2ude.com/files/ibm_db2.spec

The RPM specification file is shown as below:

#
# rpm spec file for IBM_DB2 for PHP
#
Summary: This rpm installs ibm_db.so in php extention dir
Name: ibm_db2
Version: 1.6.0
Release: 1
Group: Applications/Drivers
Source: http://pecl.php.net/get/%{name}-%{version}.tgz
License: GPL
Distribution: Linux
ExcludeArch: sparc s390 s390x
Excludeos: windows
BuildRequires: php >= 4.3
BuildRequires: php-devel >= 4.3
Provides: ibm_db2
%description
The IBM_DB2 is a shared library used by PHP to connect / access to DB2.
%prep

# Check DB2 Version 8 if it is installed

db2path=null
db2found=null
db2clifound=null
db2instance=null
if [ -d /opt/IBM/db2/V8.1 ]
then
   db2found=yes
   db2path=/opt/IBM/db2/V8.1
   db2instance=`$db2path/bin/db2ilist | sed '1q'`
   if [ -f ${db2path}/include/sqlcli1.h ]
   then
       db2clifound=yes
   fi
fi

# Check later versions of DB2 if DB2 V8 is not found

if [ "${db2path}" == "null" ] ;
then
  if [ -f /usr/local/bin/db2ls ]
  then
     db2found=yes
     db2path=`db2ls | sed '$!d' | sed 's/ .*//'`
     db2instance=`$db2path/bin/db2ilist | sed '1q'`
     if [ -f ${db2path}/include/sqlcli1.h ]
     then
        db2clifound=yes
     fi
   fi
fi
rm -f /tmp/____db2path.tmp
if test "$db2found" == "null" ; then
  : DB2 not found. Aborting ....
  exit 1
fi
if test "$db2clifound" == "null" ; then
  : DB2 Client Header Files not found in ${db2path}/include. Aborting ...
  exit 1
fi
echo ${db2path} > /tmp/____db2path.tmp
echo ${db2instance} > /tmp/____db2instance.tmp
%define DB2_PATH %(cat /tmp/____db2path.tmp)
%define DB2_INSTANCE %(cat /tmp/____db2instance.tmp)
%setup -q
phpize
%build
echo DB2_PATH=%{DB2_PATH}
%configure --with-IBM_DB2=%{DB2_PATH}
echo DB2_PATH=%{DB2_PATH}
if test $? != 0; then
  tail -500 config.log
  : configure failed
  exit 1
fi
make
%install
rm -rf $RPM_BUILD_ROOT
%makeinstall
echo '; Enable ibm_db2 extension module' > /etc/php.d/ibm_db2.ini
echo extension=ibm_db2.so >> /etc/php.d/ibm_db2.ini
echo '; We pick up the name of the first instance' >> /etc/php.d/ibm_db2.ini
echo '; Change the db2 instance name if desired' >> /etc/php.d/ibm_db2.ini
echo ibm_db2.instance_name=%{DB2_INSTANCE} >> /etc/php.d/ibm_db2.ini
%clean
rm -rf $RPM_BUILD_ROOT
%files
/etc/php.d/ibm_db2.ini

# Restart Apache Web Server since we added ibm_db2.ini for PHP

%post
if [ -x /etc/init.d/apache2 ]; then
  /etc/init.d/apache2 restart
elif [ -x /sbin/service ]; then
  /sbin/service httpd restart
fi
%changelog


* Tue Mar 13 2007 Vikram Khatri 

- Fixed for db2 instance

* Tue Feb 14 2007 Vikram Khatri 

- Downloaded version 1.6.0

Basically the above file has instructions for rpm command to compile / build / install IBM_DB2 extension for PHP. The above spec file does the following.

  • Check if DB2 V8 or DB2 9 installed.
  • Check if you have PHP > 4.3 installed.
  • Check if you have php-devel installed.
  • Extract source in BUILD directory
  • Run phpize command to create configure file
  • Build the extension and install it
  • Create ibm_db2.ini file in php.d folder
  • Create ibm_db2.instance_name entry to the name of the first db2 instance
  • Restart the httpd server
  • Clean-up.

To build the source and binary rpm using above spec file, run the following commands.

# cd `rpm --eval %_topdir`/SPECS
# rpmbuild -ba ibm_db2.spec

The rpmbuild command will build the software and install it. It will also generate binary and source RPM for you in RPMS and SRPMS folders.

[root@gopal redhat]# ls -lr RPMS
total 4
drwxr-xr-x 2 root root 4096 Feb 15 19:34 x86_64
[root@gopal redhat]# ls -lR RPMS
RPMS:
total 4
drwxr-xr-x 2 root root 4096 Feb 15 19:34 x86_64

RPMS/x86_64:
total 4
-rw-r--r-- 1 root root 2375 Mar 13 18:09 ibm_db2-1.6.0-1.x86_64.rpm
[root@gopal redhat]# ls -lR SRPMS
SRPMS:
total 132
-rw-r--r-- 1 root root 127111 Mar 13 18:09 ibm_db2-1.6.0-1.src.rpm

6. How do I build RPM from the source tarball?

Ok, you have downloaded the source tarball from this site to your machine and you are wondering as how do I build the binary RPM from it since you did not get the binary RPM for your Linux distribution. Make sure that you have all the software installed as mentioned in the first section.

[Note: The IBM_DB2 source tarball from PHP site does not have spec file in it as of today. You should download the latest source tarball from the PHP site and use the SPEC file as mentioned here. After you untar the source, just copy the spec file in it and again tar it back.

Run following commands on your machine.

# tar xvfz ibm_db2-1.6.0.tgz
# cd ibm_db2-1.6.0
# wget http://www.db2ude.com/files/ibm_db2.spec
# cd ..
# rm -f ibm_db2-1.6.0.tgz
# tar cvfz ibm_db2-1.6.0.tgz ibm_db2-1.6.0
# rpmbuild -tb ibm_db2-1.6.0.tgz

[Note: Replace ibm_db2-1.6.0.tgz with the actual file that you download from this http://pecl.php.net/package/ibm_db2 site.]

The rpmbuild -tb command will install the software on your machine and will also create a RPM for you which you can use to distribute. Look the output from the above command and you will see something similar as shown below.

Wrote: /usr/src/redhat/SRPMS/ibm_db2-1.6.0-1.src.rpm
Wrote: /usr/src/redhat/RPMS/x86_64/ibm_db2-1.6.0-1.x86_64.rpm

7. Download

IBM_DB2 spec file: http://www.db2ude.com/files/ibm_db2.spec
IBM_DB2 Source tarball: http://www.db2ude.com/files/ibm_db2-1.6.0.tgz
IBM_DB2 Source RPM: http://www.db2ude.com/files/ibm_db2-1.6.0-1.src.rpm
IBM_DB2 RPM for RHEL 3 (32 bit): http://www.db2ude.com/files/ibm_db2-1.6.0-1.i386.rpm
IBM_DB2 RPM for Fedora Core 5 : http://www.db2ude.com/files/ibm_db2-1.6.0-1.x86_64.rpm

8. Read this if you have more than one DB2 instance

If you have more than one DB2 instance on your server, read this section otherwise you are all set.

Before you test the driver, you need to make sure if you need to change the db2 instance name in /etc/php.d/ibm_db2.ini. Be default, RPM uses the name of the first DB2 instance.

For example if you have two instances of DB2 on your server and you want to use the name of the second instance, you will need to modify the entry ibm_db2.instance_name= in your /etc/php.d/ibm_db2.ini file.

You can run db2 command db2ilist to find out the list of DB2 instances on your server. This command is available in DB2 installation directory. Or, you can log in as a DB2 instance owner and run the command db2ilist.

If made any change in the ibm_db2.ini file, please remember to restart your apache server.

9. Test DB2 driver

After you installed RPM, you are now ready to test your PHP connection to the database.

Create this sample PHP page in your web directory and make changes to the $db_name, $usr_name and $password.

<?php
   $db_name = 'SAMPLE';
   $usr_name = 'db2inst1';
   $password = 'password';
   // For persistent connection change db2_connect to db2_pconnect
   $conn_resource = db2_connect($db_name, $usr_name, $password);
   if ($conn_resource) {
     echo 'Connection to database succeeded.';
     db2_close($conn_resource);
   } else {
     echo 'Connection to database failed.';
     echo 'SQLSTATE value: ' . db2_conn_error();
     echo 'with Message: ' . db2_conn_errormsg();
   }
?>