Skip to content

Instantly share code, notes, and snippets.

@morawskim
Created July 4, 2015 10:22
Show Gist options
  • Save morawskim/7f039a62f8f7682cfa2d to your computer and use it in GitHub Desktop.
Save morawskim/7f039a62f8f7682cfa2d to your computer and use it in GitHub Desktop.
Download and build PHP
#!/bin/bash
#Shell script to download and compiling PHP.
#You must manually install all dependencies!
#Author: Marcin Morawski <[email protected]>
#Exit immediately if a command exits with a non-zero status.
set -e
#Avoid accidental overwriting of a file
set -o noclobber
PHP_VERSION=$1
PHP_DOWNLOAD_MUSEUM_URL="http://museum.php.net/php5/php-$PHP_VERSION.tar.gz"
PHP_DOWNLOAD_CURRENT_URL="http://pl1.php.net/get/php-$PHP_VERSION.tar.gz/from/this/mirror"
PHP_SRC_DIRECTORY="/usr/local/src/php"
PHP_INSTALL_DIRECTORY="/opt/php/$PHP_VERSION"
PHP_INI_DIR="/opt/php/$PHP_VERSION/etc/conf.d"
#we dont use TMPDIR
TMPDIRECTORY="/tmp"
PHP_SRC_DOWNLOAD_TO="$TMPDIRECTORY/phpsource.tar.gz"
MYSQL_CONFIG_BIN=$(which mysql_config)
#APXS2_BIN=$(which apxs2)
#--with-apxs2=$APXS2_BIN \
PHP_USER='root'
PHP_GROUP='root'
PHP_OPTIONS=" --with-readline --with-gmp --enable-cli --with-openssl \
--enable-sockets --with-curl --enable-ftp --enable-pcntl --enable-memory-limit \
--enable-bcmath --with-iconv \
--with-ctype --enable-mbstring --with-mime-magic \
--enable-pdo --with-pdo-mysql --with-pdo-sqlite --with-mysqli=$MYSQL_CONFIG_BIN \
--enable-libxml --with-xsl --enable-soap --with-mcrypt --with-mhash --enable-intl \
--enable-shmop --enable-sysvsem --enable-sysvshm --enable-sysvmsg --enable-tokenizer \
--with-config-file-scan-dir=$PHP_INI_DIR
--enable-debug --enable-maintainer-zts
"
if [ $# -ne 1 ]; then
echo "Usage:" >&2
echo `basename $0` ' phpVersion' >&2
exit 1
fi
if [ $UID -ne 0 ]; then
echo 'You must run this script as root'>&2
exit 1
fi
#create dirs
mkdir -p $PHP_SRC_DIRECTORY
mkdir -p $PHP_INI_DIR
mkdir -p $PHP_INSTALL_DIRECTORY
#download php src. We use two server because old releases are stored in museum, new not
wget -O $PHP_SRC_DOWNLOAD_TO $PHP_DOWNLOAD_MUSEUM_URL || wget -O $PHP_SRC_DOWNLOAD_TO $PHP_DOWNLOAD_CURRENT_URL
sudo -u $PHP_USER tar -zxvf $PHP_SRC_DOWNLOAD_TO -C $PHP_SRC_DIRECTORY >/dev/null
#compiling and install
SOURCE_DIR="$PHP_SRC_DIRECTORY/php-$PHP_VERSION"
cd $SOURCE_DIR
sudo -u $PHP_USER ./configure --prefix=$PHP_INSTALL_DIRECTORY $PHP_OPTIONS
sudo -u $PHP_USER make
make install
chown $PHP_USER:$PHP_GROUP -R $PHP_INSTALL_DIRECTORY
rm -f $PHP_SRC_DOWNLOAD_TO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment