How to install PHP 7.x on a CentOS LAMP Stack

By default, CentOS 7 comes with support for PHP 5.4. Sadly that version has reached the end of its life in 2015and is no longer updated by the developers. If we want to stay up to date with the latest software, we may want to upgrade (if our applications are working with newer versions of PHP).

For CentOS users this either means to compile cutting edge versions from source and tweaking lots of scary system configurations – or dipping into the power of Software Collections. These are official pre-compiled packages by the software vendor, designed to run newer versions of software alongside those that are provided by default.

At the time of writing, PHP 7.2 is available but it’s not part of the software collections yet, so we’ll use  PHP 7.1 with FPM support under Apache (as it’s the recommended way to do so).

Let’s begin!

Enabling Software Collections

To make use of those repos we need, we’ll enable the Software Collections first. It’s as easy as installing a package with yum:

Now we can check which versions of PHP are available – by the time you read this article, something much higher than 7.2 might be available, who knows. Try it and see what comes up!

Installing PHP 7 packages

We need several packages for our endeavour: the PHP 7 base packages, FPM support and of course the MySQL extension. If we want to run WordPress on our stack, make sure to also install the GD extension so that image resizing will work properly.

We can install all these packages with this:

Adding FPM Support to Apache

I’m assuming your current web server is already in place, but not configured for FPM. If it is, feel free to skip this step.

Take a peek at the Apache PHP configuration file in /etc/httpd/conf.d/php.conf. There’s a line here towards the top that tells Apache how to execute PHP. By default that’s application/x-httpd-php.

Let’s change it to this:

We’ve simply commented out the original so we can bring it back easy if we need to.

Starting PHP 7 and Restarting Apache

To see if it all works, we need to start/restart our services. If you currently have FPM support enabled, stop it to avoid conflicts.

We might also want our configuration to be enabled at boot time:

To check if it’s all working, let’s take a look at the systemctl status:

Another way to check is to bring up the phpinfo() page in your web browser. It should look something like this:

 

Further Reading

  • https://www.softwarecollections.org/en/
  • https://wiki.centos.org/HowTos/php7
  • https://wiki.centos.org/AdditionalResources/Repositories/SCL
  • https://wiki.apache.org/httpd/php

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.

Add your voice!