Skip to content

Instantly share code, notes, and snippets.

@omega8cc
Created October 20, 2015 13:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save omega8cc/235fff2ce56de6bd94cf to your computer and use it in GitHub Desktop.
Save omega8cc/235fff2ce56de6bd94cf to your computer and use it in GitHub Desktop.

Install multiple PHP instances on Linux

Install prerequisites for building PHP

Install the packages needed to build packages

apt-get install build-essential

Install PHP build dependencies

apt-get build-dep php

If you get this error:

E: Unable to find a source package for php

temporarily add these lines to your /etc/apt/sources.list file:

deb http://packages.dotdeb.org stable all
deb-src http://packages.dotdeb.org stable all

Various other development packages

  • libfcgi-dev - Header files of FastCGI
  • libfcgi0ldbl - Shared library of FastCGI
  • libjpeg62-dbg - Development files for the IJG JPEG library (version 6.2)
  • libmcrypt-dev - De-/Encryption Library development files
  • libssl-dev - De-/Encryption Library development files
  • libssl-dev SSL development libraries, header files and documentation
  • libc-client2007e - c-client library for mail protocols - library files
  • libc-client2007e-dev - c-client library for mail protocols - development files
apt-get install libfcgi-dev libfcgi0ldbl libjpeg62-dbg libmcrypt-dev libssl-dev libc-client2007e libc-client2007e-dev

Download and extract PHP

mkdir /opt/php-5.3.29
mkdir /usr/local/src/php5-build
cd /usr/local/src/php5-build
wget http://cz1.php.net/get/php-5.3.29.tar.bz2/from/this/mirror -O php-5.3.29.tar.bz2
tar jxf php-5.3.29.tar.bz2
cd php-5.3.29/

./configure

./configure \
--prefix=/opt/php-5.3.29 \
--with-pdo-pgsql \
--with-zlib-dir \
--with-freetype-dir \
--enable-mbstring \
--with-libxml-dir=/usr \
--enable-soap \
--enable-calendar \
--with-curl \
--with-mcrypt \
--with-zlib \
--with-gd \
--with-pgsql \
--disable-rpath \
--enable-inline-optimization \
--with-bz2 \
--with-zlib \
--enable-sockets \
--enable-sysvsem \
--enable-sysvshm \
--enable-pcntl \
--enable-mbregex \
--with-mhash \
--enable-zip \
--with-pcre-regex \
--with-mysql \
--with-pdo-mysql \
--with-mysqli \
--with-jpeg-dir=/usr \
--with-png-dir=/usr \
--enable-gd-native-ttf \
--with-openssl \
--with-fpm-user=www-data \
--with-fpm-group=www-data \
--with-libdir=/lib/x86_64-linux-gnu \
--enable-ftp \
--with-imap \
--with-imap-ssl \
--with-kerberos \
--with-gettext \
--enable-fpm \
--with-xpm-dir=/usr

If you used --with-imap you might have gotten this error:

configure: error: Cannot find imap library (libc-client.a). Please check your c-client installation.

You need to create this link:

ln -s /usr/lib/libc-client.a /usr/lib/x86_64-linux-gnu/libc-client.a

If you get this error:

configure: error: freetype.h not found.

You need to fix the configure file. Replace this line:

if test -f "$i/include/freetype2/freetype/freetype.h"; then

With this line:

if test -f "$i/include/freetype2/freetype.h"; then

Build

make
make test
make install

Configuration

Create configuration files:

cp /usr/local/src/php5-build/php-5.3.29/php.ini-production /opt/php-5.3.29/lib/php.ini
cp /opt/php-5.3.29/etc/php-fpm.conf.default /opt/php-5.3.29/etc/php-fpm.conf

In the file /opt/php-5.3.29/etc/php-fpm.conf set:

pid = run/php-fpm.pid
user = www-data
group = www-data
listen = 127.0.0.1:5329
include=/opt/php-5.3.29/etc/pool.d/*.conf

You can choose any port you want (9000 is the standard), but I like to have the ports recognizable for the PHP versions (5329 for v5.3.29).

Create the pool directory

mkdir /opt/php-5.3.29/etc/pool.d

Create the init script:

vi /etc/init.d/php-5.3.29-fpm
#! /bin/sh
### BEGIN INIT INFO
# Provides:          php-5.3.29-fpm
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts php-5.3.29-fpm
# Description:       starts the PHP FastCGI Process Manager daemon
### END INIT INFO
php_fpm_BIN=/opt/php-5.3.29/sbin/php-fpm
php_fpm_CONF=/opt/php-5.3.29/etc/php-fpm.conf
php_fpm_PID=/opt/php-5.3.29/var/run/php-fpm.pid
php_opts="--fpm-config $php_fpm_CONF"

wait_for_pid () {
        try=0
        while test $try -lt 35 ; do
                case "$1" in
                        'created')
                        if [ -f "$2" ] ; then
                                try=''
                                break
                        fi
                        ;;
                        'removed')
                        if [ ! -f "$2" ] ; then
                                try=''
                                break
                        fi
                        ;;
                esac
                echo -n .
                try=`expr $try + 1`
                sleep 1
        done
}
case "$1" in
        start)
                echo -n "Starting php-fpm "
                $php_fpm_BIN $php_opts
                if [ "$?" != 0 ] ; then
                        echo " failed"
                        exit 1
                fi
                wait_for_pid created $php_fpm_PID
                if [ -n "$try" ] ; then
                        echo " failed"
                        exit 1
                else
                        echo " done"
                fi
        ;;
        stop)
                echo -n "Gracefully shutting down php-fpm "
                if [ ! -r $php_fpm_PID ] ; then
                        echo "warning, no pid file found - php-fpm is not running ?"
                        exit 1
                fi
                kill -QUIT `cat $php_fpm_PID`
                wait_for_pid removed $php_fpm_PID
                if [ -n "$try" ] ; then
                        echo " failed. Use force-exit"
                        exit 1
                else
                        echo " done"
                       echo " done"
                fi
        ;;
        force-quit)
                echo -n "Terminating php-fpm "
                if [ ! -r $php_fpm_PID ] ; then
                        echo "warning, no pid file found - php-fpm is not running ?"
                        exit 1
                fi
                kill -TERM `cat $php_fpm_PID`
                wait_for_pid removed $php_fpm_PID
                if [ -n "$try" ] ; then
                        echo " failed"
                        exit 1
                else
                        echo " done"
                fi
        ;;
        restart)
                $0 stop
                $0 start
        ;;
        reload)
                echo -n "Reload service php-fpm "
                if [ ! -r $php_fpm_PID ] ; then
                        echo "warning, no pid file found - php-fpm is not running ?"
                        exit 1
                fi
                kill -USR2 `cat $php_fpm_PID`
                echo " done"
        ;;
        *)
                echo "Usage: $0 {start|stop|force-quit|restart|reload}"
                exit 1
        ;;
esac

Make executable by all:

chmod 755 /etc/init.d/php-5.3.29-fpm

Update rc:

update-rc.d php-5.3.29-fpm defaults

Start:

/etc/init.d/php-5.3.29-fpm start

You can safely ignore this warning if you have no pools in /opt/php-5.3.22/etc/pool.d :

WARNING: Nothing matches the include pattern '/opt/php-5.3.22/etc/pool.d/*.conf' from /opt/php-5.3.22/etc/php-fpm.conf at line 512.

This way, you can run multiple PHP versions on one machine at the same time, without conflict.

Enjoy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment