How to compile PHP 7.1 in Ubuntu 16.04

PHPOn December 1, 2016 the PHP development community released version 7.1, which added 12 new features , we mention some of them below:

Nullable types: Type declarations for parameters and return values can now be marked as nullable by prefixing the type name with a question mark. This signifies that as well as the specified type, NULL can be passed as an argument, or returned as a value, respectively.
Void functions: The void type functions should not return any value so they should use an empty return or omit it completely, I think that if you omit the return it gives more clarity to the code since it is more consistent with the definition of void, the NULL value is not a valid return value for void functions.
Symmetric array destructuring : The shorthand array syntax ([]) may now be used to destructure arrays for assignments (including within foreach).
Class constant visibility: Now the class constants can be defined visibility or scope: public (default), protected or private.
iterable pseudo-type: A new pseudo-type (similar to callable) called iterable has been introduced. It may be used in parameter and return types, where it accepts either arrays or objects that implement the Traversable interface.
Multi catch exception handling: Multiple exceptions per catch block may now be specified using the pipe character (|).
Support for keys in list(): You can now specify keys in list(), or its new shorthand [] syntax. This enables destructuring of arrays with non-integer or non-sequential keys.


  • Update to the latest version of PHP
  • Get benefits from the latest language features
  • Correct errors from previous versions
  • Customize the installation process


This tutorial assumes that:

  • You have some knowledge about GNU/Linux
  • You have Ubuntu GNU/Linux installed
  • You are familiar with the command interpreter
  • You are familiar with the compilation process

Update your OS

$ sudo bash -c 'apt update && apt upgrade'

Download PHP

The lastest released 7.1 version is .33 so you can download it using the following command

$ wget -O php-7.1.33.tar.xz


We can decompress the downloaded file before using the tar tool

$ tar xJvf php-7.1.33.tar.xz.


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 script from Github is a bash script that helps with the installation of the GNU compiler collection, essential tools and necessary dependencies.

# wget & chmod a+x -c

then run the script by typing:


You can modify the above script according your needs.

Download script from Github is bash script that helps with the PHP compilation process. Enables the most common extensions: curl, openssl, intl, mysql, pcre,… and it allows you to install PHP in a custom DIR chosen by you, It allows to compile PHP with Apache support (prefork or worker) or with PHP-FPM support.

You can modify according to your needs.

If you compile PHP with PHP-FPM support then you need to modify and set the user and group under which will run our PHP-FPM.

wget & chmod a+x -c

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

#./ -fs php-7.0.2

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


# cd php-7.1.33 & make install

Creating configuration file

In the PHP source code are included 2 versions of the ini file. Due 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

In order to the php-fpm accepts incoming connection on all network interfaces set the value of the directive listen to:

listen 9000

Or if we want php-fpm 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 set the directive listen.allowed_clients to:

listen.allowed_clients = IP1, IP2, IP3, IP4

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

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 "" > /etc/php/conf.d/20-opcache.ini

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/

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 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 IP:9000;
     include snippets/fastcgi-php.conf;  

IP is where the php-fpm service is listening on, open the file /etc/nginx/snippets/fastcgi-php.conf look 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


Type in your browser

$ firefox http://IP/info.php

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

How to compile PHP from the source code, 1 (11)

1 thought on “How to compile PHP 7.1 in Ubuntu 16.04”

  1. thank you but it’s no longer working, article is bit outdated, please update as PHP 7.1 is no longer supported on ubantu 16.04

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.