How To Upgrade PHP 5.6 to 7.1 in CentOS 7 Linux

In this post I’ll show you how to upgrade PHP 5.6 to 7.1 in CentOS 7 Linux. I performed this process today on this server and wanted to document the problems that I had along the way, along with relevant fixes.

Note: Before you upgrade PHP 5.6 to 7.1 I suggest running through this process on a production server, I cloned the VM to a test instance and tested there first. This step is critical, as I had many problems with PHP code that was not compatible with PHP 7.1 and other various problems. Basically you should perform the upgrade on a test server first, and check all website functionality while viewing php-fpm and web server specific error logs for problems. This way you can fix problems in the test instance and know what to expect when you do it for real.

How To Upgrade PHP 5.6 to 7.1

For PHP 5.6 I’ve been using the Webtatic repository. This is a third party repository that allows me to install newer versions of PHP in CentOS 7. After some research, I found out that the IUS repository is recommended and Webtatic is listed as a problematic repository by CentOS.

With that information in mind, I decided that when upgrading to PHP 7.1 I’d also change to the IUS repository.

Preparing for PHP Update

Before beginning we can see that we’re currently running PHP version 5.6.30.

[root@centos7 ~]# php -v
PHP 5.6.30 (cli) (built: Jan 19 2017 22:31:39)
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.

[root@centos7 ~]# yum list installed | grep -i webtatic
php56w.x86_64                     5.6.30-1.w7       @webtatic
php56w-cli.x86_64                 5.6.30-1.w7       @webtatic
php56w-common.x86_64              5.6.30-1.w7       @webtatic
php56w-fpm.x86_64                 5.6.30-1.w7       @webtatic
php56w-gd.x86_64                  5.6.30-1.w7       @webtatic
php56w-mbstring.x86_64            5.6.30-1.w7       @webtatic
php56w-mysqlnd.x86_64             5.6.30-1.w7       @webtatic
php56w-opcache.x86_64             5.6.30-1.w7       @webtatic
php56w-pdo.x86_64                 5.6.30-1.w7       @webtatic
php56w-pear.noarch                1:1.10.1-1.w7     @webtatic
php56w-pecl-igbinary.x86_64       2.0.1-1.w7        @webtatic
php56w-pecl-memcache.x86_64       3.0.8-2.w7        @webtatic
php56w-pecl-memcached.x86_64      2.2.0-1.w7        @webtatic
php56w-process.x86_64             5.6.30-1.w7       @webtatic
php56w-xml.x86_64                 5.6.30-1.w7       @webtatic
webtatic-release.noarch           7-3               installed

Note down all of these PHP packages, we’ll need them later.

Repository Change

Note that if you are sticking with either Webtatic or IUS that you don’t need to perform this step, this is just me changing my repository. If you’re already using Webtatic and not changing, or IUS and not changing, move onto the ‘Backup Configuration’ step below.

As mentioned I’m using Webtatic but I want to change to IUS, so I’ll start by first removing the Webtatic repositories. This can be done by removing the webtatic-release package, which controls various .repo files within the /etc/yum.repos.d/ directory.

yum remove webtatic-release -y

I then install the IUS repository by running the below command.

yum install https://centos7.iuscommunity.org/ius-release.rpm

This requires EPEL to install, which I already had installed.

Backup 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. Remember to keep note of these, as we’ll need that in the next step.

[root@centos7 ~]# yum remove php56w php56w-cli php56w-common php56w-fpm php56w-gd php56w-mbstring php56w-mysqlnd php56w-opcache php56w-pdo php56w-pear php56w-pecl-igbinary php56w-pecl-memcache php56w-pecl-memcached php56w-process php56w-xml

Install PHP 7.1

A ‘upgrade PHP 5.6 to 7.1’ guide would not be complete without actually installing 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, if you’re staying with Webtatic keep the ‘w’. 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.

yum install php71u php71u-cli php71u-common php71u-fpm php71u-gd php71u-mbstring php71u-mysqlnd php71u-opcache php71u-pdo php71u-pear php71u-pecl-igbinary php71u-pecl-memcache php71u-pecl-memcached php71u-process php71u-xml php71u-json -y

We can now see that we’re running PHP 7.1.

[root@centos7 ~]# php -v
PHP 7.1.1 (cli) (built: Jan 18 2017 13:45:30) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.1.1, Copyright (c) 1999-2017, by Zend Technologies

Note that by default php-fpm will not start up on system boot.

[root@centos7 ~]# systemctl is-enabled php-fpm
disabled

This is not ideal, so we enable php-fpm so that it starts up on system boot automatically.

[root@centos7 ~]# systemctl enable php-fpm
Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.s                                                                                                                        ervice.

Modify Configuration

Now that PHP 7.1 is installed, I go through the old /etc/php.ini and /etc/php-fpm.d/www.conf files that I backed up in the previous step and copy the settings that I want to the new files. For me personally, this mostly involved editing the /etc/php-fpm.d/www.conf file and commenting out the below:

;listen = 127.0.0.1:9000

I then added the following entries in, or modified them from existing values to these. Note these are specific to my Nginx configuration, yours may differ which is why it’s important to put in the same settings that you saved earlier.

user = nginx
group = nginx
listen = /var/run/php-fpm/php-fpm.sock
listen.owner = nginx
listen.group = nginx

Troubleshooting

Here are the solutions to some common problems that you may encounter when you upgrade PHP 5.6 to 7.1.

I also then needed to go into the /var/lib/php/ directory and chown everything to root:nginx with the command below, as this had changed to be owned by php-fpm which is not what I’m using. Without this step session files will not correctly write.

[root@centos7 ~]# cd /var/run/php/
[root@centos7 php]# chown -R root:nginx *

[root@centos7 php]# ls -la
total 40
drwxr-xr-x.  5 root root     44 Mar  5 00:54 .
drwxr-xr-x. 35 root root   4096 Mar  5 00:48 ..
drwxrwx---.  5 root nginx    50 Mar  5 00:48 fpm
drwxrwx---.  5 root nginx    50 Mar  5 00:48 mod_php
drwxrwx---+  2 root nginx 73728 Mar  5 00:48 session

Without this step, error logs in my Nginx configuration file as shown below were encountered. While these errors are specific to a particular WordPress plugin, it demonstrates what happens when sessions can’t be written.

2017/03/05 00:49:19 [error] 905#905: *752 FastCGI sent in stderr: "PHP message: PHP Warning:  session_start(): open(/var/lib/php/fpm/session/sess_abcd, O_RDWR) failed: Permission denied (13) in /var/www/website/bwp-recaptcha/includes/class-bwp-recaptcha.php on line 236

2017/03/05 00:49:19 [error] 905#905: *749 FastCGI sent in stderr: "PHP message: PHP Warning:  session_start(): open(/var/lib/php/fpm/session/sess_8cpi4cdjdcq824pmnirdcaujr1, O_RDWR) failed: Permission denied (13) in /var/www/nginx/bwp-recaptcha/includes/class-bwp-recaptcha.php on line 236
PHP message: PHP Warning:  session_start(): Failed to read session data: files (path: /var/lib/php/fpm/session) in /var/www/nginx/bwp-recaptcha/includes/class-bwp-recaptcha.php on line 236" while reading response header from upstream, client: x.x.x.x, server: domain.com, request: "POST /wp-comments-post.php HTTP/1.1", upstream: "fastcgi://unix:/var/run/php-fpm/php-fpm.sock:", host: "www.domain.com", referrer: "https://www.domain.com/"

I also installed the ‘php71u-json’ package despite not having the equivalent installed with PHP 5.6, as it was needed for the following error.

FastCGI sent in stderr: "PHP message: PHP Fatal error:  Uncaught Error: Call to undefined function W3TC\json_decode()

I’m still having an issue with the Jetpack plugin which may be a bug with PHP opcache, as I found documented here: https://translate.google.com.au/translate?hl=en&sl=ja&u=https://jpskenn.homeip.net/2017/01/27/entry_18247/

2017/03/05 01:15:16 [error] 1688#1688: *61 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: x.x.x.x, server: domain.com, request: "POST /xmlrpc.php?for=jetpack&token=... HTTP/1.1", upstream: "fastcgi://unix:/var/run/php-fpm/php-fpm.sock:", host: "www.domain.com", referrer: "https://www.domain.com/xmlrpc.php?for=jetpack&token=..."

This appears in my Nginx error log file when I load the WordPress dashboard, it seems to be a POST to xmlrpc from Jetpack. At the time time this happens, I see the following in my php-fpm error log file.

[05-Mar-2017 01:15:16] WARNING: [pool www] child 1670 exited on signal 11 (SIGSEGV) after 163.427669 seconds from start
[05-Mar-2017 01:15:16] NOTICE: [pool www] child 1727 started

While not ideal I have been able to work around the issue by editing xmlrpc.php and adding the below entry.

ini_set ('opcache.enable', 0);

I haven’t been able to replicate this in a test instance, I’m trying to reproduce it before I report it as a PHP bug. Regardless it doesn’t seem to be causing me any major problems, everything else is otherwise working.

Summary

As shown we have shown you how to upgrade PHP 5.6 to 7.1 successfully. We changed the Webtatic repository to IUS, backed up our PHP specific configuration, removed old packages, and finally installed new packages.

  1. Just a quick update: php71u-pear is now pear1u

  2. thanks for sharing. I have been getting FastCGI sent in stderr Error and managed to solve it by installing all the recommended php7.1 extensions

  3. This has been thus far the best upgrade document that I have used. It was clear, concise, and error free. I obviously made adjustments for upgrade on RHEL6 but thank you!

  4. thank you, it works on mine.

  5. Worked perfectly, thank you.

  6. While there is no automated way of PHP migration, there are tools that can be used to make the process easier. You should also mention about these tools in your article. For example, there is PHP 7 Mar that can be used to find deprecations. There is also Phinx that can be helpful in migrating MySQL database.

    Source: https://www.cloudways.com/blog/migrate-php-and-mysql/

    • Thanks for sharing, they weren’t mentioned as I’d never heard of them and don’t currently have personal experience using any such tools, perhaps I can look at using them for a future upgrade.

Leave a Comment

NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>