How to install PHP 7 (and 7.1) on CentOS 7

CentOS 7 plus PHP 7

PHP 7 is a huge revolution in the PHP world. Bringing up to 100% speed gain under certain workloads. Still, PHP 7 adoption is not as widespread as it should. As a matter of fact CentOS 7 still uses PHP 5.4, although it’s safe, with some tricks you can install PHP 7 and enjoy all of its benefits on CentOS 7.

A foreword

PHP 7 is great, fast and absolutely one of the best re-engineering a language has ever seen. But beware, applications are not automatically compatible. Although a certain degree of backward compatibility is guaranteed, there are many deprecations and many removals (e.g. the mysql driver has been removed in favour of mysqli or MYSQL_PDO). Before upgrading to PHP 7.0 you should read the migration guide, and if you intend to install PHP 7.1, there’s a guide too.

CentOS 7 still uses PHP 5.4, and there’s a good reason Red Hat is still maintaining this version. Following this guide you will add repositories that are not part of CentOS by themselves (epel and webtatic). Installing these packages will allow you to leverage your package manager without recompiling PHP. This is especially good, but if you’re on RHEL 7 (rather than CentOS7) you will probably break Red Hat Product support. Installing these package is at user’s own risk.

Important
I take absolutely NO responsibility of what you do with your machine; use this tutorial as a guide and remember you can possibly cause data loss if you touch things carelessly.

Installing PHP 7(.1)

The process is quite straightforward, first we need to install epel and the webtatic repository:

Once done, you can decide to go for version 7 or 7.1:

77.1

If you need to use PHP with apache:

Or if you need PHP-FPM:

If you want to use the new PHP-opcache (suggested):

If you need PHP-cli:

If you need to use PHP with apache:

Or if you need PHP-FPM:

If you want to use the new PHP-opcache (suggested):

If you need PHP-cli:

That’s it, you now have a functioning PHP 7(.1) installation, you can simply verify so (if you installed PHP-cli) by issuing:

Upgrading PHP

This one is for advanced users. If you already have PHP 5.4 installed, you might want to upgrade without removing PHP altogether. In this cases you can use yum-plugin-replace to achieve such behaviour. Be careful, this procedure is prone to errors and can lead to undesired behaviours. Once again: be careful.

77.1

You can replace PHP 5.4 by doing:

You can replace PHP 5.4 by doing:

The following two tabs change content below.
The IT guy with a little boredom look in his eyes, fond of computers since forever he now works as a freelancer in the IT and shares his experiences through this blog.

Latest posts by mark (see all)

You may also like...