Skip to content

Instantly share code, notes, and snippets.

@ramsey
Last active January 25, 2016 18:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ramsey/8ba95a871c75c3aad820 to your computer and use it in GitHub Desktop.
Save ramsey/8ba95a871c75c3aad820 to your computer and use it in GitHub Desktop.
#!/bin/bash
###
# Shows build-steps used when attempting to build PHP 7 in an
# emulated MIPS chroot jail
#
# To build PHP in a MIPS chroot jail with this script:
#
# mkdir php7-qemu-mips-build && cd php7-qemu-mips-build/
# wget https://gist.githubusercontent.com/ramsey/8ba95a871c75c3aad820/raw/php7-qemu-mips-build.sh
# chmod +x php7-qemu-mips-build.sh
# vagrant init ubuntu/trusty64
# vagrant up
# vagrant ssh
# cd /vagrant
# sudo ./php7-qemu-mips-build.sh 7.0.2 mips mips wheezy
#
# That last step will take quite a while. Building anything
# in an emulated MIPS environment is slow. When it's done, the
# created chroot jail will be in:
#
# /tmp/chroot/mips-wheezy-php-7.0.2
#
# Run commands through the chroot jail:
#
# sudo chroot /tmp/chroot/mips-wheezy-php-7.0.2 php --version
#
# Core dumps may be found in /php-src/php-7.0.2 of the chroot jail.
#
if [ $EUID -ne 0 ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
### settings
php_version=${1:-7.0.2}
qemu_arch=${2:-mips}
deb_arch=${3:-mips}
suite=${4:-wheezy}
chroot_dir="/tmp/chroot/${qemu_arch}-${suite}-php-${php_version}"
apt_mirror="http://ftp.us.debian.org/debian"
php_package="https://secure.php.net/distributions/php-${php_version}.tar.bz2"
### make sure that the required tools are installed
export DEBIAN_FRONTEND=noninteractive
apt-get update
apt-get install -y wget debootstrap qemu-user-static binfmt-support
### install a minbase system with debootstrap
debootstrap --foreign --arch=$deb_arch $suite $chroot_dir $apt_mirror
cp "/usr/bin/qemu-${qemu_arch}-static" $chroot_dir/usr/bin/
chroot $chroot_dir ./debootstrap/debootstrap --second-stage
### update the list of package sources
cat <<EOF > $chroot_dir/etc/apt/sources.list
deb $apt_mirror $suite main contrib non-free
deb $apt_mirror $suite-updates main contrib non-free
deb http://security.debian.org/ $suite/updates main contrib non-free
EOF
### upgrade packages
chroot $chroot_dir apt-get update -qq
chroot $chroot_dir apt-get upgrade -qq -y
### locale configuration
chroot $chroot_dir apt-get install -qq -y debconf
chroot $chroot_dir bash -c 'echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen'
chroot $chroot_dir dpkg-reconfigure locales
### Set up the chroot for debugging segmentation fault
chroot $chroot_dir apt-get install -qq -y gdb
### install dependencies to build PHP
chroot $chroot_dir apt-get --allow-unauthenticated install -qq -y \
autoconf build-essential libcurl3-openssl-dev libgmp-dev libmcrypt-dev \
libreadline-dev libxml2-dev uuid-dev curl git
### download, build, and install the PHP version needed for this chroot
mkdir -p $chroot_dir/php-src
cd $chroot_dir/php-src
wget $php_package
tar xf "php-${php_version}.tar.bz2"
chroot $chroot_dir bash -c "cd /php-src/php-${php_version} && ./configure --disable-all --enable-bcmath --with-gmp --disable-cgi --enable-xml --enable-libxml --enable-dom --enable-filter --enable-ctype --enable-json --with-openssl --enable-phar --enable-hash --with-curl --enable-simplexml --enable-tokenizer --enable-xmlwriter --enable-zip"
chroot $chroot_dir bash -c "ulimit -c unlimited && cd /php-src/php-${php_version} && make && make install"
chroot $chroot_dir cp "/php-src/php-${php_version}/php.ini-development" /usr/local/lib/php.ini
### cleanup
chroot $chroot_dir apt-get autoclean
chroot $chroot_dir apt-get clean
chroot $chroot_dir apt-get autoremove
echo "Done!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment