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:
- #!/bin/sh
- VERSION=$1
- DEBUG=$2
- ZTS=$3
- THIRTYTWO=$4
- POSTFIX=
- EXTRA_FLAGS=
- PHP_DIR=/Users/markstory/Sites/php/php-src
- if (test "${DEBUG}" = "nodebug"); then
- POSTFIX="$POSTFIX-nodebug"
- else
- EXTRA_FLAGS="$EXTRA_FLAGS --enable-debug"
- fi
- if (test "${ZTS}" = "zts"); then
- EXTRA_FLAGS="$EXTRA_FLAGS --enable-maintainer-zts"
- POSTFIX="$POSTFIX-zts"
- fi
- if (test "${THIRTYTWO}" = "32bit"); then
- export CFLAGS="-m32"
- POSTFIX="$POSTFIX-32bit"
- fi
- SCRUBBED=`echo $VERSION | sed 's/[0-9.]*//'`
- if (test "${SCRUBBED}" = "dev"); then
- BRANCH=`echo ${VERSION} | sed 's/dev//' | sed 's/\./_/g'`
- cd $PHP_DIR
- git checkout "PHP_$BRANCH"
- else
- TAG=`echo ${VERSION} | sed 's/\./_/g'`
- cd $PHP_DIR
- git checkout "PHP_$TAG"
- fi
- echo "Building ${VERSION}${POSTFIX} with ($EXTRA_FLAGS)"
- make clean
- rm -rf configure
- ./vcsclean
- ./buildconf --force
- if (test "${THIRTYTWO}" = "32bit"); then
- OPTIONS="--disable-all"
- else
- OPTIONS="--with-xpm-dir=/usr \
- --with-apxs2=/opt/local/apache2/bin/apxs
- --with-mysql=mysqlnd \
- --enable-bcmath \
- --enable-pcntl \
- --enable-libxml \
- --enable-dom \
- --enable-memory-limit \
- --enable-filter \
- --enable-simplexml \
- --enable-tokenizer \
- --enable-debug \
- --enable-spl \
- --enable-json \
- --enable-pdo \
- --enable-exif \
- --enable-mbstring \
- --without-iconv \
- --with-gmp \
- --with-readline=/opt/local \
- --with-openssl \
- --with-curl \
- --with-mysqli=mysqlnd \
- --with-mcrypt \
- --with-memcache=/usr/local \
- --with-pdo-pgsql=/opt/local/lib/postgresql90/bin \
- --with-pgsql=/opt/local/lib/postgresql90/bin \
- --with-pdo-mysql=mysqlnd \
- --with-pdo-sqlite \
- --with-ctype \
- --with-bz2=/opt/local \
- --with-zlib \
- --with-xsl"
- fi
- ./configure \
- --prefix=/usr/local/php/${VERSION}${POSTFIX} ${EXTRA_FLAGS} ${OPTIONS}
- make -j 5
- 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:
- #!/bin/sh
- PHP_DIR=/usr/local/php
- echo "Remove old link."
- rm $PHP_DIR/active
- if [[ ! -d "$PHP_DIR/$1" ]]
- then
- echo "Cant find that version of PHP"
- exit 1
- fi
- echo "Creating new link."
- 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.
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