Wednesday, September 23, 2009

Using Berkeley DB with PHP5


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.


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 ';' > 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 

pkgdesc="The Berkeley DB embedded database system (with PHP support)"
arch=('i686' 'x86_64')
depends=('gcc-libs' 'sh' 'php')
        #post release upstream patches

build() {
  cd ${srcdir}/$pkgname-${_basever}
  patch -Np0 -i ${srcdir}/patch. || return 1
  patch -Np0 -i ${srcdir}/patch. || return 1
  patch -Np0 -i ${srcdir}/patch. || return 1
  patch -Np0 -i ${srcdir}/patch. || 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 ';' > db4.ini
  install -Dm644 db4.ini ${pkgdir}/etc/php/conf.d/db4.ini


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):

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


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


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

No comments: