Migrating PHP 5.x to PHP 7 on CentOS 7


In this article, we will learn about how to upgrade and update PHP 5.x to PHP 7, PHP 7 which was released in 2015 with speed improvements comparable to the older versions of the PHP.

Prerequisites

Assuming that we have already installed PHP 5.x on CentOS7, and mod_php module should be enabled by Apache, and we need Sudo privileges or root user.

Enabling the PHP 7 Repository

As PHP 7.x is not available in the official repositories, so we need to use IUS community Project Repository.

Download the IUS repository in the machine with the below command

# curl 'https://setup.ius.io/' -o setup-ius.sh
curl 'https://setup.ius.io/' -o setup-ius.sh
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1913 100 1913 0 0 1554 0 0:00:01 0:00:01 --:--:-- 1555


Installing the IUS repository to the local machine with the below command

# bash setup-ius.sh
bash setup-ius.sh
detected CentOS 7
Loaded plugins: fastestmirror
base | 3.6 kB
epel/x86_64/metalink | 5.2 kB
epel | 4.3 kB
extras | 3. 4 kB 00:00:00
mysql56-community | 2.5 kB 00:00:00
updates | 3.4 kB 00:00:00
(1/3): updates/7/x86_64/primary_db | 5.7 MB 00:00:00
(2/3): epel/x86_64/updateinfo | 584 kB 00:00:02
(3/3): epel/x86_64/primary_db | 4.3 MB 00:00:03
Loading mirror speeds from cached hostfile
* base: mirrors.nhanhoa.com
* epel: kodeterbuka.beritagar.id
* extras: mirrors.nhanhoa.com
* updates: mirrors.nhanhoa.com
Package epel-release-7-7.noarch already installed and latest version
Nothing to do
Loaded plugins: fastestmirror
ius-release.rpm | 8.1 kB 00:00:00
Examining /var/tmp/yum-root-BRDs50/ius-release.rpm: ius-release-1.0-14.ius.centos7.noarch
Marking /var/tmp/yum-root-BRDs50/ius-release.rpm to be installed
Resolving Dependencies
--> Running transaction check
---> Package ius-release.noarch 0:1.0-14.ius.centos7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=========================================================================================================================
Package Arch Version Repository Size=========================================================================================================================
Installing:
ius-release noarch 1.0-14.ius.centos7 /ius-release 8.
Transaction Summary
=========================================================================================================================
Install 1 Package
Total size: 8.5 k
Installed size: 8.5 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : ius-release-1.0-14.ius.centos7.noarch 1/1
Verifying : ius-release-1.0-14.ius.centos7.noarch 1/1
Installed:
ius-release.noarch 0:1.0-14.ius.centos7
Complete!

Upgrading the mod_php Modules in Apache

Here,we will upgrade the system for Apachhe module mod_php. First, we will remove the old packages using the below command.

# yum remove php-cli mod_php php-common

Installing the new PHP 7 modules using the IUS repository using the below command.

