How to install php on RHEL 8 / CentOS 8 Linux

In the RHEL 8 / CentOS 8 Linux systems, the way software is organized has changed: critical packages are now contained in the BaseOs repository, while the AppStream one contains multiple versions of some of the most common used applications and programming languages which are organized in modules, and updated independently of the distribution release cycle. This strategy has been adopted in order to avoid the problem of being stuck with a certain, too outdated or too recent release of a certain software.

In this tutorial you will learn:

  • How to install PHP on RHEL 8 / CentOS 8
  • What are software modules in RHEL 8 / CentOS 8
  • How to install and switch between different versions of the PHP module
php-modules-rhel8

Available php modules on RHEL 8

Software Requirements and Conventions Used

Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System RHEL 8 / CentOS 8
Software No additional software is needed to follow this tutorial
Other Permission to run command with root privileges.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

What is PHP?

PHP (PHP: Hypertext Preprocessor) doesn’t need a lot of presentations: this general-purpose programming language was originally created in 1994 by Rasmus Lerdorf and it is released under the PHP license, as open source software. It’s main field of application is the web, but the language can be used in other contexts. RHEL 8 / CentOS 8 comes with two main repositories enabled: BaseOs and AppStream.

The first contains the core
packages of the distribution which follows the life cycle of the distributions itself; the second contains different versions of several userspace applications, which are updated independently of the main distribution. The software contained in the latter is organized in modules. Each module provides different versions of an application making possible to switch between the available ones (but not to install them simultaneously). In this tutorial we will see how easy is to install PHP and switch between different versions of this language on the RHEL 8 / CentOS 8 distributions.

What PHP version?

While the 5.6 version of PHP is now officially dead (it reached EOL on the 1 Jan 2019), three branches of the programming language are currently maintained: 7.1, which actually receives only security fixes and will be patched until 1 Dec 2019, and versions 7.2 and 7.3, which will be supported respectively until 30 Nov 2020 and 6 Dec 2021.



The version of PHP supplied by default in RHEL 8 / CentOS 8, from the AppStream repository is 7.2.11, as we can verify by running the dnf info php command, which returns the following output:

Available Packages
Name         : php
Version      : 7.2.11
Release      : 1.el8+2002+9409c40c
Arch         : x86_64
Size         : 1.5 M
Source       : php-7.2.11-1.el8+2002+9409c40c.src.rpm
Repo         : rhel-8-for-x86_64-appstream-beta-rpms
Summary      : PHP scripting language for creating dynamic web sites
URL          : http://www.php.net/
License      : PHP and Zend and BSD and MIT and ASL 1.0
Description  : PHP is an HTML-embedded scripting language. PHP attempts to make it
             : easy for developers to write dynamically generated web pages. PHP also
             : offers built-in database integration for several commercial and
             : non-commercial database management systems, so writing a
             : database-enabled webpage with PHP is fairly simple. The most common
             : use of PHP coding is probably as a replacement for CGI scripts.
             :
             : The php package contains the module (often referred to as mod_php)
             : which adds support for the PHP language to Apache HTTP Server.

Are there other PHP versions available in the AppStream repository?. To answer this question we can run a dedicated subcommand of dnf, module, and ask information about the PHP module, by using the list action:

$ sudo dnf module list php
Red Hat Enterprise Linux 8 for x86_64 - AppStream Beta (RPMs)
Name                       Stream                        Profiles                                          Summary
php                        7.1                           devel, minimal, default [d]                       PHP scripting language
php                        7.2 [d]                       devel, minimal, default [d]                       PHP scripting language

Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled

When running the dnf module list command, by default, all available modules are displayed. To restrict the query to only a specific module, we provide the module name to the command. In this case we can see that two streams exist for the PHP module, covering versions 7.1 and 7.2 of the language and related libraries. As we saw, the latter version is the one that gets installed by default: in the output of the command we run above this is highlighted by the use of the [d] mark in the second column, which, as specified in the legend, stands for default.

Another interesting part of the output is the Profiles column, where the available profiles for a stream are reported. Multiple profiles can be installed simultaneously and each one of them comprehends a different set of packages. The [d] notation is also used here to mark the default profile selected when installing the stream.

To install package of a specific stream of a software module we must use the name:stream/profile notation. For example, to install the version 7.1 of PHP, we can run:

$ sudo dnf module install php:7.1/default


In this case we could have omitted the profile name, since default is used automatically if another one is not specified. If we use the same command we run above, we can now see that the 7.1 stream is marked with [e], which stands for enabled, and the default profile of the stream is marked with [i], which indicates that the profile of the stream is installed:

$ sudo dnf module list php
Red Hat Enterprise Linux 8 for x86_64 - AppStream Beta (RPMs)
Name                      Stream                       Profiles                                             Summary
php                       7.1 [e]                      devel, minimal, default [d] [i]                      PHP scripting language
php                       7.2 [d]                      devel, minimal, default [d]                          PHP scripting language

If we have a stream installed and we want to switch to another of the available ones, we can simply install the latter: the packages will be upgraded or downgraded automatically, depending on the situation. To switch to the 7.2 stream, for example, we would run:

$ sudo dnf module install php:7.2/default
=======================================================================================================================================================
 Package                    Arch                 Version                                     Repository                                           Size
=======================================================================================================================================================
Upgrading:
 php-common                 x86_64               7.2.11-1.el8+2002+9409c40c                  rhel-8-for-x86_64-appstream-beta-rpms               653 k
 php-mbstring               x86_64               7.2.11-1.el8+2002+9409c40c                  rhel-8-for-x86_64-appstream-beta-rpms               580 k
 php-xml                    x86_64               7.2.11-1.el8+2002+9409c40c                  rhel-8-for-x86_64-appstream-beta-rpms               188 k
 php-fpm                    x86_64               7.2.11-1.el8+2002+9409c40c                  rhel-8-for-x86_64-appstream-beta-rpms               1.6 M
 php-json                   x86_64               7.2.11-1.el8+2002+9409c40c                  rhel-8-for-x86_64-appstream-beta-rpms                73 k
 php-cli                    x86_64               7.2.11-1.el8+2002+9409c40c                  rhel-8-for-x86_64-appstream-beta-rpms               3.1 M
Switching module streams:
 php                                             7.1 -> 7.2

Transaction Summary
=======================================================================================================================================================
Upgrade  6 Packages

Total download size: 6.2 M
Is this ok [y/N]: y

Similarly, if we want to remove a specific module stream we can use the remove action instead of the “install” one. This causes a stream to be removed: if we specify a profile, only the packages belonging to that profile are removed, otherwise, all the profiles of a stream are interested by the action. For example, to uninstall all packages belonging to all profiles of the 7.2 stream, we would simply run:



$ sudo dnf module remove php:7.2

To remove only the packages related to the devel profile of the same stream, instead, we would run:

$ sudo dnf module remove php:7.2/devel

Conclusions

The new release of Red Hat Enterprise Linux, comes with a change in the way software is organized. The BaseOs repository contains the packages which represents the core of the distributions, and follow its release cycle, the AppStream one instead, contains userspace software organized in modules providing different versions, or “streams” available to be installed: PHP is also provided this way.

The versions of PHP available by
default in RHEL 8 / CentOS 8 are 7.1.20 and 7.2.11. It’s possible to install other versions of the programming language from external repositories, like Remi, but I recommend to stick with the default software sources if it’s not strictly necessary to do otherwise.