Compile PHP 7 on Debian 8

This entry is part 5 of 5 in the series: How to compile PHP from the source code

Introduction

A look at PHP-5.6

On December 3 the PHP community released version 7 of this popular programming language considered to be one of the biggest updates in all history of PHP due the engine (Zend Engine) was refactored in order to achieve best performance. Among the new features of PHP 7 we can mention:

  • Improved performance: now PHP 7 is 2 times faster than PHP 5.6
  • Best use of memory: PHP 7 uses less memory than previous versions
  • Consistent support for 64 bit
  • Improved Exception hierarchy
  • Several fatal errors became exceptions
  • Anonymous classes can now be defined
  • 2 new operators was added: null coalescing (?) and spaceship (<=>)
  • Scalar statements and return type
  • Obsolete extensions were deleted
  • Other

Why compile from source?

Compiling from source is a good choice if you want to:

  • Upgrade to the latest version
  • Benefit from the latest features of the language
  • Correct errors from previous versions
  • Customize the installation process

Goals

After you finish this tutorial you will be able to compile PHP 7 on a Debian GNU/Linux distribution

Note: The compilation process is similar for any distribution.

Prerequisites

This tutorial assumes that you have

  • Some knowledge of GNU/Linux
  • Debian GNU/Linux installed
  • Be familiar with the command interpreter
  • Be familiar with the compilation process

Download PHP

The PHP version available at the time of writing this tutorial is the 7.0.2, to download it, type:

$ wget http://us1.php.net/distributions/php-7.0.2.tar.xz

Unpack

We can decompress the downloaded file before using the tar tool

$ tar xJvf php-7.0.2.tar.xz.

where:

x = Extract
J = allows you to manipulate extensions xz
v = verbose
f = file

If the above command issues an error message you must install the package xz-utils details in: Error to decompress .tar.xz file.

Download php-dev-install-dep.sh script from Github

PHP-dev-install-dep.sh is a bash script which facilitates the process of installation of the GNU compiler collection, essential tools and necessary dependencies.

# wget https://raw.githubusercontent.com/yoander/sysadmin/master/shscript/php-dev-install-dep.sh & 
chmod a+x -c php-dev-install-dep.sh

then run the script by typing:

#./php-dev-install-dep.sh

You can modify the above script to suit your needs.

Download php-build.sh script from Github

php-build.sh facilitates the process of compilation of PHP. Enables the most common extensions: curl, openssl, intl, mysql, pcre,… and it allows you to install PHP in a DIR chosen by you It offers the possibility to compile PHP with support for Apache (prefork or worker) or with support for PHP-FPM.

You can modify php-build.sh according to your needs.

If you compile PHP with support for PHP-FPM php-build.sh should be edited and set the user and group under which will run our PHP-FPM.

# wget https://raw.githubusercontent.com/yoander/sysadmin/master/shscript/php-build.sh & 
chmod a+x -c php-build.sh

Compile PHP with support for PHP-FPM and systemd

PHP-FPM (FastCGI Process Manager) is an alternative implementation of FastCGI protocol it is include in PHP tree since version 5.3.3.

systemd is a replacement to the SysV initialization system. systemd is also a suite of services and settings for the GNU/Linux operating system administration.

First we create the necessary DIR that will be used during the build process.

# mkdir -p /etc/php /etc/php/conf.d /usr/lib/php/modules /usr/share/pear

We install systemd development libraries

# apt-get install libsystemd-dev

then we compile

#./php-build.sh -fs php-7.0.2

where -f=PHP-FPM support, -s=integration with systemd

Install

# cd php-7.0.2 & make install

Creating configuration file

The source code of PHP comes with 2 versions of the ini file. Since we are compiling PHP on a PROD server we type:

# cp -v php.ini-production /etc/php/php.ini

Adjust the directives according to your needs

Creating the configuration file for PHP-FPM

# cp -pv /etc/php/php-fpm.conf.default /etc/php/php-fpm.conf

Edit the file /etc/php/php-fpm.conf

To enable that php-fpm service listen for all interfaces set the value of the directive listen to:

listen 9000

Or if we want NGINX listens on a specific IP address

listen IP:9000

IP can be IPv4 or IPv6

To restrict access to the service php-fpm and allow only certain IPs establish the directive listen.allowed_clients to:

listen.allowed_clients = IP1, IP2, IP3, IP4

IP1, IP2, IP3, IP4 can be IPv4 or IPv6

We enable OPcache

OPcache improves the performance of your applications storing precompiled code in the shared memory allowing to server greater number of requests per second.

# echo "zend_extension=opcache.so" > /etc/php/conf.d/20-opcache.ini

We create PHP-FPM initialization service

# cp -v ./sapi/fpm/php-fpm.service /etc/systemd/system/php-fpm.service

If the user and group under which the service will run do not exist then we must create them.

# groupadd - www-data system &  
useradd -system -m -d /var/www - s /usr/sbin/nologin - g www-data www-data

The above commands create the user www-data and group www-data

-m = create HOME DIR if not exist
-d = DIR HOME for the user in question
-s = login shell (do not login because www-data is not an authenticated user on the operating system)
-g = group will the user belong which

If the user and Group www-data exist then create the /var/www DIR

# mkdir -p /var/www/ 

For security reasons, we will change the owner and group to the /var/www DIR

# chown root:root -c /var/www/ & chmod 755 /var/www/ 

We create the config file for the www pool

# cp -v /etc/php/php-fpm.d/www.conf.default /etc/php/php-fpm.d/www.conf

Start PHP-FPM service with the operating system

# systemctl enable php-fpm

Start PHP-FPM service

# systemctl php-fpm start

Check the PHP-FPM service

# systemctl status php-fpm

Create the info.php file

# echo '<?php phpinfo();' > /var/www/info.php

NGINX

NGINX is a web server with excellent performance and low memory consumption. NGINX can be used as reverse-proxy and load balancer In this example we will use NGINX as a reverse proxy to check our PHP-FPM service.

Installing NGINX

# apt-get install nginx

Edit /etc/nginx/nginx.conf

We look for server section and add the following block

location ~ .php$ {
    root /var/www; 
    fastcgi_pass 192.168.33.10:9000;
    include snippets/fastcgi-php.conf;          
}

192.168.33.10 is the IP where the php-fpm service is listening on, open the file

/etc/nginx/snippets/fastcgi-php.conf

Looking for and comment the line

try_files $fastcgi_script_name = 404;

Start NGINX with the operating system

# systemctl enable nginx

Start the NGINX service

# systemctl start nginx

Check the NGINX service

# systemctl nginx status

Try

Type in your browser

$ firefox http://IP/info.php

IP is the IP or url where the NGINX server is listening.

PHP 7 info

Conclusions

Now that you've learned how to compile PHP you can get all the advantages (correction of errors, improvements to performance, latest features) without waiting for the distribution packagers to update the PHP version.




Leave a Comment

Your email address will not be published. Required fields are marked *