Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save twicejr/a7637ed2d3de74a61dc8 to your computer and use it in GitHub Desktop.
Save twicejr/a7637ed2d3de74a61dc8 to your computer and use it in GitHub Desktop.
Linux: Build Thread Safe PHP with PHP5-FPM
#!/usr/bin/env bash
# PARAMETERS
# ----------
# PHP
# ---
PHP_TIMEZONE="UTC"
PHP_DIRECTORY="/etc/php5ts"
PHP_VERSION="5.6.5"
# Update the box release repositories
# -----------------------------------
apt-get update
# Essential Packages
# ------------------
apt-get install -y build-essential git-core vim curl
# PHP with Thread Safe build
# --------------------------
git clone https://github.com/php/php-src.git /tmp/php-src --branch php-$PHP_VERSION --depth 1
cd /tmp/php-src
apt-get install -y make autoconf re2c
# Bison 3 isn't supported at this moment; you may have to choose appropriate channe/binary for your machine (perhaps 2.7)
# I couldn't any decent channel for 32bit machinel; had to use LinuxBrew
# TODO: Link bison installer script, if it works
# apt-get install bison
# If you add some configuration command, add the dependancies here
apt-get install -y libicu-dev libmcrypt-dev libssl-dev libcurl4-openssl-dev libbz2-dev libxml2-dev libpng-dev libjpeg-dev libedit-dev libreadline-dev
./buildconf --force
#./configure --prefix=$PHP_DIRECTORY --with-config-file-path=$PHP_DIRECTORY --with-config-file-scan-dir=$PHP_DIRECTORY/conf.d --disable-all --enable-maintainer-zts --with-curl --with-openssl --with-gd --enable-gd-native-ttf --enable-intl --enable-mbstring --with-mcrypt --with-mysqli=mysqlnd --with-zlib --with-bz2 --enable-exif --with-pdo-mysql=mysqlnd --with-libedit --enable-zip --enable-pdo --enable-pcntl --enable-sockets --enable-mbregex --with-tsrm-pthreads --enable-fpm --with-fpm-group=www-data --with-fpm-user=www-data --enable-opcache --enable-cgi
./configure --prefix=$PHP_DIRECTORY --with-config-file-path=$PHP_DIRECTORY --with-config-file-scan-dir=$PHP_DIRECTORY/conf.d --enable-maintainer-zts --with-curl --with-openssl --with-gd --enable-gd-native-ttf --enable-intl --enable-mbstring --with-mcrypt --with-mysqli=mysqlnd --with-zlib --with-bz2 --enable-exif --with-pdo-mysql=mysqlnd --with-libedit --enable-zip --enable-pdo --enable-pcntl --enable-sockets --enable-mbregex --with-tsrm-pthreads --enable-fpm --with-fpm-group=www-data --with-fpm-user=www-data --enable-opcache --enable-cgi
make
make install
cp php.ini-production $PHP_DIRECTORY/php.ini
# TODO: change upload_max_filesize and post_max_size to 1024M
# Link PHP, PHP-FPM and other binaries
mv /usr/bin/php{,php.bkp}
mv /usr/bin/phpize{,phpize.bkp}
mv /usr/bin/php-config{,php-config.bkp}
mv /usr/bin/pear{,pear.bkp}
mv /usr/bin/pecl{,pecl.bkp}
mv /usr/bin/php5-fpm{,php5-fpm.bkp}
ln -s $PHP_DIRECTORY/bin/php /usr/bin/php
ln -s $PHP_DIRECTORY/bin/phpize /usr/bin/phpize
ln -s $PHP_DIRECTORY/bin/php-config /usr/bin/
ln -s $PHP_DIRECTORY/bin/pear /usr/bin/pear
ln -s $PHP_DIRECTORY/bin/pecl /usr/bin/pecl
ln -s $PHP_DIRECTORY/sbin/php-fpm /usr/bin/php-fpm
mv $PHP_DIRECTORY/etc/php-fpm.conf{.default,}
ln -s $PHP_DIRECTORY/etc/php-fpm.conf /etc/php-fpm.conf
# pthreads Extension build
# ------------------------
#git clone https://github.com/krakjoe/pthreads.git /tmp/pthreads
#cd /tmp/pthreads
#apt-get install -y php5-dev
#phpize
#./configure --with-php-config=$PHP_DIRECTORY/bin/php-config
#make
#make install
$PHP_DIRECTORY/bin/pecl install pthreads
mkdir -p $PHP_DIRECTORY/conf.d
echo "extension=pthreads.so" > $PHP_DIRECTORY/conf.d/pthreads.ini
# mongo Extension build
# ------------------------
$PHP_DIRECTORY/bin/pecl install mongo
mkdir -p $PHP_DIRECTORY/conf.d
echo "extension=mongo.so" > $PHP_DIRECTORY/conf.d/mongo.ini
# xdebug Extension build
# ------------------------
$PHP_DIRECTORY/bin/pecl install xdebug
mkdir -p $PHP_DIRECTORY/conf.d
echo "extension=xdebug.so" > $PHP_DIRECTORY/conf.d/xdebug.ini
# Setting PHP configurations
# --------------------------
# Date timezone
sed 's#;date.timezone\([[:space:]]*\)=\([[:space:]]*\)*#date.timezone\1=\2\"'"$PHP_TIMEZONE"'\"#g' $PHP_DIRECTORY/php.ini > $PHP_DIRECTORY/php.ini.tmp
mv $PHP_DIRECTORY/php.ini.tmp $PHP_DIRECTORY/php.ini
# Error messages
sed 's#display_errors = Off#display_errors = On#g' $PHP_DIRECTORY/php.ini > $PHP_DIRECTORY/php.ini.tmp
mv $PHP_DIRECTORY/php.ini.tmp $PHP_DIRECTORY/php.ini
sed 's#display_startup_errors = Off#display_startup_errors = On#g' $PHP_DIRECTORY/php.ini > $PHP_DIRECTORY/php.ini.tmp
mv $PHP_DIRECTORY/php.ini.tmp $PHP_DIRECTORY/php.ini
sed 's#error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT#error_reporting = E_ALL#g' $PHP_DIRECTORY/php.ini > $PHP_DIRECTORY/php.ini.tmp
mv $PHP_DIRECTORY/php.ini.tmp $PHP_DIRECTORY/php.ini
# git pull fpm init script, move to init scripts
# ---------------------------------------------------------
# IMPORTANT: the listen.* may have to be uncommented, and TCP socket may have to be converted to Unix socket
git clone https://gist.github.com/873677a6d3b509ad7b07.git /tmp/fpm-init
cd /tmp/fpm-init
mv php5-fpm.sh /etc/init.d/php5-fpm
chmod +x /etc/init.d/php5-fpm
/etc/init.d/php5-fpm start
# IMPORTANT: Following may have to be added to /etc/apache2/conf-available/php5-fpm.conf
# ...and then run "a2enconf php5-fpm"
# <IfModule mod_fastcgi.c>
# AddHandler php5-fcgi .php
# Action php5-fcgi /php5-fcgi
# Alias /php5-fcgi /usr/lib/cgi-bin/php5-fcgi
# FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi -socket /var/run/php5-fpm.sock -pass-header Authorization
#</IfModule>
#<Directory /usr/lib/cgi-bin>
# Require all granted
#</Directory>
# Apache changes
# ---------------------------------------------------------
# Note: worker, for php-fpm; event segfaults with spdy.
apt-get install libapache2-mod-fastcgi
a2dismod mpm_prefork
a2enmod actions fastcgi alias mpm_worker
apache2ctl configtest
/etc/init.d/apache2 restart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment