Migrating PHP 5.x to PHP 7 on CentOS 7

In this article, we will learn how to upgrade PHP 5.x to PHP 7 on CentOS 7. PHP 7, released in 2015, offers significant performance improvements over previous PHP versions.

Prerequisites

You need PHP 5.x already installed on CentOS 7 with mod_php enabled in Apache, plus sudo privileges or root access.

Enabling the PHP 7 Repository

Since PHP 7.x is not available in official CentOS repositories, we'll use the IUS Community Project Repository.

Download the IUS repository setup script −

# 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

Install the IUS repository −

# bash setup-ius.sh
bash setup-ius.sh
detected CentOS 7
Loaded plugins: fastestmirror
base                                                     | 3.6 kB  00:00:00
epel/x86_64/metalink                                     | 5.2 kB  00:00:00
epel                                                     | 4.3 kB  00:00:00
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.5 k

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 Apache mod_php Module

Remove the old PHP 5.x packages −

# yum remove php-cli mod_php php-common

Install PHP 7 modules from the IUS repository −

# yum install php70u-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!

Restart Apache and verify its status −

# systemctl restart httpd
# 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

Verify the PHP version from command line −

# 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

Create a test file to verify PHP 7 in the browser −

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

Access the file via browser −

http://IP_Address/info.php

PHP 7 Info Page

Conclusion

You have successfully upgraded from PHP 5.x to PHP 7 on CentOS 7 using the IUS repository. This upgrade provides significant performance improvements and new language features while maintaining compatibility with most existing applications.

Updated on: 2026-03-15T08:29:00+05:30

545 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements