How to Install PHP 7 with Apache and MariaDB on CentOS 7/Debian 8

Last last week (more precisely on Aug. 21, 2015), the PHP development team announced the availability of the latest release of PHP 7 and encouraged users and developers worldwide to test it.

However, we must note that since this is a RC (Release Candidate) version, it is expected that it may have bugs or incompatibilities with existing setups so users are being asked to report them using the bug tracking system and to not use PHP 7 in production while it remains in that phase.

Install PHP 7 on CentOS 7 and Debian 8
Compile PHP 7 on CentOS 7 and Debian 8

The bright side is that this version includes several fixes (you may want to refer to this page in the project’s GitHub repository for a detailed list of the new features and enhancements), with the most distinguishing feature being a remarkable performance increase when compared to previous versions.

This article will walk you through the process of installing and compiling PHP 7 RC1 from source tarball along with Apache and MariaDB on CentOS 7 and Debian 8 Jessie. The same instructions also works on CentOS based distributions like RHEL, Fedora, Scientific Linux and Debian based such as Ubuntu/Mint.

​Installing PHP 7 in CentOS 7 and Debian 8

As stated in the introduction, since this version is a RC instead of a stable release, we cannot reasonably expect to find it in the repositories. For that reason, we will have to download the source code and compile the program from scratch.

Before we do that, however, we need to remember that in order to better take advantage of PHP 7 and perhaps the best way to try it out is installing it along with Apache and MariaDB – which we CAN find in the repositories:

On CentOS 7

# yum update && yum install httpd mariadb mariadb-server

On Debian 8

# aptitude update && aptitude install apache2 mariadb-server mariadb-client mariadb.common

In either case, the tarball with the source code of PHP can be downloaded and extracted as follows:

# wget https://downloads.php.net/~ab/php-7.0.0RC1.tar.gz
# tar xzf php-7.0.0RC1.tar.gz -C /opt

Once done, let’s move into /opt/php-7.0.0RC1 and execute the buildconf script with the –force switch in order to force the build of a RC version:

# ls
PHP 7 buildconf
PHP 7 buildconf
# cd /opt/php-7.0.0RC1.tar.gz
# ./buildconf --force
Build PHP 7
Build PHP 7

Now it’s time to execute our well-known configure command. While the options below will ensure a standard PHP 7 installation, you can refer to the complete option list in the PHP manual in order to better customize the installation as per your needs:

# ./configure \
--prefix=$HOME/php7/usr \
--with-config-file-path=$HOME/php7/usr/etc \
--enable-mbstring \
--enable-zip \
--enable-bcmath \
--enable-pcntl \
--enable-ftp \
--enable-exif \
--enable-calendar \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-wddx \
--with-curl \
--with-mcrypt \
--with-iconv \
--with-gmp \
--with-pspell \
--with-gd \
--with-jpeg-dir=/usr \
--with-png-dir=/usr \
--with-zlib-dir=/usr \
--with-xpm-dir=/usr \
--with-freetype-dir=/usr \
--enable-gd-native-ttf \
--enable-gd-jis-conv \
--with-openssl \
--with-pdo-mysql=/usr \
--with-gettext=/usr \
--with-zlib=/usr \
--with-bz2=/usr \
--with-recode=/usr \
--with-mysqli=/usr/bin/mysql_config \
--with-apxs2

If you run into the following error:

configure: error: no acceptable C compiler found in $PATH
see 'config.log' for more details
PHP 7 Configure
PHP 7 Configure

Simply install gcc and dependencies with following command and run the above configure command again.

# yum install gcc       [On CentOS 7 box]
# aptitude install gcc  [On Debian 8 box]

You’ll be on your way to compiling PHP 7, which may take a while. If there are other missing libraries or resources, this process will fail but you can always install them and run configure again.

For example, I had to install libxml2-devel after getting the following error message:

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

Unfortunately, we cannot possibly cover all case scenarios since the installed software may vary from one system to another. During the installation, you may want to refer to this page which outlines several errors that you may run into while installing PHP from source, along with their respective solutions.

On CentOS 7

Here’s a complete list of the packages I had to install in my CentOS 7 box before being able to complete the configure process:

gcc
libxml2-devel
pkgconfig
openssl-devel
bzip2-devel
curl-devel
libpng-devel
libpng-devel
libjpeg-devel
libXpm-devel
freetype-devel
gmp-devel
libmcrypt-devel
mariadb-devel
aspell-devel
recode-devel
httpd-devel

You can install all of the above required packages with one single yum command as shown.

# yum install gcc libxml2-devel pkgconfig openssl-devel bzip2-devel libpng-devel libpng-devel libjpeg-devel libXpm-devel freetype-devel gmp-devel libmcrypt-devel mariadb-devel aspell-devel recode-devel httpd-devel

The following message indicates that configure finished successfully:

PHP 7 Configuration Successful
PHP 7 Configuration Successful

Then run,

# make
# make install

When the installation is complete you can check the version using the command line:

Check PHP Version
Check PHP Version

On Debian 8

In Debian, I had to install the following packages for the configure process to complete successfully:

make
libxml2-dev
libcurl4-openssl-dev
libjpeg-dev
libpng-dev
libxpm-dev
libmysqlclient-dev
libicu-dev
libfreetype6-dev
libxslt-dev
libssl-dev
libbz2-dev
libgmp-dev
libmcrypt-dev
libpspell-dev 
librecode-dev
apache2-dev

You can install all the above required packages with apt-get command on Debian 8.

# apt-get install make libxml2-dev libcurl4-openssl-dev libjpeg-dev libpng-dev libxpm-dev libmysqlclient-dev libicu-dev libfreetype6-dev libxslt-dev libssl-dev libbz2-dev libgmp-dev libmcrypt-dev libpspell-dev librecode-dev apache2-dev

Then add, –with-libdir=/lib/x86_64-linux-gnu to the configure options, and create the following symlink to the gmp.h header file:

# ln -s /usr/include/x86_64-linux-gnu/gmp.h /usr/include/gmp.h

Then ran make and make install as in the previous case. Within 10-15 minutes the compilation should have completed and we can verify the installed PHP version as before:

# make
# make install
Check PHP Version in Debian
Check PHP Version in Debian 8

​Setting Up php.ini and Testing PHP 7 Installation

When you install PHP from source, two sample php.ini are provided. In this case, they are located inside /opt/php-7.0.0RC1:

# ls -l /opt/php-7.0.0RC1 | grep php.ini
PHP 7 Configuration php.ini File
PHP 7 Configuration php.ini File

You now need to copy one of them to /usr/local/lib, which is designated as the default location for such file as per the Install notes:

# cp /opt/php-7.0.0RC1/php.ini-development /usr/local/lib

And don’t forget to add this configuration directive to the main configuration files of Apache.

/etc/httpd/conf/httpd.conf    [On CentOS 7 box]
/etc/apache2/apache2.conf in  [On Debian 8 box] 
LoadModule php7_module        /usr/lib64/httpd/modules/libphp7.so
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>

In Debian 8 you can omit the LoadModule line and also you need to remove and create the following symbolic links to the indicated Apache modules:

# cd /etc/apache2
# rm mods-enabled/mpm_event.conf
# rm mods-enabled/mpm_event.load
# ln -s mods-available/mpm_prefork.conf mpm_prefork.conf
# ln -s mods-available/mpm_prefork.load mpm_prefork.load

Then, restart the web server:

# systemctl restart httpd     [On CentOS 7 box]
# systemctl restart apache2   [On Debian 8 box]

If starting up Apache in CentOS 7 returns an error message saying it can’t find the libphp7.so module, simply copy to the indicated path from /opt/php-7.0.0RC1/.libs/libphp7.so.

The classic way to test a PHP/Apache installation is using a phpinfo() file. Create a file named test.php with the following contents in the web server’s document root (/var/www/html in both distributions):

<?php
phpinfo();
?>

And launch a browser in a client within your network to test:

http://localhost/test.php
OR
http://IP-address/test.php
Verify PHP 7 info in CentOS 7
Verify PHP 7 info in CentOS 7
Verify PHP 7 info in Debian 8
Verify PHP 7 info in Debian 8

Summary

In this article we have explained how to install PHP 7 from source code, the newest RC of this popular server-side scripting language that aims at improving performance at unprecedented values. Until it reaches the stable in November of this year 2015, you are STRONGLY advised to NOT use this release in a production environment.

If you have any questions / comments / suggestions about this article, feel free to let us know using the form below.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Gabriel Cánepa
Gabriel Cánepa is a GNU/Linux sysadmin and web developer from Villa Mercedes, San Luis, Argentina. He works for a worldwide leading consumer product company and takes great pleasure in using FOSS tools to increase productivity in all areas of his daily work.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

8 Comments

Leave a Reply
  1. Hello
    I have installed MariaDB 10.1.21 and PHP7 from Webtatic . Both works, but when I try to execute php script $connection=mysqli_connect($host,$db_login,$db_password);

    I have an error “PHP Warning: mysqli_connect(): Headers and client library minor version mismatch. Headers:50550 Library:100121”

    I execute php script phpinfo() and got: PHP Version 7.0.14; Client API library version 10.1.21-MariaDB ; Client API header version 5.5.50-MariaDB

    What should I do? Do I need to reinstall MariaDB for version 5.5 or not? Do you have any solution?

    Reply
  2. 1. php5 uninstall first?
    2. -with open-ssl not works, I have compiled without that
    3. /usr/lib64/httpd/modules/libphp7.so not existing directory and file.
    Result: Job for apache2.service failed.

    Reply
    • @Artyom,
      1) Yes, it is recommended that you uninstall previous versions of PHP present on your system.
      2) Have you install mod_ssl? It should work if you did.
      3) Have you tried searching for that *.so file elsewhere on the system?

      Reply
  3. Hello I’m a new Linux user and have not been able to locate the HTTPD.conf file to add the following code. I’m still in the learning process, a command line structure to add this from the terminal, will be greatly appreciated. Thank you,

    LoadModule php7_module /usr/lib64/httpd/modules/libphp7.so

    SetHandler application/x-httpd-php

    Reply
  4. When I execute sudo make install command, it outputs the following error.

    Installing PEAR environment: /usr/lib/php/

    Warning: fopen(): SSL operation failed with code 1. OpenSSL Error messages:
    error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed in /home/bumper/php-src/pear/fetch.php on line 66

    Warning: fopen(): Failed to enable crypto in /home/bumper/php-src/pear/fetch.php on line 66

    Warning: fopen(https://pear.php.net/install-pear-nozlib.phar): failed to open stream: operation failed in /home/bumper/php-src/pear/fetch.php on line 66

    Error..
    fopen(https://pear.php.net/install-pear-nozlib.phar): failed to open stream: operation failed
    make: *** [install-pear] Error 1

    So what could be the problem? Thank you.

    Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.