Replace PHP 5.4 with 5.6 in CentOS (or RedHat) 7.x
CentOS 7.x (and RedHat 7.x) ships with PHP 5.4, which does not allow you to run software like Drupal 8, Symfony 3, etc. This tutorial shows how to replace PHP 5.4 with Remi's PHP 5.6 packages.
Remi's PHP56 repository allows you to install PHP in 2 different ways:
- Software Collection: updated PHP packages are installed in /opt/remi and it doesn't replace your system's PHP. You decide which PHP module to load in Apache's configuration. RedHat has official Software Collections for PHP55 and PHP56 in their software channels.
- Replacement packages: Old PHP packages are uninstalled and replaced with the new packages. Updates for the new PHP packages will come from Remi's repository. RedHat does not offer "replacement packages" for PHP as of July-2016, only Remi provides them.
In our example server below, we will replace the current PHP version with Remi's PHP 5.6.
Update pending packages
Before installing PHP 5.6, apply any existing updates available for your distribution:
root@server: ~ # yum check-update root@server: ~ # yum update
Also, this is a good moment to get (and save somewhere) a list of our installed packages (in case we need to rollback):
root@server: ~/sources # rpm -qa |grep php php-mbstring-5.4.16-36.1.el7_2.1.x86_64 php-mysql-5.4.16-36.1.el7_2.1.x86_64 (...) php-gd-5.4.16-36.1.el7_2.1.x86_64 php-odbc-5.4.16-36.1.el7_2.1.x86_64
Download and Install Remi's repository files
Download and install the "repos" package:
root@server: ~ # wget http://rpms.famillecollet.com/enterprise/remi-release-7.rpm root@server: ~ # rpm -Uhv remi-release-7.rpm warning: remi-release-7.rpm: Header V4 DSA/SHA1 Signature, key ID 00f97f56: NOKEY Preparing... ################################# [100%] Updating / installing... 1:remi-release-7.2-1.el7.remi ################################# [100%]
Now enable the repository in Yum by setting enable=1
for the remi-php56
repo:
root@server: ~ # vim /etc/yum.repos.d/remi.repo (...) [remi-php56] name=Remi's PHP 5.6 RPM repository for Enterprise Linux 7 - $basearch mirrorlist=http://rpms.remirepo.net/enterprise/7/php56/mirror enabled=1 gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi (...)
Replace PHP 5.4 with PHP 5.6
Install the packages not as a Software Collection but as "Replacement Packages":
root@server: ~ # yum update
This yum update
command should upgrade all our PHP packages. If it does not upgrade them or we don't have PHP already installed, then do:
root@server: ~ # yum --enablerepo=remi-php56 install php-cli
Yum will remove the old packages and install the new ones. For me it was clean and simple, for other cases (lots of PHP packages or modules), it might require some manual work (uninstall some old package previously or similar).
We can get some PHP warnings like: PHP Warning: Module 'xmlwriter' already loaded in Unknown on line 0
or PHP Warning: PHP Startup: mbstring: Unable to initialize module
. This happens while the modules are being upgraded. You can ignore these warnings: the only module that will still providing errors/warnings when we restart Apache will be APC:
PHP Warning: PHP Startup: apc: Unable to initialize module
Disable APC and enable OPCACHE instead
APC does not work anymore with PHP >=5.5 (only the user-land cache APCu still works), so the best approach is now to use PHP's integrated OPCACHE.
# Disabling APC: root@server: ~ # mv /etc/php.d/apc.ini /etc/php.d/apc.ini.old.PHP54 # Enable OPCACHE in php.ini: root@server: ~ # tail -20 /etc/php.ini ; Initial opcache settings opcache.enable=1 opcache.enable_cli=1 opcache.fast_shutdown=1 ; Memory config opcache.memory_consumption=1024 opcache.interned_strings_buffer=16 opcache.max_accelerated_files=32531 opcache.max_file_size=0 ; FOR PRODUCTION (never check timestamps of php files): ;opcache.revalidate_freq=0 ;validate_timestamps=0 ; FOR DEVELOPMENT (check timestamp on each access): opcache.revalidate_freq=0 validate_timestamps=1
Now, restart Apache:
root@server: ~ # systemctl restart httpd
And enjoy your PHP 5.6:
root@server: ~ # php -v PHP 5.6.23 (cli) (built: Jun 22 2016 08:56:52) Copyright (c) 1997-2016 The PHP Group Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
If you need to install phpmyadmin at this point, you must use remi's package also:
root@server: ~ # yum --enablerepo=remi-php56 install phpMyAdmin