Skip to content

Instantly share code, notes, and snippets.

@jmikola
Last active March 10, 2017 06:17
Show Gist options
  • Save jmikola/f19db4e8de105ee60c06 to your computer and use it in GitHub Desktop.
Save jmikola/f19db4e8de105ee60c06 to your computer and use it in GitHub Desktop.
Building and managing multiple versions of PHP
#!/bin/sh
set -e
base_install_dir="$HOME/bin/php-bin"
download_dir="$HOME/bin/php-cache"
php_tarball="" # set after calling get_php()
debug() {
echo "===> $1"
}
get_php() {
if [ ! -d "$download_dir" ]; then
debug "Creating download directory: $download_dir"
mkdir -p $download_dir
fi
php_tarball="$download_dir/php-$1.tar.gz"
if [ ! -f "$php_tarball" ]; then
local url="http://php.net/get/php-$1.tar.gz/from/this/mirror"
debug "Downloading: $url => $php_tarball"
curl -fsSL -o $php_tarball $url
else
debug "Using previously downloaded file: $php_tarball"
fi
}
build_php() {
local version=$1
local label=$2
if [ -z "$label" ]; then
debug "No label provided; using: $version"
label="$version"
fi
local tmp_dir=`mktemp -d --tmpdir "build_php-$version.XXX"`
local build_dir="$tmp_dir"
local install_dir="$base_install_dir/php-$label"
local etc_dir="$base_install_dir/etc-$label"
local conf_dir="$etc_dir/conf.d"
local configopts=""
get_php $version
debug "Unpacking: $php_tarball => $build_dir"
tar xzf $php_tarball -C $build_dir
if [ -d "$build_dir/php-$version" ]; then
build_dir="$build_dir/php-$version"
debug "Changed build directory to: $build_dir"
fi
# Set configuration options
for enable in bcmath cgi dom json libxml pcntl phar posix session xml; do
configopts="$configopts --enable-$enable"
done;
for with in openssl pear zlib; do
configopts="$configopts --with-$with"
done;
# Append ZTS config option if necessary
if [ -n "$3" ]; then
debug "Detected ZTS config option; adding build flag"
configopts="$configopts --enable-maintainer-zts"
fi
# Create configuration paths if necessary
if [ ! -d "$etc_dir" ]; then
debug "Creating config file directory: $etc_dir"
mkdir -p $etc_dir
fi
if [ ! -d "$conf_dir" ]; then
debug "Creating config file scan directory: $conf_dir"
mkdir -p $conf_dir
fi
# Set the config paths and target directory for installation
configopts="$configopts --with-config-file-path=$etc_dir"
configopts="$configopts --with-config-file-scan-dir=$conf_dir"
configopts="$configopts --prefix=$install_dir"
local build_log=`mktemp --tmpdir "build_php-$version.log.XXX"`
debug "Building and installing to: $install_dir"
debug "Build output will be logged to: $build_log"
cd $build_dir
./configure $configopts >> $build_log 2>&1
make all -j8 >> $build_log 2>&1
make install >> $build_log 2>&1
debug "Build complete; deleting temporary directory: $tmp_dir"
rm -r $build_dir
}
if [ -z $1 ]; then
echo "Usage: $0 version [label] [zts]"
echo " version: Full x.y.z version (e.g. 5.5.15)"
echo " label: Short version for install path (e.g. 5.5); defaults to full version"
echo " zts: Add --enable-maintainer-zts build flag if 1; defaults to false"
exit 1
fi
build_php $1 $2 $3
# This file should be sourced from ~/.bashrc (or equivalent)
# Usage: pe version
# version: label corresponding build_php.sh
pe () {
bindir="$HOME/bin/php-bin/php-$1/bin"
if [ -x "$bindir/php" ]; then
PATH="$bindir:$PATH"
# Trim duplicate entries from PATH (see: https://sites.google.com/site/jdisnard/path-dupes)
PATH=`awk -F: '{for (i=1;i<=NF;i++) { if ( !x[$i]++ ) printf("%s:",$i); }}' <<< $PATH`
else
echo "Can't find $bindir/php or it isn't executable"
return 1
fi
}
[PHP]
date.timezone = "America/New_York"
display_errors = On
display_startup_errors = On
error_reporting = -1
log_errors = On
memory_limit = -1
report_memleaks = On
report_zend_debug = 0
short_open_tag = Off
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment