Installing multiple versions of PHP from source

With the recent release of PHP 5.4-RC1, I wanted to give it a spin and make sure there weren’t any upcoming issues for CakePHP. I recently saw a great article from Derick Rethans on getting PHP setup from an SVN checkout. I found getting the shallow SVN checkout he used a bit of a pain. Thankfully, there is a github mirror of the PHP source code.

After cloning the php-src repo locally into ~/Sites/php/php-src, I started adapting Derick’s script for my own purposes. I don’t use a number of extensions that he had enabled, so I opted out of those. Something is not right with the iconv libraries available on Snow Leopard, so I had to disable iconv as well. I also have Postgres and MySQL installed through macports, so I need to update the paths for those. The script I ended up with looks like:

Show Plain Text
  1. #!/bin/sh
  2.  
  3. VERSION=$1
  4. DEBUG=$2
  5. ZTS=$3
  6. THIRTYTWO=$4
  7. POSTFIX=
  8. EXTRA_FLAGS=
  9. PHP_DIR=/Users/markstory/Sites/php/php-src
  10.  
  11. if (test "${DEBUG}" = "nodebug"); then
  12.     POSTFIX="$POSTFIX-nodebug"
  13. else
  14.     EXTRA_FLAGS="$EXTRA_FLAGS --enable-debug"
  15. fi
  16.  
  17. if (test "${ZTS}" = "zts"); then
  18.     EXTRA_FLAGS="$EXTRA_FLAGS --enable-maintainer-zts"
  19.     POSTFIX="$POSTFIX-zts"
  20. fi
  21.  
  22. if (test "${THIRTYTWO}" = "32bit"); then
  23.     export CFLAGS="-m32"
  24.     POSTFIX="$POSTFIX-32bit"
  25. fi
  26.  
  27. SCRUBBED=`echo $VERSION | sed 's/[0-9.]*//'`
  28. if (test "${SCRUBBED}" = "dev"); then
  29.     BRANCH=`echo ${VERSION} | sed 's/dev//' | sed 's/\./_/g'`
  30.     cd $PHP_DIR
  31.     git checkout "PHP_$BRANCH"
  32. else
  33.     TAG=`echo ${VERSION} | sed 's/\./_/g'`
  34.     cd $PHP_DIR
  35.     git checkout "PHP_$TAG"
  36. fi
  37.  
  38. echo "Building ${VERSION}${POSTFIX} with ($EXTRA_FLAGS)"
  39.  
  40. make clean
  41. rm -rf configure
  42. ./vcsclean
  43. ./buildconf --force
  44.  
  45. if (test "${THIRTYTWO}" = "32bit"); then
  46.     OPTIONS="--disable-all"
  47. else
  48.     OPTIONS="--with-xpm-dir=/usr \
  49.         --with-apxs2=/opt/local/apache2/bin/apxs
  50.         --with-mysql=mysqlnd \
  51.         --enable-bcmath \
  52.         --enable-pcntl \
  53.         --enable-libxml \
  54.         --enable-dom \
  55.         --enable-memory-limit \
  56.         --enable-filter \
  57.         --enable-simplexml \
  58.         --enable-tokenizer \
  59.         --enable-debug \
  60.         --enable-spl \
  61.         --enable-json \
  62.         --enable-pdo \
  63.         --enable-exif \
  64.         --enable-mbstring \
  65.         --without-iconv \
  66.         --with-gmp \
  67.         --with-readline=/opt/local \
  68.         --with-openssl \
  69.         --with-curl \
  70.         --with-mysqli=mysqlnd \
  71.         --with-mcrypt \
  72.         --with-memcache=/usr/local \
  73.         --with-pdo-pgsql=/opt/local/lib/postgresql90/bin \
  74.         --with-pgsql=/opt/local/lib/postgresql90/bin \
  75.         --with-pdo-mysql=mysqlnd \
  76.         --with-pdo-sqlite \
  77.         --with-ctype \
  78.         --with-bz2=/opt/local \
  79.         --with-zlib \
  80.         --with-xsl"
  81. fi
  82.  
  83. ./configure \
  84. --prefix=/usr/local/php/${VERSION}${POSTFIX} ${EXTRA_FLAGS} ${OPTIONS}
  85.  
  86. make -j 5
  87. make install

I put this script in ~/Sites/php. I didn’t really like the way Derick changed his path around. Instead I decided to add /usr/local/php/active to my PATH, and use a script to switch to one of the PHP versions I’ve installed. In ~/Sites/php/link.sh I put:

Show Plain Text
  1. #!/bin/sh
  2.  
  3. PHP_DIR=/usr/local/php
  4.  
  5. echo "Remove old link."
  6. rm $PHP_DIR/active
  7.  
  8. if [[ ! -d "$PHP_DIR/$1" ]]
  9. then
  10.     echo "Cant find that version of PHP"
  11.     exit 1
  12. fi
  13.  
  14. echo "Creating new link."
  15. ln -s "$1/bin" "$PHP_DIR/active"

After I’ve compiled the various versions of PHP I want to use, I can switch between them using ~/php/link.sh 5.4-dev. While it took a bit of work to get going, finally knowing how, and being able to re-build PHP from source is well worth it.

Comments

Well… how was Cake with 5.4? :-)

Ryan on 12/13/11

Ryan: It wasn’t all rainbows at the start, but things work great now :)

mark story on 12/21/11

You create the wrong tag names, here’s a fix:

if (test "${SCRUBBED}" = "dev"); then BRANCH=`echo ${VERSION} | sed 's/dev//'` cd $PHP_DIR git checkout "PHP-$BRANCH" else cd $PHP_DIR git checkout "php-$VERSION" fi

Basically you dont need to convert “.” to “_”.
See: https://github.com/php/php-src/releases

David Steinsland on 9/4/13

Comments are not open at this time.