How To Install PHP In Linux – PHP 7.x On RHEL 7/8

Learn how to install PHP in Linux (redhat enterprise Linux /CentOS 7 & 8) – Learn how to update the PHP version in RHEL / CentOS 7 & 8 – verify your PHP version

UNDERSTANDING THE SUBJECT MATTER

What is PHP?

PHP, which formerly stands for Personal Home Pages, now stands for Hypertext Preprocessor is an open-source scripting language that is most appropriate for web development. (websites, blog sites, web applications, etc.)

PHP is a server-side scripting language, it only needs to be installed on the server and does not have to be installed on the client. It can be easily accessible by clients with the use of a web browser.

It is very powerful and large to run any powerful site you can talk of. It is powerful enough to run large websites like Twitter, LinkedIn, Facebook, Instagram, etc.

A PHP file can contain the combination of html code, JAvaScript, CSS, text and PHP code itself.

PHP being an open source can be freely downloaded and used.

What Platforms Can PHP Run On?

PHP can run on different platforms, Windows OS, MAC, and Linux inclusive and it is also compatible with almost every web server. (Nginx, Apache, IIS, etc).

It is also compatible with almost every database (MySQL, MS SQL, Postgre, Oracle, etc).

With perfect understanding and basics of how PHP works, you can begin to code in 1 hour, as it is very easy to use.

Moving forward, PHP file extension is ” .php ” and an example of a simple PHP file is,

<?php
echo '<p>This is a PHP script</p>';
?>

which will give the output of ” This is a PHP script” as it was tested in the ACTION TIME section of this article, step 9.

In this tutorial, this subject matter will look at how to install the latest version of PHP on a Red Hat Enterprise Linux 7 & 8.

Installing PHP requires that you have Nginx webserver or Apache webserver[link] already in place for testing after the installation.

PHP 5.4 comes with RHEL 7 repo by default. and PHP 7.2 comes with RHEL 8 repo by default.

To install PHP 7.x on an RHEL /CentOS 7 system, you will need to get it from the remi repository and before you can install the remi repository, you will need to install the EPEL repository.

To Install PHP 7.x on an RHEL 8 system, for PHP 7.2, all you need to do is to use the command,

yum install php

However, for other versions higher than 7.2, you will need to follow the step by step process in the “ACTION TIME” section or update the version 7.2 to a higher version as it was also done in the “ACTION TIME” section

ACTION TIME

1. Download and Install the Extra Packages for Enterprise Linux repository (EPEL)

[root@HQPRD2 ~]# yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-manager
epel-release-latest-7.noarch.rpm                                                                                                                          |  15 kB  00:00:00
Examining /var/tmp/yum-root-eEXQJM/epel-release-latest-7.noarch.rpm: epel-release-7-12.noarch
Marking /var/tmp/yum-root-eEXQJM/epel-release-latest-7.noarch.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package epel-release.noarch 0:7-12 will be installed
--> Finished Dependency Resolution

Dependencies Resolved


Total size: 24 k
Installed size: 24 k
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : epel-release-7-12.noarch                                                                                                                                      1/1
rhel-7-server-extras-rpms/x86_64/productid                                                                                                                | 2.1 kB  00:00:00
  Verifying  : epel-release-7-12.noarch                                                                                                                                      1/1

Installed:
  epel-release.noarch 0:7-12

Complete!
[root@HQPRD2 ~]#

2. Download and Install the remi repository

[root@HQPRD2 ~]# yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm

Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-manager
remi-release-7.rpm                                                                                                                                        |  20 kB  00:00:00
Examining /var/tmp/yum-root-eEXQJM/remi-release-7.rpm: remi-release-7.7-2.el7.remi.noarch
Marking /var/tmp/yum-root-eEXQJM/remi-release-7.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package remi-release.noarch 0:7.7-2.el7.remi will be installed
--> Finished Dependency Resolution
epel/x86_64/metalink                                                                                                                                      |  25 kB  00:00:00
epel/x86_64                                                                                                                                               | 5.3 kB  00:00:00
epel/x86_64/group_gz                                                                                                                                      |  90 kB  00:00:00
epel/x86_64/updateinfo                                                                                                                                    | 1.0 MB  00:00:02
epel/x86_64/primary_db                                                                                                                                    | 6.7 MB  00:00:08

Dependencies Resolved


Total size: 26 k
Installed size: 26 k
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : remi-release-7.7-2.el7.remi.noarch                                                                                                                            1/1
  Verifying  : remi-release-7.7-2.el7.remi.noarch                                                                                                                            1/1

Installed:
  remi-release.noarch 0:7.7-2.el7.remi

Complete!
[root@HQPRD2 ~]#

3. Enable the repository of the PHP version that is to be installed from the remi repository

. The repository for PHP 7.4 is remi-php74

. The repository for PHP 7.3 is remi-php73

. The repository for PHP 7.2 is remi-php72

. The repository for PHP 7.1 is remi-php71

. The repository for PHP 7.0 is remi-php70

You can verify by running the command,

[root@HQPRD2 ~]# ls -l /etc/yum.repos.d

total 216
-rw-r--r--. 1 root root   1050 Sep 17 16:25 epel.repo
-rw-r--r--. 1 root root   1149 Sep 17 16:25 epel-testing.repo
-rw-r--r--. 1 root root   2076 Apr 24  2019 mysql-community.repo
-rw-r--r--. 1 root root   2108 Apr 24  2019 mysql-community-source.repo
-rw-r--r--. 1 root root    106 Feb 19 00:30 nginx.repo
-rw-r--r--. 1 root root 145966 Feb 19 06:45 redhat.repo
-rw-r--r--. 1 root root    446 Feb 17 08:07 remi-glpi91.repo
-rw-r--r--. 1 root root    446 Feb 17 08:07 remi-glpi92.repo
-rw-r--r--. 1 root root    446 Feb 17 08:07 remi-glpi93.repo
-rw-r--r--. 1 root root    446 Feb 17 08:07 remi-glpi94.repo
-rw-r--r--. 1 root root    855 Feb 17 08:07 remi-modular.repo
-rw-r--r--. 1 root root    456 Feb 17 08:07 remi-php54.repo
-rw-r--r--. 1 root root   1314 Feb 17 08:07 remi-php70.repo
-rw-r--r--. 1 root root   1314 Feb 17 08:07 remi-php71.repo
-rw-r--r--. 1 root root   1314 Feb 20 00:50 remi-php72.repo
-rw-r--r--. 1 root root   1314 Feb 17 08:07 remi-php73.repo
-rw-r--r--. 1 root root   1314 Feb 17 08:07 remi-php74.repo
-rw-r--r--. 1 root root   2605 Feb 17 08:07 remi.repo
-rw-r--r--. 1 root root    750 Feb 17 08:07 remi-safe.repo
[root@HQPRD2 ~]#

In my case, I am going to install PHP version 7.2, hence, I will enable remi-php72

[root@HQPRD2 yum.repos.d]# yum-config-manager --enable remi-php72

Loaded plugins: langpacks, product-id, subscription-manager
=============================== repo: remi-php72 ===============================
[remi-php72]
async = True
bandwidth = 0
base_persistdir = /var/lib/yum/repos/x86_64/7Server
baseurl =
cache = 0
cachedir = /var/cache/yum/x86_64/7Server/remi-php72
check_config_file_age = True
compare_providers_priority = 80
cost = 1000
deltarpm_metadata_percentage = 100
deltarpm_percentage =
enabled = 1
enablegroups = True
exclude =
failovermethod = priority
ftp_disable_epsv = False
gpgcadir = /var/lib/yum/repos/x86_64/7Server/remi-php72/gpgcadir
gpgcakey =
gpgcheck = True
gpgdir = /var/lib/yum/repos/x86_64/7Server/remi-php72/gpgdir
gpgkey = file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi
hdrdir = /var/cache/yum/x86_64/7Server/remi-php72/headers
http_caching = all
includepkgs =
ip_resolve =
keepalive = True
keepcache = False
mddownloadpolicy = sqlite
mdpolicy = group:small
mediaid =
metadata_expire = 21600
metadata_expire_filter = read-only:present
metalink =
minrate = 0
mirrorlist = http://cdn.remirepo.net/enterprise/7/php72/mirror
mirrorlist_expire = 86400
name = Remi's PHP 7.2 RPM repository for Enterprise Linux 7 - x86_64
old_base_cache_dir =
password =
persistdir = /var/lib/yum/repos/x86_64/7Server/remi-php72
pkgdir = /var/cache/yum/x86_64/7Server/remi-php72/packages
proxy = False
proxy_dict =
proxy_password =
proxy_username =
repo_gpgcheck = False
retries = 10
skip_if_unavailable = False
ssl_check_cert_permissions = True
sslcacert =
sslclientcert =
sslclientkey =
sslverify = True
throttle = 0
timeout = 30.0
ui_id = remi-php72
ui_repoid_vars = releasever,
   basearch
username =

[root@HQPRD2 yum.repos.d]#

4. Install PHP 7.2 and it’s dependencies

[root@HQPRD2 ~]# yum install php php-fpm php-curl -y

Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-
              : manager
Resolving Dependencies
--> Running transaction check
---> Package php.x86_64 0:7.2.28-1.el7.remi will be installed
--> Processing Dependency: php-cli(x86-64) = 7.2.28-1.el7.remi for package: php-                                                                                        7.2.28-1.el7.remi.x86_64
---> Package php-common.x86_64 0:7.2.28-1.el7.remi will be installed
--> Processing Dependency: php-json(x86-64) = 7.2.28-1.el7.remi for package: php                                                                                        -common-7.2.28-1.el7.remi.x86_64
---> Package php-fpm.x86_64 0:7.2.28-1.el7.remi will be installed
--> Running transaction check
---> Package php-cli.x86_64 0:7.2.28-1.el7.remi will be installed
---> Package php-json.x86_64 0:7.2.28-1.el7.remi will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package          Arch         Version                   Repository        Size
================================================================================
Installing:
 php              x86_64       7.2.28-1.el7.remi         remi-php72       3.2 M
 php-common       x86_64       7.2.28-1.el7.remi         remi-php72       1.1 M
 php-fpm          x86_64       7.2.28-1.el7.remi         remi-php72       1.7 M
Installing for dependencies:
 php-cli          x86_64       7.2.28-1.el7.remi         remi-php72       4.8 M
 php-json         x86_64       7.2.28-1.el7.remi         remi-php72        66 k

Transaction Summary
================================================================================
Install  3 Packages (+2 Dependent packages)

Total download size: 11 M
Installed size: 42 M
Is this ok [y/d/N]: y
Downloading packages:
(1/5): php-json-7.2.28-1.el7.remi.x86_64.rpm               |  66 kB   00:01
(2/5): php-common-7.2.28-1.el7.remi.x86_64.rpm             | 1.1 MB   00:02
(3/5): php-7.2.28-1.el7.remi.x86_64.rpm                    | 3.2 MB   00:03
(4/5): php-cli-7.2.28-1.el7.remi.x86_64.rpm                | 4.8 MB   00:06
php-fpm-7.2.28-1.el7.remi.x86_ FAILED
http://remi.xpg.com.br/enterprise/7/php72/x86_64/php-fpm-7.2.28-1.el7.remi.x86_6                                                                                        4.rpm: [Errno 14] HTTP Error 502 - Bad Gateway
Trying other mirror.
(5/5): php-fpm-7.2.28-1.el7.remi.x86_64.rpm                | 1.7 MB   00:01
--------------------------------------------------------------------------------
Total                                              445 kB/s |  11 MB  00:24
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : php-common-7.2.28-1.el7.remi.x86_64                          1/5
  Installing : php-json-7.2.28-1.el7.remi.x86_64                            2/5
  Installing : php-cli-7.2.28-1.el7.remi.x86_64                             3/5
  Installing : php-7.2.28-1.el7.remi.x86_64                                 4/5
  Installing : php-fpm-7.2.28-1.el7.remi.x86_64                             5/5
  Verifying  : php-json-7.2.28-1.el7.remi.x86_64                                                                                                                    1/5
  Verifying  : php-7.2.28-1.el7.remi.x86_64                                                                                                                         2/5
  Verifying  : php-common-7.2.28-1.el7.remi.x86_64                                                                                                                  3/5
  Verifying  : php-cli-7.2.28-1.el7.remi.x86_64                                                                                                                     4/5
  Verifying  : php-fpm-7.2.28-1.el7.remi.x86_64                                                                                                                     5/5

Installed:
  php.x86_64 0:7.2.28-1.el7.remi                      php-common.x86_64 0:7.2.28-1.el7.remi                      php-fpm.x86_64 0:7.2.28-1.el7.remi

Dependency Installed:
  php-cli.x86_64 0:7.2.28-1.el7.remi                                                 php-json.x86_64 0:7.2.28-1.el7.remi

Complete!
[root@HQPRD2 ~]#

some other PHP dependencies are php-mysql, php-gd, php-opache. You can install if you need them.

5. verify the version of the PHP installed.

[root@HQPRD2 ~]# php -v

PHP 7.2.28 (cli) (built: Feb 18 2020 11:07:04) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
[root@HQPRD2 ~]#
how to install PHP in Linux

6. verify the installed modules.

[root@HQPRD2 ~]# php -m

[PHP Modules]
bz2
calendar
Core
ctype
curl
date
exif
fileinfo
filter
ftp
gettext
hash
iconv
json
libxml
openssl
pcntl
pcre
Phar
readline
Reflection
session
sockets
SPL
standard
tokenizer
zlib

[Zend Modules]

[root@HQPRD2 ~]#

7. Verify the PHP fpm service

[root@HQPRD2 ~]# systemctl status php-fpm

● php-fpm.service - The PHP FastCGI Process Manager
   Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
[root@HQPRD2 ~]# systemctl start php-fpm
[root@HQPRD2 ~]#

8. start the PHP fpm service and verify again

[root@HQPRD2 ~]# systemctl start php-fpm

[root@HQPRD2 ~]#

9. Do a PHP page test

I am going to be doing my page test with Apache web server. You will need to do some configuration changes if you are using nginx web server.

  • make sure your Apache server is up and running, you can confirm by using the command,
[root@HQPRD2 html]# systemctl status httpd

● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: active (running) since Thu 2020-02-20 01:57:54 PST; 17min ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 9124 (httpd)

  • create the file, page1.php
[root@HQPRD2 ~]# vi /var/www/html/page1.php

<?php
    phpInfo();
?>

  • create the file, page2.php
[root@HQPRD2 ~]# vi /var/www/html/page2.php

<?php
echo '<p>This is a PHP script</p>';
?>

Test using elinks or from your web browser.

you can download elinks if you don’t have it installed by using the command,

[root@HQPRD2 ~]#  yum installed elinks 
[root@HQPRD2 ~]#  elinks http://192.168.170.150/page1.php

[root@HQPRD2 ~]#  elinks http://192.168.170.150/page2.php

How To Update PHP 7.x to The Latest 7.x

As of when I am writing this article, the latest version of PHP is 7.4. In my case, I have PHP version 7.2 Installed and I want to update it to the latest.

To Update PHP 7.x version, do the following steps.

1. Verify the PHP version

[root@HQPRD2 ~]# php -v

PHP 7.2.28 (cli) (built: Feb 18 2020 11:07:04) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

2. Enable PHP 7.4 In the remi repository

[root@HQPRD2 ~]# yum-config-manager --enable remi-php74

Loaded plugins: langpacks, product-id, subscription-manager
=============================== repo: remi-php74 ===============================
[remi-php74]
async = True
bandwidth = 0
base_persistdir = /var/lib/yum/repos/x86_64/7Server
baseurl =
cache = 0
cachedir = /var/cache/yum/x86_64/7Server/remi-php74
check_config_file_age = True
compare_providers_priority = 80
cost = 1000
7Server/remi-php74/packages
hp74
ui_repoid_vars = releasever,
   basearch
username =

3. Update PHP version

[root@HQPRD2 ~]# yum update php

Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-
              : manager
                          4/30
  Installing : libsodium-1.0.18-1.el7.x86_64                               5/30
  Installing : php-sodium-7.4.3-1.el7.remi.x86_64                          
  Updating   : php-fpm-7.4.3-1.el7.remi.x86_64                             
  libsodium.x86_64 0:1.0.18-1.el7      php-sodium.x86_64 0:7.4.3-1.el7.remi

Updated:
  php.x86_64 0:7.4.3-1.el7.remi

Dependency Updated:
  php-bcmath.x86_64 0:7.4.3-1.el7.remi
  php-cli.x86_64 0:7.4.3-1.el7.remi
  php-common.x86_64 0:7.4.3-1.el7.remi
  php-fpm.x86_64 0:7.4.3-1.el7.remi
  php-gd.x86_64 0:7.4.3-1.el7.remi
  p6_64 0:7.4.3-1.el7.remi
  php-tidy.x86_64 0:7.4.3-1.el7.remi
  php-xml.x86_64 0:7.4.3-1.el7.remi

Complete!

4. Verify the new PHP version

[root@HQPRD2 ~]# php -v

PHP 7.4.3 (cli) (built: Feb 18 2020 11:53:05) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies

Tutorial Video On How To Install PHP In Linux & Update.

Your feedback is welcomed. If you love others, you will share with others

Be the first to comment

Leave a Reply

Your email address will not be published.


*