Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Build PHP7 from source
#!/usr/bin/env bash
set -e
PHP_DIR=${PHP_DIR:-/etc/php7/cli}
FPM_DIR=${FPM_DIR:-/etc/php7/fpm}
PHP_VERSION=${PHP_VERSION:-7.0.0}
MODS_DIR=${MODS_DIR:-/etc/php7/mods-available}
BUILD_DIR=/usr/src/php
# Build all extensions/features by default
EXT_BZ2=${EXT_BZ2:-1}
EXT_CLI=${EXT_CLI:-1}
EXT_CURL=${EXT_CURL:-1}
EXT_DBG=${EXT_DBG:-1}
EXT_FPM=${EXT_FPM:-1}
EXT_GD=${EXT_GD:-1}
EXT_INTL=${EXT_INTL:-1}
EXT_MCRYPT=${EXT_MCRYPT:-1}
EXT_MYSQL=${EXT_MYSQL:-1}
EXT_MYSQLI=${EXT_MYSQLI:-1}
EXT_PGSQL=${EXT_PGSQL:-1}
EXT_PSPELL=${EXT_PSPELL:-1}
EXT_READLINE=${EXT_READLINE:-1}
EXT_SQLITE=${EXT_SQLITE:-1}
EXT_XSLT=${EXT_XSLT:-1}
RUNTIME_DEPS="ca-certificates curl librecode0 libsqlite3-0 libxml2"
PHPIZE_DEPS="autoconf file g++ gcc libc-dev make pkg-config re2c"
BUILD_DEPS="libpcre3-dev libcurl4-openssl-dev libreadline6-dev librecode-dev libsqlite3-dev libssl-dev libxml2-dev xz-utils bison"
CONFIGURE_ARGS="--prefix=/usr \
--sysconfdir=/etc \
--localstatedir=/var \
--datadir=/usr/share/php \
--mandir=/usr/share/man \
--disable-cgi \
--with-openssl \
--with-recode \
--with-zlib \
--with-zlib-dir=/usr \
--enable-bcmath \
--enable-calendar \
--enable-exif \
--enable-ftp \
--with-gettext \
--enable-mbstring \
--enable-pcntl \
--enable-sockets \
--enable-soap \
--enable-zip \
--with-iconv \
--with-pcre-regex=/usr \
--without-pear"
if [ "$EXT_BZ2" -eq 1 ]; then
CONFIGURE_ARGS="$CONFIGURE_ARGS --with-bz2=shared"
BUILD_DEPS="$BUILD_DEPS libbz2-dev"
fi
if [ "$EXT_CURL" -eq 1 ]; then
CONFIGURE_ARGS="$CONFIGURE_ARGS --with-curl=shared"
fi
if [ "$EXT_READLINE" -eq 1 ]; then
CONFIGURE_ARGS="$CONFIGURE_ARGS --with-readline"
fi
if [ "$EXT_GD" -eq 1 ]; then
CONFIGURE_ARGS="$CONFIGURE_ARGS \
--with-gd=shared,/usr \
--with-webp-dir=/usr \
--with-jpeg-dir=/usr \
--with-png-dir=/usr \
--with-xpm-dir=/usr/X11R6/lib \
--with-freetype-dir=/usr"
BUILD_DEPS="$BUILD_DEPS libjpeg-dev libfreetype6-dev libpng-dev libwebp-dev libxpm-dev libgd2-xpm-dev"
fi
if [ "$EXT_INTL" -eq 1 ]; then
CONFIGURE_ARGS="$CONFIGURE_ARGS --enable-intl=shared"
BUILD_DEPS="$BUILD_DEPS libicu-dev"
fi
if [ "$EXT_MYSQL" -eq 1 ]; then
CONFIGURE_ARGS="$CONFIGURE_ARGS --with-pdo-mysql=shared,mysqlnd"
fi
if [ "$EXT_MYSQLI" -eq 1 ]; then
CONFIGURE_ARGS="$CONFIGURE_ARGS --with-mysqli=shared,mysqlnd"
fi
if [ "$EXT_PGSQL" -eq 1 ]; then
CONFIGURE_ARGS="$CONFIGURE_ARGS --with-pgsql=shared,/usr --with-pdo-pgsql=shared,/usr"
BUILD_DEPS="$BUILD_DEPS libpq-dev"
fi
if [ "$EXT_SQLITE" -eq 1 ]; then
CONFIGURE_ARGS="$CONFIGURE_ARGS --with-pdo-sqlite=shared --with-sqlite3=shared"
else
CONFIGURE_ARGS="$CONFIGURE_ARGS --without-pdo-sqlite --without-sqlite3"
fi
if [ "$EXT_MYSQL" -eq 1 ] || [ "$EXT_MYSQLI" -eq 1 ]; then
CONFIGURE_ARGS="$CONFIGURE_ARGS --enable-mysqlnd"
else
CONFIGURE_ARGS="$CONFIGURE_ARGS --disable-mysqlnd"
fi
if [ "$EXT_MYSQL" -eq 1 ] || [ "$EXT_MYSQLI" -eq 1 ] || [ "$EXT_PGSQL" -eq 1 ] || [ "$EXT_SQLITE" -eq 1 ]; then
CONFIGURE_ARGS="$CONFIGURE_ARGS --enable-pdo=shared"
else
CONFIGURE_ARGS="$CONFIGURE_ARGS --disable-pdo"
fi
if [ "$EXT_XSLT" -eq 1 ]; then
CONFIGURE_ARGS="$CONFIGURE_ARGS --with-xsl=shared"
BUILD_DEPS="$BUILD_DEPS libxslt1-dev"
fi
if [ "$EXT_PSPELL" -eq 1 ]; then
CONFIGURE_ARGS="$CONFIGURE_ARGS --with-pspell=shared"
BUILD_DEPS="$BUILD_DEPS libpspell-dev"
fi
if [ "$EXT_DBG" -eq 1 ]; then
CONFIGURE_ARGS="$CONFIGURE_ARGS --enable-phpdbg"
fi
# Install all dependencies
apt-get update
apt-get install -y --no-install-recommends ${RUNTIME_DEPS} ${PHPIZE_DEPS} ${BUILD_DEPS}
# Fix location of libpcre
ln -s /usr/lib/x86_64-linux-gnu/libpcre.a /usr/lib/libpcre.a
# Download php
cd /tmp
curl -SL "http://php.net/get/php-$PHP_VERSION.tar.xz/from/this/mirror" -o php.tar.xz
curl -SL "http://php.net/get/php-$PHP_VERSION.tar.xz.asc/from/this/mirror" -o php.tar.xz.asc
gpg --keyserver ha.pool.sks-keyservers.net --recv-keys 1A4E8B7277C42E53DBA9C7B9BCAA30EA9C0D5763
gpg --verify php.tar.xz.asc
rm -rf ${BUILD_DIR} &> /dev/null || true
mkdir -p ${BUILD_DIR}
tar -xof php.tar.xz -C ${BUILD_DIR} --strip-components=1
rm php.tar.xz*
cd ${BUILD_DIR}
# Build and install php-cli
if [ "$EXT_CLI" -eq 1 ]; then
./configure ${CONFIGURE_ARGS} \
--with-config-file-path=${PHP_DIR} \
--with-config-file-scan-dir=${PHP_DIR}/conf.d
make -j"$(nproc)"
make install
mkdir -p ${PHP_DIR}/conf.d
cp php.ini-production ${PHP_DIR}/php.ini
fi
# Build and install php-fpm
if [ "$EXT_FPM" -eq 1 ]; then
# We have to do a full new build here because the fpm build will use
# different configuration directories and therefore must be build again.
make clean
rm configure
./buildconf --force
./configure ${CONFIGURE_ARGS} \
--disable-cli \
--enable-fpm \
--with-config-file-path=${FPM_DIR} \
--with-config-file-scan-dir=${FPM_DIR}/conf.d
make -j"$(nproc)"
make install
rm -rf /etc/php-fpm.d /etc/php-fpm.conf.default &> /dev/null || true
mkdir -p ${FPM_DIR}/conf.d
cp php.ini-production ${FPM_DIR}/php.ini
fi
# Generate
mkdir -p ${MODS_DIR}
for FILE in $(php-config --extension-dir)/*.so; do
BASENAME=$(basename ${FILE})
echo "extension=${BASENAME}" > ${MODS_DIR}/${BASENAME::-2}ini;
if [ "$EXT_CLI" -eq 1 ]; then
ln -s ${MODS_DIR}/${BASENAME::-2}ini ${PHP_DIR}/conf.d/
fi
if [ "$EXT_FPM" -eq 1 ]; then
ln -s ${MODS_DIR}/${BASENAME::-2}ini ${FPM_DIR}/conf.d/
fi
done;
# Optimize
find /usr/bin /usr/sbin -type f -executable -exec strip --strip-all '{}' + &> /dev/null || true
# Cleanup everything
cd /
rm -rf ${BUILD_DIR}
apt-get clean
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment