Update Centos 7 to PHP 7 via Remi repos
On 03.12.2015 has been released PHP 7.0 with great performance & feature improvements.
Since I have a couple of applications running on PHP 5.5 branch with quite complicate environment settings I was wondering whether there is an easy way to simply upgrade to PHP 7 without the need to do too much of manual uninstall/install stuff. I was actually surprised how easy things went on. So here is in short steps to upgrade existing PHP installation on Centos (applicable probably also to Fedora & Red Hat).
Following is assumed (or at least applied to my setup):
- the installation was executed via yum manager. There was no manual PHP compilation, no custom settings.
- the original PHP 5.5.X branch was previously installed from REMI repositories for PHP 5.5
- the new PHP 7.4.X will be installed from REMI repositories for PHP 7.4
- we use PHP-FPM running on NGINX via unix socket
Steps to upgrade to PHP 7
First let's check installed PHP packages from remi PHP5 repository (just informative check):
yum list installed | grep remi
Remove all existing PHP packages:
yum remove php-*
Now php-fpm service is fully removed including dependencies, so there should be no conflicts with new branch. Now let's install PHP 7.4. We will use remi repository again via option --enablerepo=remi,remi-php74. Following is a single line:
yum --enablerepo=remi,remi-php74 install php-fpm php-common php-cli php-pear php-pdo php-mysqlnd php-gd php-mbstring php-mcrypt php-xml php-iconv php-soap php-opcache php-imagick php-tidy php-json php-openssl php-bcmath
Following steps were applicable to PHP 7.0 - 7.1. I found that for PHP 7.4 it is resolved automatically by yum. Just in case, install ZIP with following steps:
If ZIP extension will not install, it can be installed from PECL repo - check the version:
yum --enablerepo=remi,remi-php74 info php-*zip*
And install:
yum --enablerepo=remi,remi-php74 install php-pecl-zip
THAT'S IT. Now PHP 7.4 is installed, let's do some after work:
Start/restart the service ..
systemctl restart php-fpm
.. and verify version - you should see PHP 7.4.X via command line CLI:
php -v
.. or check where it is installed:
whereis php
Since php-fpm is newly installed service, ensure autostart:
sudo systemctl enable php-fpm
Check in nginx configuration, e.g. update path to unix socket in nginx/conf.d/default.conf or in any of your virtual host file(s):
fastcgi_pass unix:/tmp/php7-fpm.sock;
Now navigating to webserver root URL (e.g. http://localhost) should display some HTML page but .... whooops, something went wrong and I see "502 Bad Gateway" error. This is pretty common access error meaning that nginx and php-fpm run under different owners and/or groups and therefore cannot talk to each other (via unix socket). Fix it:
in file /etc/nginx/nginx.conf add first line:
user nginx;
in file /etc/php-fpm.d/www.conf comment out line:
; listen = 127.0.0.1:9000
and add lines:
listen = /tmp/php7-fpm.sock
listen.owner = nginx
listen.group = nginx
So now both services nginx and php-fpm will run under owner nginx so they both can access socket file in /tmp/php7-fpm.sock and the 502 Bad Gateway error should disappear.
Conclusion
Upgrading from PHP 5.X to PHP 7.X is quite easy. Steps above actually apply to any PHP 7.X version. Remi Collet, the maintainer of the RPM repositories, does awesome job - many thanx for it. Repos are always up-to-date, I observed regular updates just a few days with each new PHP release. Moving to PHP 7.X - the most secure, performant and feature-richest PHP version should be a breeze - Go for it!