# yum install php70-mysqlnd mod_php70u php70u-cli
Loaded plugins: fastestmirror
ius | 2.2 kB 00:00:00
ius/x86_64/primary_db | 173 kB 00:00:00
Loading mirror speeds from cached hostfile
* base: mirrors.nhanhoa.com
* epel: kodeterbuka.beritagar.id
* extras: mirrors.nhanhoa.com
* ius: hkg.mirror.rackspace.com
* updates: mirrors.nhanhoa.com
Resolving Dependencies
--> Running transaction check
---> Package mod_php70u.x86_64 0:7.0.8-1.ius.centos7 will be installed
--> Processing Dependency: php-common(x86-64) = 7.0.8-1.ius.centos7 for package: mod_php70u-7.0.8-1.ius.centos7.x86_64
---> Package php70u-cli.x86_64 0:7.0.8-1.ius.centos7 will be installed
---> Package php70u-mysqlnd.x86_64 0:7.0.8-1.ius.centos7 will be installed
--> Processing Dependency: php-pdo(x86-64) = 7.0.8-1.ius.centos7 for package: php70u-mysqlnd-7.0.8-1.ius.centos7.x86_64
--> Running transaction check
---> Package php70u-common.x86_64 0:7.0.8-1.ius.centos7 will be installed
---> Package php70u-pdo.x86_64 0:7.0.8-1.ius.centos7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=========================================================================================================================
Package Arch Version Repository Size
=========================================================================================================================
Installing:
mod_php70u x86_64 7.0.8-1.ius.centos7 ius 2.6 M
php70u-cli x86_64 7.0.8-1.ius.centos7 ius 4.0 M
php70u-mysqlnd x86_64 7.0.8-1.ius.centos7 ius 239 k
Installing for dependencies:
php70u-common x86_64 7.0.8-1.ius.centos7 ius 1.1 M
php70u-pdo x86_64 7.0.8-1.ius.centos7 ius 126 k
Transaction Summary
=========================================================================================================================
Install 3 Packages (+2 Dependent packages)
Total download size: 8.0 M
Installed size: 30 M
Is this ok [y/d/N]: y
Downloading packages:
(1/5): php70u-pdo-7.0.8-1.ius.centos7.x86_64.rpm | 126 kB 00:00:00
(2/5): mod_php70u-7.0.8-1.ius.centos7.x86_64.rpm | 2.6 MB 00:00:01
(3/5): php70u-common-7.0.8-1.ius.centos7.x86_64.rpm | 1.1 MB 00:00:01
(4/5): php70u-mysqlnd-7.0.8-1.ius.centos7.x86_64.rpm | 239 kB 00:00:01
(5/5): php70u-cli-7.0.8-1.ius.centos7.x86_64.rpm | 4.0 MB 00:00:03
-------------------------------------------------------------------------------------------------------------------------
Total 2.2 MB/s | 8.0 MB 00:00:03
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : php70u-common-7.0.8-1.ius.centos7.x86_64 1/5
Installing : php70u-pdo-7.0.8-1.ius.centos7.x86_64 2/5
Installing : php70u-mysqlnd-7.0.8-1.ius.centos7.x86_64 3/5
Installing : php70u-cli-7.0.8-1.ius.centos7.x86_64 4/5
Installing : mod_php70u-7.0.8-1.ius.centos7.x86_64 5/5
Verifying : php70u-cli-7.0.8-1.ius.centos7.x86_64 1/5
Verifying : php70u-common-7.0.8-1.ius.centos7.x86_64 2/5
Verifying : mod_php70u-7.0.8-1.ius.centos7.x86_64 3/5
Verifying : php70u-pdo-7.0.8-1.ius.centos7.x86_64 4/5
Verifying : php70u-mysqlnd-7.0.8-1.ius.centos7.x86_64 5/5
Installed:
mod_php70u.x86_64 0:7.0.8-1.ius.centos7 php70u-cli.x86_64 0:7.0.8-1.ius.centos7
php70u-mysqlnd.x86_64 0:7.0.8-1.ius.centos7
Dependency Installed:
php70u-common.x86_64 0:7.0.8-1.ius.centos7 php70u-pdo.x86_64 0:7.0.8-1.ius.centos7
Complete!

Finally, we will restart the apache and check the status of the apache

# systemctl restart httpd

Checking the apache service status

# 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 2016-07-21 10:24:44 EDT; 46s ago
Docs: man:httpd(8)
man:apachectl(8)
Main PID: 3636 (httpd)
Status: "Total requests: 0; Current requests/sec: 0; Current traffic: 0 B/sec"
CGroup: /system.slice/httpd.service
├─3636 /usr/sbin/httpd -DFOREGROUND
├─3637 /usr/sbin/httpd -DFOREGROUND
├─3638 /usr/sbin/httpd -DFOREGROUND
├─3639 /usr/sbin/httpd -DFOREGROUND
├─3640 /usr/sbin/httpd -DFOREGROUND
└─3641 /usr/sbin/httpd -DFOREGROUND
Jul 21 10:24:43 centos-linux-1.shared systemd[1]: Starting The Apache HTTP Server...
Jul 21 10:24:44 centos-linux-1.shared systemd[1]: Started The Apache HTTP Server.

Testing the PHP Version

We can verify the PHP version by using a command line mode and we can also create an info.php and check the version of the PHP

# php -v
PHP 7.0.8 (cli) (built: Jun 23 2016 11:34:57) ( NTS )
Copyright (c) 1997-2016 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies

Creating the info.php file using the below code and check the info in GUI mode.

# /var/www/html/info.php
<?php
Phpinfo();
?>

After we create the file access the web browser with the below url

http://IP_Address /info.php

After we setup and configure the above steps we can now able to update the PHP 5.x to PHP 7 on Centos 7 / RHEL 7 as of now the Official repository for the CentOS 7 is not released. We can also see that PHP 7 upgraded on the server.

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 22-Jan-2020

346 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements