Introduction

PHP 7 - speedDebian's current stable package repository may not always contain an up to date software to be in line with our expectations. There is a very good reason for this, such as a stable system as a trade-off for running bleeding edge software.

PHP server-side scripting language is not an exemption! At the time of writing the current Debian's PHP version is 5.6.29 whereas the latest PHP source release is 7.1.0( given that there is no PHP 6 at all ).

The current PHP developer's claims put PHP 7 in front of PHP 5.6 in terms of speed, where PHP 7 is supposed to be as twice as fast as its predecessor:

Thanks to the new Zend Engine 3.0, your apps see up to 2x faster performance and 50% better memory consumption than PHP 5.6, allowing you to serve more concurrent users without adding any hardware. Designed and refactored for today’s workloads, PHP 7 is the ultimate choice for web developers today.
Reference: zend.com/en/resources/php-7

Objective

To compare a speed between PHP 5.6 and latest PHP 7.1.0 is not an objective of this article. However, the objective is the get that latest PHP 7 release installed on Debian 8 system. Additionally we will install PHP-FPM (FastCGI Process Manager). This guide will offer two possible installations.

First, the guide will use compile-php-debian script available at github.com/linuxconfig/compile-php-debian to automate the PHP 7 installation and later it will show, how to install PHP 7 manually.

Requirements

  • Privileged access to your Debian Linux System as root or via sudo command is required

Difficulty

MEDIUM

Conventions

  • # - requires given command to be executed with root privileges either directly as a root user or by use of sudo command
  • $ - given command to be executed as a regular non-privileged user

Instructions

Remove PHP5 Packages

If you already have PHP5 installed on your system, you need to remove them prior to new PHP 7 installation. To do so for list all php related package on your system:
# dpkg -l | grep -E "^ii.*php" | cut -d " " -f3
libapache2-mod-php5
php5
php5-cli
php5-common
php5-json
php5-readline
The following command will remove all packages listed by the above command output:
# REMOVE=$(dpkg -l | grep -E "^ii.*php" | cut -d " " -f3)
# dpkg -P $REMOVE

Scripted Automatic Installation

The following scripted automatic installation performs all necessary steps to compile and install user selected PHP 7 version as well as it will also install PHP-FPM daemon. Start by cloning compile-php-debian script:
$ git clone https://github.com/linuxconfig/compile-php-debian.git
Next, navigate to compile-php-debian directory:
$ cd compile-php-debian
Now simply decide what PHP 7 version you wish to install and enter your selected version number as an argument to install_php.sh script. For example to install PHP version 7.0.8 enter:
# ./install_php.sh 7.0.8
FOR PHP VERSION 7.1.0 RUN
# ./install_php.sh 7.1.0 
The installation may take some time. Once finished start PHP-FPM Daemon by executing service command:
# service php7-fpm start
# service php7-fpm status
[ ok ] php-7.1.0-fpm is running.
The PHP-FPM daemon is now listening on a localhost's port number 9000. You may also attempt to connect to PHP-FPM daemon directly using cgi-fcgi command found within libfcgi0ldbl package:
# cgi-fcgi -bind -connect 127.0.0.1:9000
X-Powered-By: PHP/7.1.0
Content-type: text/html; charset=UTF-8
This completes your PHP 7 installation on your Debain 8 Linux system.

Manual Installation

Prerequisites Installation

Let's start by the installation of all prerequisites to fit the most common PHP compilation options:
# apt-get install autoconf bison build-essential git-core libbz2-dev libcurl4-openssl-dev libfreetype6-dev libicu-dev libjpeg-dev libmcrypt-dev libpng-dev libpspell-dev libreadline-dev libssl-dev 
libxml2-dev pkg-config

PHP Source Code Download

Probably the easiest way to get the PHP source code it to clone PHP's repository on GitHub. Execute the following command to clone PHP source directory:
$ git clone https://github.com/php/php-src.git
Once the git clone operation is finished you will be left with a new directory name php-src.

Another alternative is to get a tarball of a specific PHP version you wish to install by downloading it directly from http://php.net/downloads.php. The advantage in this case is a minimal download size requirement. However, you will not be able to choose selectively different versions for multiple deployments.

PHP Source Code Compilation

At this stage we are ready to perform a PHP source code compilation. Navigate to a previously downloaded php-src directory:
$ cd php-src
Next, decide on what PHP version you wish to compile and install. Run the following git command to list all available PHP 7 versions:
$ git branch -a | grep PHP-7
  remotes/origin/PHP-7.0
  remotes/origin/PHP-7.0.0
  remotes/origin/PHP-7.0.1
  remotes/origin/PHP-7.0.10
  remotes/origin/PHP-7.0.11
  remotes/origin/PHP-7.0.12
  remotes/origin/PHP-7.0.13
  remotes/origin/PHP-7.0.14
  remotes/origin/PHP-7.0.2
  remotes/origin/PHP-7.0.3
  remotes/origin/PHP-7.0.4
  remotes/origin/PHP-7.0.5
  remotes/origin/PHP-7.0.6
  remotes/origin/PHP-7.0.7
  remotes/origin/PHP-7.0.8
  remotes/origin/PHP-7.0.9
  remotes/origin/PHP-7.1
  remotes/origin/PHP-7.1.0
  remotes/origin/PHP-7.1.0RC1
  remotes/origin/PHP-7.1.0RC2
  remotes/origin/PHP-7.1.0RC3
  remotes/origin/PHP-7.1.0beta1
  remotes/origin/PHP-7.1.0beta2
  remotes/origin/PHP-7.1.0beta3
Use git checkout command to switch to your desired PHP version branch which you wish to be used for the compilation. Example:
$ git checkout PHP-7.1.0
Branch PHP-7.1.0 set up to track remote branch PHP-7.1.0 from origin.
Switched to a new branch 'PHP-7.1.0'
Next, create a directory to be used as a installation target. In our case it will be /usr/local/php-7.1.0:
# mkdir /usr/local/php-7.1.0
and specify all compilation strings. Take a special note of the --prefix and --with-config-file-scan-dir configuration strings which define installation and configuration settings.The following is a list of most common PHP compilation options.

Feel, free to update the list to fit your needs. Depending on your selection, you may be required to install additional prerequisites. Copy and paste below text into your terminal followed by ENTER:
CONFIGURE_STRINGS="--enable-bcmath \
                   --enable-calendar \
                   --enable-dba \
                   --enable-exif \
                   --enable-filter \
                   --enable-fpm \
                   --enable-ftp \
                   --enable-gd-native-ttf \
                   --enable-intl \
                   --enable-mbstring \
                   --enable-mysqlnd \
                   --enable-pcntl \
                   --enable-shmop \
                   --enable-simplexml \
                   --enable-soap \
                   --enable-sockets \
                   --enable-sysvmsg \
                   --enable-sysvsem \
                   --enable-sysvshm \
                   --enable-wddx \
                   --enable-xmlreader \
                   --enable-xmlwriter \
                   --enable-zip \
                   --prefix=/usr/local/php-7.1.0 \
                   --with-bz2 \
                   --with-config-file-scan-dir=/usr/local/php-7.1.0/etc/conf.d \
                   --with-curl \
                   --with-fpm-group=www-data \
                   --with-fpm-user=www-data \
                   --with-freetype-dir \
                   --with-gd \
                   --with-gettext \
                   --with-jpeg-dir \
                   --with-mcrypt \
                   --with-mhash \
                   --with-mysqli=mysqlnd \
                   --with-mysql-sock=/var/run/mysqld/mysqld.sock \
                   --with-openssl \
                   --without-pear \
                   --with-pdo-mysql=mysqlnd \
                   --with-pdo-sqlite \
                   --with-png-dir \
                   --with-pspell \
                   --with-readline \
                   --with-sqlite3 \
                   --with-zlib"
The above will set CONFIGURE_STRINGS shell variable to contain all your PHP compilation options. Next, we need to build our configure and Makefiles. To do so execute:
$ ./buildconf --force
Forcing buildconf
Removing configure caches
rebuilding aclocal.m4
rebuilding configure
rebuilding main/php_config.h.in
Next, perform compilation configuration:
$ ./configure $CONFIGURE_STRINGS
If no errors were produced by the above configure command, then its time to perform compilation by running a make command:
$ make
....
Build complete.
Don't forget to run 'make test'.
Once the compilation if the PHP source code is finished, optionally execute make test command or run make install command to install all previously compiled binaries into their relevant directories:
# make install
Installing shared extensions:     /usr/local/php-7.1.0/lib/php/extensions/no-debug-non-zts-20160303/
Installing PHP CLI binary:        /usr/local/php-7.1.0/bin/
Installing PHP CLI man page:      /usr/local/php-7.1.0/php/man/man1/
Installing PHP FPM binary:        /usr/local/php-7.1.0/sbin/
Installing PHP FPM config:        /usr/local/php-7.1.0/etc/
Installing PHP FPM man page:      /usr/local/php-7.1.0/php/man/man8/
Installing PHP FPM status page:   /usr/local/php-7.1.0/php/php/fpm/
Installing phpdbg binary:         /usr/local/php-7.1.0/bin/
Installing phpdbg man page:       /usr/local/php-7.1.0/php/man/man1/
Installing PHP CGI binary:        /usr/local/php-7.1.0/bin/
Installing PHP CGI man page:      /usr/local/php-7.1.0/php/man/man1/
Installing build environment:     /usr/local/php-7.1.0/lib/php/build/
Installing header files:           /usr/local/php-7.1.0/include/php/
Installing helper programs:       /usr/local/php-7.1.0/bin/
  program: phpize
  program: php-config
Installing man pages:             /usr/local/php-7.1.0/php/man/man1/
  page: phpize.1
  page: php-config.1
/php-src/build/shtool install -c ext/phar/phar.phar /usr/local/php-7.1.0/bin
ln -s -f phar.phar /usr/local/php-7.1.0/bin/phar
Installing PDO headers:           /usr/local/php-7.1.0/include/php/ext/pdo/
All done. PHP 7.1.0 is now installed. To confirm execute:
$ /usr/local/php-7.1.0/bin/php -v
PHP 7.1.0 (cli) (built: Jan  2 2017 09:09:59) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.1.0-dev, Copyright (c) 1998-2016 Zend Technologies

FastCGI Process Manager Installation

In this section we will install PHP-FPM to be used as a front for installation with webservers such as Nginx or Apache. First, create a symbolic link of newly compiled /usr/local/php-$version/sbin/php-fpm binary to make our installation tidy:
# ln -s /usr/local/php-7.1.0/sbin/php-fpm /usr/local/php-7.1.0/sbin/php-7.1.0-fpm
Still in the php-src directory, setup a default php.ini config:
# cp php.ini-production /usr/local/php-7.1.0/lib/php.ini
Enable PHP-FPM daemon configuration file:
# mv /usr/local/php-7.1.0/etc/php-fpm.d/www.conf.default /usr/local/php-7.1.0/etc/php-fpm.d/www.conf
Use text editor and create new /usr/local/php-7.1.0/etc/php-fpm.conf config file with a following content:
[global]
pid = /var/run/php7-fpm.pid
error_log = /var/log/php7-fpm.log
include=/usr/local/php-7.1.0/etc/php-fpm.d/*.conf

Enable Extra PHP modules

Insert any module directives you wish to enable into your php.ini config file. For example to enable Zend's OpCache PHP module run:
# echo "zend_extension=opcache.so" >> /usr/local/php-7.1.0/lib/php.ini

Configure PHP7-FPM init.d Script

First, insert a following modified php7-fpm.init script into /etc/init.d/ directory:
# wget -qO /etc/init.d/php7-fpm https://linuxconfig.org/images/php7-fpm.init
In case you have installed different version of PHP other than 7.1.0 use your text editor and change the following script variables to fit your installed PHP VERSION:
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/php-VERSION/sbin
NAME=php-VERSION-fpm
DAEMON=/usr/local/php-VERSION/sbin/$NAME
CONFFILE=/usr/local/php-VERSION/etc/php-fpm.conf
Lastly, make /etc/init.d/php7-fpm executable and run udpate-rc.d command:
# chmod +x /etc/init.d/php7-fpm
# update-rc.d php7-fpm defaults

Start PHP-FPM Daemon

Now you should be able to simply start PHP-FPM Daemon by executing service command:
# service php7-fpm start
# service php7-fpm status
[ ok ] php-7.1.0-fpm is running.
The PHP-FPM daemon is now listening on a localhost's port number 9000.