I recently had to set up a few CentOS servers with PHP , MySQL , memcached and ketama, and ended up writing this script to do it. You can grab the whole thing from subversion (you’ll need the php folder as well).

if [ ! -d src ]; then
    mkdir src
fi;
cd src


if [ ! -f php-5.2.5.tar.gz ]; then
    wget http://jp2.php.net/get/php-5.2.5.tar.gz/from/jp.php.net/mirror
    tar xvzf php-5.2.5.tar.gz
    sudo yum remove httpd
fi;

if [ ! -f memcached-1.2.5.tar.gz ]; then
    wget http://www.danga.com/memcached/dist/memcached-1.2.5.tar.gz
    tar xvzf memcached-1.2.5.tar.gz
fi;

if [ ! -d ketama ]; then
    svn co svn://svn.audioscrobbler.net/misc/ketama
    cd ketama/libketama
    sed -ri "s|PREFIX=/usr/local|PREFIX=/usr|" Makefile
fi;


sudo yum install \
    libxml2 libxml2-devel \
    openssl openssl-devel \
    pcre pcre-devel \
    bzip2 bzip2-devel \
    curl curl-devel \
    db4 db4-devel \
    libjpeg libjpeg-devel \
    libpng libpng-devel \
    freetype freetype-devel \
    gmp gmp-devel \
    libc-client-devel \
    openldap-devel \
    libmcrypt libmcrypt-devel \
    libmhash libmhash-devel \
    mysql-devel \
    ncurses-devel \
    unixODBC-devel \
    postgresql postgresql-devel \
    sqlite sqlite-devel \
    readline readline-devel \
    net-snmp net-snmp-devel \
    libtidy libtidy-devel \
    libxslt libxslt-devel \
    expat-devel \
    httpd-devel \
    libtool-ltdl libtool-ltdl-devel \
    krb5-libs krb5-devel \
    libevent libevent-devel \
    autoconf \
    bison \
    flex


if [ -d /usr/lib64 ]; then
    export LDFLAGS=-L/usr/lib64/mysql

    if [ ! -f /usr/lib/libexpat.so.32bit ]; then
        sudo mv /usr/lib/libexpat.so /usr/lib/libexpat.so.32bit
    fi;
    sudo rm -f /usr/lib/libexpat.so
    sudo ln -s /usr/lib64/libexpat.so /usr/lib/libexpat.so

    if [ ! -f /usr/lib/libdb-4.3.so.32bit ]; then
        sudo mv /usr/lib/libdb-4.3.so /usr/lib/libdb-4.3.so.32bit
    fi;
    sudo rm -f /usr/lib/libdb-4.3.so
    sudo ln -s /usr/lib64/libdb-4.3.so /usr/lib/libdb-4.3.so

    if [ ! -f /usr/lib/libltdl.so.32bit ]; then
        sudo mv /usr/lib/libltdl.so /usr/lib/libltdl.so.32bit
    fi;
    sudo rm -f /usr/lib/libltdl.so
    sudo ln -s /usr/lib64/libltdl.so /usr/lib/libltdl.so

fi;

cd ketama/libketama
make clean
make
sudo make install
cd ../../
curr_dir=`pwd`
echo ${curr_dir}
rm -f php-5.2.5/ext/ketama
ln -s ${curr_dir}/ketama/php_ketama ${curr_dir}/php-5.2.5/ext/ketama
if [ -d /usr/lib64 ]; then
    sudo rm -f /usr/lib64/libketama.so
    sudo ln -s /usr/lib/libketama.so /usr/lib64/
fi;

cd memcached-1.2.5
make clean
./configure
make
sudo make install
cd ../

cd php-5.2.5
make clean
rm -Rf autom4te.cache
./buildconf --force

'./configure'  \
    '--host=i686-redhat-linux-gnu' \
    '--build=i686-redhat-linux-gnu' \
    '--target=i386-redhat-linux' \
    '--prefix=/usr/local/php-5.2.5'  \
    '--bindir=/usr/bin' \
    '--sbindir=/usr/sbin' \
    '--sysconfdir=/etc' \
    '--datadir=/usr/share' \
    '--includedir=/usr/include' \
    '--libdir=/usr/lib' \
    '--libexecdir=/usr/libexec' \
    '--localstatedir=/var' \
    '--sharedstatedir=/usr/com' \
    '--mandir=/usr/share/man' \
    '--infodir=/usr/share/info' \
    '--cache-file=../config.cache' \
    '--with-libdir=lib' \
    '--with-config-file-path=/etc' \
    '--with-config-file-scan-dir=/etc/php.d' \
    '--disable-debug' \
    '--with-pic' \
    '--disable-rpath' \
    '--with-bz2' \
    '--with-curl' \
    '--with-exec-dir=/usr/bin' \
    '--with-freetype-dir=/usr/lib' \
    '--with-png-dir=/usr/lib' \
    '--enable-gd-native-ttf' \
    '--without-gdbm' \
    '--with-gettext' \
    '--with-gmp' \
    '--with-iconv' \
    '--with-jpeg-dir=/usr/lib' \
    '--with-openssl' \
    '--with-libexpat-dir=/usr/lib' \
    '--with-pcre-regex=/usr' \
    '--with-zlib' \
    '--with-layout=GNU' \
    '--enable-exif' \
    '--enable-ftp' \
    '--enable-magic-quotes' \
    '--enable-sockets' \
    '--enable-sysvsem' \
    '--enable-sysvshm' \
    '--enable-sysvmsg' \
    '--enable-track-vars' \
    '--enable-trans-sid' \
    '--enable-yp' \
    '--enable-wddx' \
    '--with-kerberos' \
    '--enable-ucd-snmp-hack' \
    '--with-unixODBC=shared,/usr' \
    '--enable-memory-limit' \
    '--enable-shmop' \
    '--enable-calendar' \
    '--enable-dbx' \
    '--enable-dio' \
    '--without-mime-magic' \
    '--without-sqlite' \
    '--with-libxml-dir=/usr/lib' \
    '--enable-force-cgi-redirect' \
    '--enable-pcntl' \
    '--with-imap=shared' \
    '--with-imap-ssl' \
    '--enable-mbstring=shared' \
    '--enable-mbstr-enc-trans' \
    '--enable-mbregex' \
    '--with-ncurses=shared' \
    '--with-gd=shared' \
    '--enable-bcmath=shared' \
    '--enable-dba=shared' \
    '--with-db4=/usr/lib' \
    '--with-xmlrpc=shared' \
    '--with-ldap=shared' \
    '--with-ldap-sasl' \
    '--with-mysql=shared,/usr/lib/mysql' \
    '--with-mysqli=shared,/usr/bin/mysql_config' \
    '--enable-dom=shared' \
    '--with-dom-xslt=/usr/lib' \
    '--with-pgsql=shared' \
    '--with-snmp=shared,/usr'\
    '--enable-soap=shared' \
    '--with-xsl=shared,/usr' \
    '--enable-xmlreader=shared' \
    '--enable-xmlwriter=shared' \
    '--enable-fastcgi' \
    '--enable-pdo=shared' \
    '--with-pdo-odbc=shared,unixODBC,/usr' \
    '--with-pdo-mysql=shared,/usr' \
    '--with-pdo-pgsql=shared,/usr' \
    '--with-pdo-sqlite=shared,/usr' '--enable-json=shared' \
    '--enable-zip=shared' \
    '--with-readline' \
    '--enable-dbase=shared' \
    '--with-mcrypt=shared,/usr' \
    '--with-mhash=shared,/usr' \
    '--with-tidy=shared,/usr' '--with-apxs2' \
    '--with-ketama'

sudo yum remove php php-dba php-ldap php-mysql php-mysqli php-pdo

make
sudo make install
cd ../
sudo cp ../php/php.conf /etc/httpd/conf.d/
sudo cp ../php/info.php /var/www/html/
sudo cp ../php/php.ini /etc/php.ini
sudo /usr/sbin/httpd -k stop

* PECL path problem - not sure why
sudo sed -ri "s|exec\(\"php-config --prefix\"\)|\"/usr/lib/20060613\"|" /usr/share/pear/PEAR/Builder.php
sudo pecl install --force memcache

sudo /usr/sbin/httpd -k start

cd ../