How to install PHP 7.* 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.* you should read the appropriate migration guide:

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.*

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

# yum install epel-release
# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

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

77.17.2

If you need to use PHP with apache:

# yum install php70w

Or if you need PHP-FPM:

# yum install php70w-fpm

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

# yum install php70w-opcache

If you need PHP-cli:

# yum install php70w-cli

If you need to use PHP with apache:

# yum install mod_php71w php71w-common

Or if you need PHP-FPM:

# yum install php71w-fpm

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

# yum install php71w-opcache

If you need PHP-cli:

# yum install php71w-cli

If you need to use PHP with apache:

# yum install mod_php72w php72w-common

Or if you need PHP-FPM:

# yum install php72w-fpm

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

# yum install php72w-opcache

If you need PHP-cli:

# yum install php72w-cli

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

# php --version
PHP 7.1.4 (cli) (built: Apr 15 2017 08:07:03) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies

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.17.2

You can replace PHP 5.4 by doing:

# yum install yum-plugin-replace
# yum replace php-common --replace-with=php70w-common

You can replace PHP 5.4 by doing:

# yum install yum-plugin-replace
# yum replace php-common --replace-with=php71w-common

You can replace PHP 5.4 by doing:

# yum install yum-plugin-replace
# yum replace php-common --replace-with=php72w-common
Image courtesy of mark | marksei
mark

You may also like...

Leave a Reply

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