Wednesday, September 23, 2009

Using Berkeley DB with PHP5

Intro


Although the official documentation is a bit old and refers to PHP4 only, the db4 extension works fine with PHP5 too.

Nobody has packaged it perhaps why web developers only use relational databases (but to shoot a fly you don't need a cannon and BDB has its own strenghts). So you have to compile it yourself. Here's how.

I know PHP has a generic interface to dbm-like file-based databases. This is not enough for me, because it's limited to a subset of features common to all supported backends. I want to use the full power of latest Berkeley DB versions.

Download the sources


You have basically two choices here: link db4 against the best BDB version found in your system or download the latest. Of course the latter is a choice only if your system does not already come with latest BDB. If you choose the first it may help to see if there is a source package or equivalent that generates the binary package you have already installed. E.g. this is the case with Arch Linux.

Compile


Make sure you have autoconf, otherwise install it. Some distros (e.g. Debian) require you to install such packages as make, g++ and php5-dev too.

My favorite linux distro makes my life simpler once again: to add BDB support to PHP I just need to grab the latest db core package PKGBUILD and related files and add some simple commands at the end of the build() shell function. The KISS principle rocks!

cd ${srcdir}/$pkgname-${_basever}/php_db4
  phpize || return 1
  ./configure --prefix=/usr || return 1
  make || return 1
  make INSTALL_ROOT=${pkgdir} install || return 1
  echo ';extension=db4.so' > db4.ini
  install -Dm644 db4.ini ${pkgdir}/etc/php/conf.d/db4.ini

Then do a makepkg, et voilĂ ! For reference this is the full PKGBUILD I used (may change in the future):

# $Id$
# Maintainer: Allan McRae 
# Contributor: Andreas Radke 

pkgname=db
pkgver=4.7.25.4
_basever=4.7.25
pkgrel=1
pkgdesc="The Berkeley DB embedded database system (with PHP support)"
arch=('i686' 'x86_64')
url="http://www.oracle.com/technology/software/products/berkeley-db/index.html"
license=('custom')
groups=('base')
depends=('gcc-libs' 'sh' 'php')
options=('!libtool')
install=db.install
source=(http://download-uk.oracle.com/berkeley-db/db-${_basever}.tar.gz
        #post release upstream patches
        http://www.oracle.com/technology/products/berkeley-db/db/update/4.7.25/patch.4.7.25.1
        http://www.oracle.com/technology/products/berkeley-db/db/update/4.7.25/patch.4.7.25.2
        http://www.oracle.com/technology/products/berkeley-db/db/update/4.7.25/patch.4.7.25.3
        http://www.oracle.com/technology/products/berkeley-db/db/update/4.7.25/patch.4.7.25.4)
backup=('etc/php/conf.d/db4.ini')
md5sums=('ec2b87e833779681a0c3a814aa71359e'
         '5fdf101259e5164dea1c8c86214fde38'
         'bd410a11c71fee52fddb6aa2d8d4f80c'
         '6fcd69f64f5b34bfe8f0a63cc2e402c1'
         'c71830a1303cd34595ca655257196eec')

build() {
  cd ${srcdir}/$pkgname-${_basever}
  patch -Np0 -i ${srcdir}/patch.4.7.25.1 || return 1
  patch -Np0 -i ${srcdir}/patch.4.7.25.2 || return 1
  patch -Np0 -i ${srcdir}/patch.4.7.25.3 || return 1
  patch -Np0 -i ${srcdir}/patch.4.7.25.4 || return 1
  cd ${srcdir}/$pkgname-${_basever}/build_unix
  ../dist/configure --prefix=/usr --enable-compat185 \
    --enable-shared --enable-static --enable-cxx
  make LIBSO_LIBS=-lpthread || return 1
  make DESTDIR=${pkgdir} install
  rm -rf ${pkgdir}/usr/docs
  install -Dm644 ${srcdir}/${pkgname}-${_basever}/LICENSE ${pkgdir}/usr/share/licenses/${pkgname}/LICENSE

  cd ${srcdir}/$pkgname-${_basever}/php_db4
  phpize || return 1
  ./configure --prefix=/usr || return 1
  make || return 1
  make INSTALL_ROOT=${pkgdir} install || return 1
  echo ';extension=db4.so' > db4.ini
  install -Dm644 db4.ini ${pkgdir}/etc/php/conf.d/db4.ini
}

Enable


Only one thing is left before you can dive into programming Berkeley DB databases in PHP: enable the extension by editing /etc/php/php.ini (or wherever it is located in the linux flavor you're using) and adding (usually at the end of file):

extension=db4.so

In Arch Linux you just need to uncomment a line in /etc/php/conf.d/db4.ini thanks to the extra code in my PKGBUILD above.

Don't forget to gracefully reload Apache to make this change effective. In Arch Linux:

/etc/rc.d/httpd reload

Check


To make sure your extension is loaded examine the output from:

<?php
  phpinfo();
?>

You should see that "db4 support" is "enabled".

No comments: