How to install PHP 7.1.16 as PHP-FPM & FastCGI for ISPConfig 3.1 on Debian 8 (Jessie)

Channel: Linux
Abstract: nano /etc/init.d/php-7.1-fpm #cp /usr/local/src/php7-build/php-7.1.16/php.ini-production /opt/php-7.1/lib/php.ini cp /opt/php-7.1/etc/php-fpm.conf.def
How to install PHP 7.1.16 as PHP-FPM & FastCGI for ISPConfig 3.1 on Debian 8 (Jessie)

The final version of PHP 7.1 is available for download on Github and the PHP mirrors now. PHP 7.1 is the next generation of the PHP programming language, it is up to 2 times faster than PHP 5.6 and 14 times faster than PHP 5.0 according to the release notes. The new PHP version is not 100% compatible with PHP 5.x as some deprecated API's have been removed, so it is a good idea to start testing your web sites for compatibility with this new release. This can be done easily and without affecting all sites on your server by using the multi PHP version feature in ISPConfig 3. The PHP version can be selected in the ISPConfig 3 website settings for each site individually. This feature works with PHP-FPM and FastCGI. This tutorial shows how to build the new PHP 7.1 as a PHP-FPM and a FastCGI version on a Debian Jessie server. This PHP 7.1 build includes many compiled in extensions and Zend OPcache and Memcached as loadable modules.

 

1 Preliminary Note

I will install PHP 7.1 which has been released as final version a few days ago. Please note that PHP-FPM can be used on both Apache and Nginx servers while FastCGI is available only for Apache servers.

With older PHP versions, PHP-FPM and FastCGI had been mutually exclusive so that an FPM and FastCGI binary had to be build separately. With PHP 7.1, a single binary that supports FPM and FCGI mode can be build.

 

2 Compile PHP 7.1 with PHP-FPM and Fastcgi

Download and extract PHP 7.1 from Github:

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

Install the prerequisites for building PHP 7.1 and the nano editor that I will use to edit the config files:

apt-get install build-essential nano autoconf
apt-get install libfcgi-dev libfcgi0ldbl libjpeg62-turbo-dbg libmcrypt-dev libssl-dev libc-client2007e libc-client2007e-dev libxml2-dev libbz2-dev libcurl4-openssl-dev libjpeg-dev libpng12-dev libfreetype6-dev libkrb5-dev libpq-dev libxml2-dev libxslt1-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-7.0.7#

)

Configure and build PHP 7.1 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-7.1 --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-pdo-mysql --with-mysqli --with-mysql-sock=/var/run/mysqld/mysqld.sock --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/php7-build/php-7.1.16/php.ini-production /opt/php-7.1/lib/php.ini
cp /opt/php-7.1/etc/php-fpm.conf.default /opt/php-7.1/etc/php-fpm.conf
cp /opt/php-7.1/etc/php-fpm.d/www.conf.default /opt/php-7.1/etc/php-fpm.d/www.conf

Open /opt/php-7.1/etc/php-fpm.conf and adjust the following setting (remove the ; in front of the pid line):

nano /opt/php-7.1/etc/php-fpm.conf
[...]
pid = run/php-fpm.pid
[...]

Then open /opt/php-7.1/etc/php-fpm.d/www.conf and adjust 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):

nano /opt/php-7.1/etc/php-fpm.d/www.conf
[...]
listen = 127.0.0.1:8999
[...]

When you use multiple additional PHP versions, then ensure to use a free port, e.g. 8998, 8997 and so on for the next versions.

3 Create the init script and systemd unit file

Debian supports Systemd as well as the traditional init scripts. Most systems today use Systemd, in this case proceed with chapter 3.2, for systems with traditional init system, follow chapter 3.1 and then proceed with chapter 4. Do not install both, the systemd and init.d script, on the same system.

3.1 Create a init.d start script

First I will create an init script for the php-fpm service and then I will create a systemd unit.

First, create an init script for PHP-FPM:

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

Finally start PHP-FPM:

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

The result should be:

/etc/init.d/php-7.1-fpm start
Starting php-fpm done

3.2 Create  Systemd script for PHP 7.1

Now create the systemd unit file:

nano /lib/systemd/system/php-7.1-fpm.service

with the following content:

[Unit]
Description=The PHP 7.1 FastCGI Process Manager
After=network.target

[Service]
Type=simple
PIDFile=/opt/php-7.1/var/run/php-fpm.pid
ExecStart=/opt/php-7.1/sbin/php-fpm --nodaemonize --fpm-config /opt/php-7.1/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID

[Install]
WantedBy=multi-user.target

Enable the service and reload systemd:

systemctl enable php-7.1-fpm.service
systemctl daemon-reload

Finally start PHP-FPM:

systemctl start php-7.1-fpm.service

4 Enable Zend OPcache

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

nano /opt/php-7.1/lib/php.ini

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

[...]
zend_extension=opcache.so

The Memcache and APCu extension can not be installed on PHP 7.1 yet, so I will skip their installation for now. I will update the tutorial later when the pecl extensions are compatible with PHP 7.1.

Test the PHP version:

cd /opt/php-7.1/bin
./php --version

The output should be similar to this screenshot.

Please note: The screenshot is from PHP 7.1.16, the tutorial gets updated continuously for new PHP 7.1 versions, but we don't take new screenshots each time, so the PHP 7.1 version that you will see on your server is might be newer. The current version of this tutorial is for php-7.1.16.

5 Enable Memcache (optional)

In this chapter, I will compile and enable the PHP Memcached extension.

The first step is to install the libmemcached-dev package from Debian.

apt-get install libmemcached-dev

Then create a diretory, download the PHP memcache extension from Github, unpack the archive and enter the directory that contains the unpacked files.

mkdir /usr/local/src/php7-build/php-memcache
cd /usr/local/src/php7-build/php-memcache
wget https://github.com/php-memcached-dev/php-memcached/archive/php7.zip
unzip php7.zip
cd php-memcached-php7

Prepare he sources by running the phpize command from PHP 7.1.

/opt/php-7.1/bin/phpize

Configure and build the PHP memcache extension.

./configure --with-php-config=/opt/php-7.1/bin/php-config
make
make install

To enable the Memcache extension, open /opt/php-7.1/lib/php.ini...

nano /opt/php-7.1/lib/php.ini

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

[...]
extension=memcached.so

2.1 Install xDebug extension (optional)

The xDebug module is a debugging extension for PHP. The installation is optional.

Install xDebug with these commands.

cd /opt/php-7.1/etc
pecl -C ./pear.conf update-channels
pecl -C ./pear.conf install xdebug

Then edit the php.ini file with an editor:

nano /opt/php-7.1/lib/php.ini

and add the following line at the end of the file:

zend_extension=/opt/php-7.1/lib/php/extensions/no-debug-non-zts-20160303/xdebug.so

Finally restart the php-fpm daemon:

service php-7.1-fpm restart

7 Enable PHP 7.1 in ISPConfig

In ISPConfig 3, 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 7.1) - this PHP version will be listed under this name in the website settings in ISPConfig:

Go to the FastCGI Settings tab and fill out the fields as follows:

Path to the PHP FastCGI binary: /opt/php-7.1/bin/php-cgi
Path to the php.ini directory: /opt/php-7.1/lib

Then g to the PHP-FPM Settings tab and fill out the fields as follows:

Path to the PHP-FPM init script: /etc/init.d/php-7.1-fpm
Path to the php.ini directory: /opt/php-7.1/lib
Path to the PHP-FPM pool directory: /opt/php-7.1/etc/php-fpm.d

7 Links
  • PHP: http://www.php.net/
  • ISPConfig: http://www.ispconfig.org/
  • Debian: http://www.debian.org/

Ref From: howtoforge

Related articles