How to Upgrade from PHP 5.6 to 7 on CentOS 6/7

16571
Share:
how-to-upgrade-from-php-5-6-to-7-on-centos-6-7

Update 25-12-2020: PHP 8 installation on Linux Mint 20 Ulyana or Ubuntu 20.04 Focal Fossa is available.

PHP 7

When it comes on choosing software version to be used, I always use two approach. On my daily desktop computer I'll run the latest version available, meanwhile on my production servers I'll run older version. The reason why I use an older version on my production servers is whenever I found a bug or an issue, there's chance some other persons found it already. There are probably already have some workarounds too.

This is also the case of PHP running on my production servers, which still on version 5.6. Recently I've checked PHP's website and figured out that PHP 5.6 will EOL in the end of 2018 (that's about two months from now), I guess now is the time to upgrade my PHP version.

I’ve been using the Webtatic repository for most of my servers to install PHP 5.6. If you haven't noticed, this is a third party repository that allows us to install alternate versions of PHP in CentOS 6/7. After some research, I found out that Webtatic is listed as a problematic repository by CentOS and there's an alternative repositories:

  • SCL (PHP 7.0)
  • IUS (PHP 7.1)
  • REMI (PHP 7.2)

If you're like me but not sure how to get this done, then I believe this post could help you (at least you have an idea). I will use PHP 7.1 as an example, but you could use another mirror to match your PHP version you would like to use.

PHP Milestone

How To Upgrade PHP 5.6 to 7.1

First step is to make sure we're using PHP 5.6

$ php -v
PHP 5.6.37 (cli) (built: Jul 20 2018 18:40:47)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2016, by Zend Technologies

Now let’s get an idea of the packages that we need to upgrade. I’ll run "yum list installed" then grep for Webtatic so that we can see all packages installed from this repository.

$ sudo yum list installed | grep -i webtatic
libmemcached10.x86_64              1.0.16-1.ius.el6                    @webtatic
php56w-cli.x86_64                  5.6.37-1.w6                         @webtatic
php56w-common.x86_64               5.6.37-1.w6                         @webtatic
php56w-fpm.x86_64                  5.6.37-1.w6                         @webtatic
php56w-gd.x86_64                   5.6.37-1.w6                         @webtatic
php56w-mbstring.x86_64             5.6.37-1.w6                         @webtatic
php56w-mysqlnd.x86_64              5.6.37-1.w6                         @webtatic
php56w-opcache.x86_64              5.6.37-1.w6                         @webtatic
php56w-pdo.x86_64                  5.6.37-1.w6                         @webtatic
php56w-pear.noarch                 1:1.10.4-1.w6                       @webtatic
php56w-pecl-igbinary.x86_64        2.0.5-1.w6                          @webtatic
php56w-pecl-imagick.x86_64         3.4.3-1.w6                          @webtatic
php56w-pecl-memcached.x86_64       2.2.0-2.w6                          @webtatic
php56w-process.x86_64              5.6.37-1.w6                         @webtatic
php56w-xml.x86_64                  5.6.37-1.w6                         @webtatic
webtatic-release.noarch            6-9                                 @webtatic

It's good idea to copy those output and store it to a text file.

$ sudo yum list installed | grep -i webtatic > installed_php_webtatic.txt

Change yum Repository

I want to remove Webtatic repository and change it with IUS repository. Here's what I do to remove Webtatic repository:

$ sudo yum remove webtatic-release -y

It will output like this:

Loaded plugins: fastestmirror
Setting up Remove Process
Resolving Dependencies
--> Running transaction check
---> Package webtatic-release.noarch 0:6-9 will be erased
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package                  Arch           Version        Repository         Size
================================================================================
Removing:
 webtatic-release         noarch         6-9            @webtatic          23 k

Transaction Summary
================================================================================
Remove        1 Package(s)

Installed size: 23 k
Downloading Packages:
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Erasing    : webtatic-release-6-9.noarch                                  1/1
  Verifying  : webtatic-release-6-9.noarch                                  1/1

Removed:
  webtatic-release.noarch 0:6-9                                                

Complete!

Next step is install IUS repository:

$ sudo yum install https://centos6.iuscommunity.org/ius-release.rpm

The output will be like this:

... snipped
Dependencies Resolved

=======================================================================================
 Package            Arch          Version                    Repository           Size
=======================================================================================
Installing:
 ius-release        noarch        1.0-15.ius.centos6         /ius-release        8.5 k

Transaction Summary
=======================================================================================
Install       1 Package(s)

Total size: 8.5 k
Installed size: 8.5 k
Is this ok [y/N]: y
Downloading Packages:
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : ius-release-1.0-15.ius.centos6.noarch                               1/1
  Verifying  : ius-release-1.0-15.ius.centos6.noarch                               1/1

Installed:
  ius-release.noarch 0:1.0-15.ius.centos6                                             

Complete!

Backup Existing PHP Configuration

Before we go ahead and remove the older version of PHP, keep in mind that installing new packages also means new configuration files. I suggest taking a copy of PHP specific configuration files, such as /etc/php.ini and /etc/php-fpm.d/www.conf.

Remove Old PHP Packages

While you can use something like "yum replace" for this, I just used "yum remove" to remove all of the previously listed PHP packages (I saved the list on installed_php_webtatic.txt).

$ sudo yum remove php56w-cli php56w-common php56w-fpm php56w-gd php56w-mbstring php56w-mysqlnd php56w-opcache php56w-pdo php56w-pear php56w-pecl-igbinary php56w-pecl-imagick php56w-pecl-memcached php56w-process php56w-xml

The output will be similar with:

Loaded plugins: fastestmirror
Setting up Remove Process
Resolving Dependencies
--> Running transaction check
---> Package php56w-cli.x86_64 0:5.6.37-1.w6 will be erased
---> Package php56w-common.x86_64 0:5.6.37-1.w6 will be erased
---> Package php56w-fpm.x86_64 0:5.6.37-1.w6 will be erased
---> Package php56w-gd.x86_64 0:5.6.37-1.w6 will be erased
---> Package php56w-mbstring.x86_64 0:5.6.37-1.w6 will be erased
---> Package php56w-mysqlnd.x86_64 0:5.6.37-1.w6 will be erased
---> Package php56w-opcache.x86_64 0:5.6.37-1.w6 will be erased
---> Package php56w-pdo.x86_64 0:5.6.37-1.w6 will be erased
---> Package php56w-pear.noarch 1:1.10.4-1.w6 will be erased
---> Package php56w-pecl-igbinary.x86_64 0:2.0.5-1.w6 will be erased
---> Package php56w-pecl-imagick.x86_64 0:3.4.3-1.w6 will be erased
---> Package php56w-pecl-memcached.x86_64 0:2.2.0-2.w6 will be erased
---> Package php56w-process.x86_64 0:5.6.37-1.w6 will be erased
---> Package php56w-xml.x86_64 0:5.6.37-1.w6 will be erased
--> Finished Dependency Resolution

Dependencies Resolved

=======================================================================================
 Package                     Arch         Version                Repository       Size
=======================================================================================
Removing:
 php56w-cli                  x86_64       5.6.37-1.w6            @webtatic       7.7 M
 php56w-common               x86_64       5.6.37-1.w6            @webtatic       8.4 M
 php56w-fpm                  x86_64       5.6.37-1.w6            @webtatic       3.9 M
 php56w-gd                   x86_64       5.6.37-1.w6            @webtatic       770 k
 php56w-mbstring             x86_64       5.6.37-1.w6            @webtatic       2.7 M
 php56w-mysqlnd              x86_64       5.6.37-1.w6            @webtatic       984 k
 php56w-opcache              x86_64       5.6.37-1.w6            @webtatic       300 k
 php56w-pdo                  x86_64       5.6.37-1.w6            @webtatic       351 k
 php56w-pear                 noarch       1:1.10.4-1.w6          @webtatic       2.1 M
 php56w-pecl-igbinary        x86_64       2.0.5-1.w6             @webtatic       113 k
 php56w-pecl-imagick         x86_64       3.4.3-1.w6             @webtatic       820 k
 php56w-pecl-memcached       x86_64       2.2.0-2.w6             @webtatic       314 k
 php56w-process              x86_64       5.6.37-1.w6            @webtatic       129 k
 php56w-xml                  x86_64       5.6.37-1.w6            @webtatic       641 k

Transaction Summary
=======================================================================================
Remove       14 Package(s)

Installed size: 29 M
Is this ok [y/N]: y

Just press "y" and "Enter". Package removal will be continued. Wait until the process finished.

Install PHP 7.1

Now with the list of PHP packages that we previously removed, we simply copy them and edit them a bit. The 'w' on the end of the package means it’s Webtatic. As I’m changing to IUS, PHP packages end with the letter 'u'. Based on this, I use yum to install these same PHP packages except version 7.1 rather than 5.6, that is 71 instead of 56. For example, package php56w would become php71u. I just edit those package list in a text editor and do "search and replace".

$ sudo yum install php71u-cli php71u-common php71u-fpm php71u-gd php71u-mbstring php71u-mysqlnd php71u-opcache php71u-pdo php71u-pear php71u-pecl-igbinary php71u-pecl-imagick php71u-pecl-memcached php71u-process php71u-xml

The response is like this:

Loaded plugins: fastestmirror
Setting up Install Process
Loading mirror speeds from cached hostfile
 * base: mirror.hostduplex.com
 * epel: mirrors.develooper.com
 * extras: mirror.hostduplex.com
 * ius: mirrors.kernel.org
 * updates: repos.lax.quadranet.com
ius                                                             | 2.3 kB     00:00    
ius/primary_db                                                  | 339 kB     00:00    
No package php71u-pear available.
No package php71u-pecl-memcached available.
Resolving Dependencies
--> Running transaction check
---> Package php71u-cli.x86_64 0:7.1.22-1.ius.centos6 will be installed
---> Package php71u-common.x86_64 0:7.1.22-1.ius.centos6 will be installed
---> Package php71u-fpm.x86_64 0:7.1.22-1.ius.centos6 will be installed
---> Package php71u-gd.x86_64 0:7.1.22-1.ius.centos6 will be installed
--> Processing Dependency: libwebp.so.5()(64bit) for package: php71u-gd-7.1.22-1.ius.centos6.x86_64
---> Package php71u-mbstring.x86_64 0:7.1.22-1.ius.centos6 will be installed
---> Package php71u-mysqlnd.x86_64 0:7.1.22-1.ius.centos6 will be installed
---> Package php71u-opcache.x86_64 0:7.1.22-1.ius.centos6 will be installed
---> Package php71u-pdo.x86_64 0:7.1.22-1.ius.centos6 will be installed
---> Package php71u-pecl-igbinary.x86_64 0:2.0.5-2.ius.centos6 will be installed
---> Package php71u-pecl-imagick.x86_64 0:3.4.3-2.ius.centos6 will be installed
---> Package php71u-process.x86_64 0:7.1.22-1.ius.centos6 will be installed
---> Package php71u-xml.x86_64 0:7.1.22-1.ius.centos6 will be installed
--> Running transaction check
---> Package libwebp.x86_64 0:0.4.3-3.el6 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=======================================================================================
 Package                    Arch         Version                      Repository  Size
=======================================================================================
Installing:
 php71u-cli                 x86_64       7.1.22-1.ius.centos6         ius        4.1 M
 php71u-common              x86_64       7.1.22-1.ius.centos6         ius        1.1 M
 php71u-fpm                 x86_64       7.1.22-1.ius.centos6         ius        1.4 M
 php71u-gd                  x86_64       7.1.22-1.ius.centos6         ius        165 k
 php71u-mbstring            x86_64       7.1.22-1.ius.centos6         ius        989 k
 php71u-mysqlnd             x86_64       7.1.22-1.ius.centos6         ius        217 k
 php71u-opcache             x86_64       7.1.22-1.ius.centos6         ius        234 k
 php71u-pdo                 x86_64       7.1.22-1.ius.centos6         ius        118 k
 php71u-pecl-igbinary       x86_64       2.0.5-2.ius.centos6          ius         44 k
 php71u-pecl-imagick        x86_64       3.4.3-2.ius.centos6          ius        144 k
 php71u-process             x86_64       7.1.22-1.ius.centos6         ius         73 k
 php71u-xml                 x86_64       7.1.22-1.ius.centos6         ius        186 k
Installing for dependencies:
 libwebp                    x86_64       0.4.3-3.el6                  epel       210 k

Transaction Summary
=======================================================================================
Install      13 Package(s)

Total download size: 8.9 M
Installed size: 32 M
Is this ok [y/N]: y

Again, press "y" followed by "Enter" will continue installation process. If you notice the output, there are some packages not available: php71u-pear and php71u-pecl-memcached. We'll fix this later.

Copy Configuration Files

This is just how "quick and dirty" approach to get PHP 7.1 running quickly and minimize downtime.

Overwrite /etc/php.ini and /etc/php-fpm.d/www.conf with our backup files.

Run php-fpm Service

To check whether php-fpm service is running on Centos 6:

$ sudo service php-fpm status

It may respond with this line:

php-fpm is stopped

It means the php-fpm service is stopped and we need to run it. On your terminal, supply this command:

$ sudo service php-fpm start

Verify Installation

To verify whether installation is successfull or not, run this command:

$ php -v
PHP 7.1.22 (cli) (built: Sep 13 2018 16:52:37) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.1.22, Copyright (c) 1999-2018, by Zend Technologies

If it reads PHP 7.1.*, then congratulations, we have successfully installed PHP 7.1 on our CentOS 6/7 system.

Test Web Page

When I opened my web page, it responded with error 500. Consulting nginx's error log, it says something like this "PHP message: PHP Fatal error:  Interface 'JsonSerializable' ..."

I believe it's just missing PHP JSON module, so I just install php71u-json like this:

$ sudo yum install php71u-json

Restart php-fpm service and my web page is running again.

Final Words

I hope that you now know how to upgrade PHP 5.6 to 7.1 on CentOS 6/7. If you run into any issues or have any feedback feel free to drop a comment below.

 

 

Tags PHP Linux
Share:

2 comments

  1. hi, i heve php 7.0 installed, i want to update to php 7.1, can i use this way for that?

  2. Hi, i did all instructions but when i run phpinfo(), it shows only text, php , i think apache no detect php. when i run php -v, the output is ok, PHP 7.1.30 (cli) (built: May 31 2019 00:55:18) ( NTS )
    Copyright (c) 1997-2018 The PHP Group
    Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies
    with Zend OPcache v7.1.30, Copyright (c) 1999-2018, by Zend Technologies

    any idea to fix it? all services are running [OK]

    • It seems that Apache don't understand how to handle .php file. Make sure you have something like this on your apache2.conf: ​

      <FilesMatch \.php$>
      ​SetHandler application/x-httpd-php
      ​</FilesMatch>

Leave a reply

Your email address will not be published. Required fields are marked *