How To Build PHP 5.6 (PHP-FPM & FastCGI) with Zend OPcache And APCu For ISPConfig 3 On Debian 7 (Wheezy)

Version 1.0
Authors: Till Brehm <t [dot] brehm [at] ispconfig [dot] org>, Falko Timme
Follow howtoforge on Twitter

ISPConfig 3 has a builtin feature to support multiple PHP versions on one server and select the optimal PHP version for a website. This feature works with PHP-FPM and FastCGI. This tutorial shows how to build PHP 5.6 as a PHP-FPM and a FastCGI version on a Debian Wheezy server. These PHP 5.5 builds include Zend OPcache, and APCu.

 

1 Preliminary Note

I will install PHP 5.6.36, the latest PHP 5 version at the time of this writing. I will also show how to build some additional PHP extensions such as APCu and memcache. The ioncube loader is not available for php 5.6 at this time, so I will leave out that step.

Please note that PHP-FPM can be used on both Apache and nginx servers, while FastCGI is available only for Apache servers.

PHP-FPM and FastCGI are mutually exclusive in PHP 5.6, that's why I show two ways of building PHP, one for PHP-FPM, one for FastCGI, however you can compile PHP twice to get both, one time with --enable-fpm and one time with --enable-cgi (make sure to use different base directories, such as /opt/php-5.6.36 for PHP-FPM and /opt/phpfcgi-5.6.36 for FastCGI).

 

2 Building PHP 5.6.36 (PHP-FPM)

Download and extract PHP 5.6.36:

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

Install the prerequisites for building PHP5:

apt-get install build-essential
apt-get build-dep php5
apt-get install libfcgi-dev libfcgi0ldbl libjpeg62-dbg libmcrypt-dev libssl-dev libc-client2007e libc-client2007e-dev
ln -s /usr/lib/libc-client.a /usr/lib/x86_64-linux-gnu/libc-client.a

(The last command is needed if you build PHP with --with-imap, because otherwise ./configure will stop with the following error:

checking for crypt in -lcrypt... yes
configure: error: Cannot find imap library (libc-client.a). Please check your c-client installation.
[email protected]:/usr/local/src/php5-build/php-5.6.36#

)

Configure and build PHP 5.6.36 as follows (you can adjust the ./configure command to your needs, take a look at

./configure --help

to see all available options; if you use a different ./configure command, it is possible that additional libraries are required, or the build process will fail):

./configure --prefix=/opt/php-5.6.36 --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 --enable-exif --enable-bcmath --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 --with-xmlrpc --with-xsl --enable-opcache --enable-fpm

The last switch (--enable-fpm) makes sure this PHP version will work with PHP-FPM.

make
make install

Copy php.ini and php-fpm.conf to the correct locations:

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

Open /opt/php-5.6.36/etc/php-fpm.conf and adjust the following settings - in the listen line you must use an unused port (e.g. 8999; port 9000 might be in use by Debian's default PHP-FPM already), and you must add the line include=/opt/php-5.6.36/etc/pool.d/*.conf at the end:

vi /opt/php-5.6.36/etc/php-fpm.conf
[...]
pid = run/php-fpm.pid
[...]
user = www-data
group = www-data
[...]
listen = 127.0.0.1:8999
[...]
include=/opt/php-5.6.36/etc/pool.d/*.conf

Create the pool directory for PHP-FPM:

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

Next create an init script for PHP-FPM:

vi /etc/init.d/php-5.6.36-fpm
#! /bin/sh
### BEGIN INIT INFO
# Provides:          php-5.6.36-fpm
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts php-5.6.36-fpm
# Description:       starts the PHP FastCGI Process Manager daemon
### END INIT INFO
php_fpm_BIN=/opt/php-5.6.36/sbin/php-fpm
php_fpm_CONF=/opt/php-5.6.36/etc/php-fpm.conf
php_fpm_PID=/opt/php-5.6.36/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 the init script executable and create the system startup links:

chmod 755 /etc/init.d/php-5.6.36-fpm
insserv php-5.6.36-fpm

Finally start PHP-FPM:

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

As long as there are no pools in /opt/php-5.6.36/etc/pool.d, you will get this warning which you can ignore:

[email protected]:/usr/local/src/php5-build/php-5.6.36# /etc/init.d/php-5.6.36-fpm start
Starting php-fpm [29-Aug-2014 13:21:12] WARNING: Nothing matches the include pattern '/opt/php-5.6.36/etc/pool.d/*.conf' from /opt/php-5.6.36/etc/php-fpm.conf at line 528.
done
[email protected]:/usr/local/src/php5-build/php-5.6.36#

To enable the Zend OPcache, open /opt/php-5.6.36/lib/php.ini...

vi /opt/php-5.6.36/lib/php.ini

... and add the following line at the end:

[...]
zend_extension=opcache.so

That's it - if you like, you can now install some additional modules like APCu, memcache, memcached, and ioncube.

The APCu, memcache, and memcached modules can be installed through PEAR which we must install and initialize first:

apt-get -y install php-pear
cd /opt/php-5.6.36/etc
pecl -C ./pear.conf update-channels

APCu can now be installed as follows:

pecl -C ./pear.conf install channel://pecl.php.net/apcu-4.0.6

Accept all default values. Afterwards, open /opt/php-5.6.36/lib/php.ini...

vi /opt/php-5.6.36/lib/php.ini

... and add the line extension=apcu.so at the end of the file (you can also configure some additional APCu settings):

[...]
extension=apcu.so
apc.enabled=1
apc.shm_size=128M
apc.ttl=0
apc.gc_ttl=600
apc.enable_cli=1
apc.mmap_file_mask=/tmp/apc.XXXXXX
;apc.mmap_file_mask=/dev/zero
;apc.shm_segments = 5

The memcache extension can be installed as follows:

pecl -C ./pear.conf install memcache

Open /opt/php-5.6.36/lib/php.ini...

vi /opt/php-5.6.36/lib/php.ini

... and add the line extension=memcache.so at the end of the file:

[...]
extension=memcache.so

Reload PHP-FPM afterwards:

/etc/init.d/php-5.6.36-fpm reload

In ISPConfig 3.0.5, you can configure the new PHP version under System > Additional PHP Versions. On the Name tab, you just fill in a name for the PHP version (e.g. PHP 5.6.36) - this PHP version will be listed under this name in the website settings in ISPConfig:

Go to the PHP-FPM Settings tab (the FastCGI Settings tab can be left empty) and fill out the fields as follows:

Share this page:

Suggested articles

14 Comment(s)

Add comment

Comments

By: Anonymous


 before apt-get build-dep php5 need add 

nano /etc/apt/sources.list 

 deb-src http://ftp.debian.org/debian ... main contrib non-free

And use checkinstall insted make install

By: John Lanz

Hi,

When I execute ./configure --prefix=/opt/php-5.6.0 --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 --enable-exif --enable-bcmath --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 --with-xmlrpc --with-xsl --enable-opcache --enable-fpm

I got an error: configure: error: xml2-config not found. Please check your libxml2 installation.

and on make: make: *** No targets specified and no makefile found.  Stop.

What should I do?

Please help.

Thanks,

John

By: seb

Hello, What is the procedure to follow to upgrade to version 5.6.7 php. Best Regards.

 

Bonjour,

Quel est la procédure a suivre pour upgrade en version php 5.6.7.

Cordialement.

By: Etienne

Hi.

After having done all this stuff.

What should I change into NginX config tu use the new php version ?

Thank's

By: till

You change nothing in nginx conf. Login to ispconfig, go to the website settings and select the new php version in the php version select field, then press the save button.

By: Mark

Hi, why is there (page no 2) some duplicate content from page 1?

By: till

On page 1 you compile a php-fpm version, on page 2 a php-fcgi version of PHP.

By: prochor666

Hi, I tried this How to for PHP 5.6.12

Everything went ok until start fpm:

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

Starting php-fpm [02-Sep-2015 18:16:07] ERROR: [/opt/php-5.6.12/etc/php-fpm.conf:540] value is NULL for a ZEND_INI_PARSER_ENTRY

[02-Sep-2015 18:16:07] ERROR: failed to load configuration file '/opt/php-5.6.12/etc/php-fpm.conf'

[02-Sep-2015 18:16:07] ERROR: FPM initialization failed

failed

Problem is when this line is inserted:

include /opt/php-5.6.12/etc/pool.d/*.conf

If I comment this line, fpm starts, but PHP version in ISPconfig doesn't work.

 

I found this:

http://serverfault.com/questions/547394/is-there-a-limit-setting-a-php-admin-value-in-php-fpm

and used this fix:

https://github.com/DaveRandom/php-src/commit/9ad8e89d4f080626a92fc8817ab156c09b6b266a

and compiled with it. Same result.

 

Any clue? Please help.

By: FS

I found the same error but it was some lines i add manually in "php directives" tab in ISP Config. I remove all manual directive and it works well after (then i put back my config line by line and find where was my error).

By: spartannitro

Hi, I've followed this tutorial and got PHP 5.6.16 working but I have an issue, afther choosing this version the timezone of the web using it, has 5 hours more than my actual timezone, so how can I change this and make it fit with my actual TimeZone?

By: borekon

Not working for me. I select the php version in the domain tab and click save. Then, open a phpinfo page that shows i'm using the default version of PHP instead the installed one.

By: qwwq

I have the same problem but with 7.0.5 and can't find helpp ;/

By: brody

could you configure this for centos 7?

By: Jack Mehoff

Thank you. This saved my life.