How to install PHP from source on CentOS

PHP-IconI have recently installed PHP 7 from source on a fresh minimal CentOS 7 box. No previous version of PHP was installed, and I thought I’d give 7 a spin. There were a few pitfalls I hadn’t come across before, so here’s what worked for me.

Downloading and extracting the source code

It sounds crazy, but this was the hardest part of the whole installation! There were two problems I’ve encountered here.

The first was that PHP offer downloads via a mirror. A direct link may look something like this: http://php.net/get/php-7.0.12.tar.bz2/from/this/mirror. This means that if we were to download this file using wget, it would be saved as “mirror”. Now what we want.

So instead we can ask wget to give the download a different name using the -O parameter, like so:

wget -O php7.tar.bz2 http://php.net/get/php-7.0.12.tar.bz2/from/this/mirror

This will save our file as php7.tar.bz2 instead. So far so good. Unpacking this file seems to be impossible. From what the internet tells me, this should be the correct way of extracting a tar.bz2 file:

tar -jxvf php7.tar.bz2

But that didn’t work, not matter how hard I tried. All I ever got was a “non-recoverable” error. Which sucks. In the end I extracted the file on my Mac, created a ZIP archive and downloaded that instead. Unnecessarily cumbersome and idiotic, but worked. Finally I had them on my CentOS box.

Building the source code

Jumping into the extracted directory, the configure command can prepare the build. At this stage I encountered an error:

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

This can be fixed by installing the libxml2-devel package (NOT libxml2 as the error would have you believe). Let’s do that and run configure again:

yum install libxml2-devel
./configure

Now we can run make, followed by make test to see if the installation is going to go well. This will take a few minutes.

make
make test

Feel free to skip “make test” if you’re in a hurry. In my case, after over 10.000 tests, PHP told me this:

You may have found a problem in PHP.
This report can be automatically sent to the PHP QA team at
http://qa.php.net/reports and http://news.php.net/php.qa.reports
This gives us a better understanding of PHP's behavior.
If you don't want to send the report immediately you can choose
option "s" to save it. You can then email it to qa-reports@lists.php.net later.

Perplexed yet unfazed, I continued on and installed PHP anyway:

make install

And only moments later, PHP 7 was running on my CentOS system.

php -v

PHP 7.0.12 (cli) (built: Nov  8 2016 06:59:14) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies

Not sure what else to tell you.





Jay is the CEO and founder of WP Hosting, a boutique style managed WordPress hosting and support service. He has been working with Plesk since version 9 and is a qualified Parallels Automation Professional. In his spare time he likes to develop iOS apps and WordPress plugins, or draw on tablet devices. He blogs about his coding journey at http://wpguru.co.uk and http://pinkstone.co.uk.