Howto Install PHP 5.4, 5.5 or 5.6 on CentOS 6 and CentOS 7

PHP on CentOS

I still use CentOS as my distro of choice on any new server and whilst things are getting better CentOS and RHEL still don’t have up to date versions of PHP available inside the default yum repos. A couple of years ago I posted about Howto install PHP Mcrypt on CentOS using EPEL which is one of my sites most popular posts!

So CentOS and PHP, as you can see below (taken from DistroWatch) CentOS is a bit outdated with it’s PHP versions which as a PHP developer can be a pain in the ass!

PHP Current Version CentOS 7.0-1406 CentOS 6.6
5.6.4 5.4.16 5.3.3

However there is yet again hope! This time we need 2 additional repos. First up we need EPEL and secondly we need Remi a French guy who is king of PHP repos!

Repo Installation

Open up a SSH connection to your server and run the following commands (make sure you run as sudo if you need to):

For CentOS 7 (including EPEL install)

wget http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm
wget http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
rpm -Uvh remi-release-7*.rpm epel-release-7*.rpm

If you already have EPEL installed:

wget http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
rpm -Uvh remi-release-7*.rpm

For CentOS 6 (including EPEL install)

wget http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
wget http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
rpm -Uvh remi-release-6*.rpm epel-release-6*.rpm

If you already have EPEL installed:

wget http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
rpm -Uvh remi-release-6*.rpm

Enabling the Repo

Now we need to make sure the repo is enabled and select which version you want to install. We need to head over to /etc/yum.repos.d you should inside see a file called remi.repo.

Open the file in your favourite editor (Nano, Pico, Vi etc), you’ll see a number of sections. We need to make sure that the first section [remi] is enabled:

[remi]
name=Les RPM de remi pour Enterprise Linux 6 - $basearch
#baseurl=http://rpms.famillecollet.com/enterprise/6/remi/$basearch/
mirrorlist=http://rpms.famillecollet.com/enterprise/6/remi/mirror
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi

Note the line enabled=1 make sure this is set! Now technically you can actually go ahead and install PHP, but you will only get PHP 5.4.*. Which might be want to you want is so skip ahead to the next section!

If we want PHP 5.5 or PHP 5.6 we need to do a bit more work, further down in the repo.repo file you will see two additional sections [remi-php55] and [remi-php56], decide which PHP version you want to install and then enable the correct. So for PHP 5.6 we would change to:

[remi-php56]
name=Les RPM de remi de PHP 5.6 pour Enterprise Linux 6 - $basearch
#baseurl=http://rpms.famillecollet.com/enterprise/6/php56/$basearch/
mirrorlist=http://rpms.famillecollet.com/enterprise/6/php56/mirror
# WARNING: If you enable this repository, you must also enable "remi"
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi

Once you made your changes save your modified file and quit your editor.

Installing PHP

Now I’m assuming you don’t already have PHP installed, this bit is super simple.

sudo yum install php php-gd php-mysql php-mcrypt

So the above assumes you want MySQL, GD and Mcrypt support in your PHP, but you should see something like the below depending on which version of PHP you are trying to install:

================================================================================================================================
 Package                        Arch                   Version                                 Repository                  Size
================================================================================================================================
Installing:
 php                            x86_64                 5.5.20-2.el6.remi                       remi-php55                 2.6 M
 php-gd                         x86_64                 5.5.20-2.el6.remi                       remi-php55                  72 k
 php-mysqlnd                    x86_64                 5.5.20-2.el6.remi                       remi-php55                 3.6 M
Installing for dependencies:
 php-cli                        x86_64                 5.5.20-2.el6.remi                       remi-php55                 3.7 M
 php-common                     x86_64                 5.5.20-2.el6.remi                       remi-php55                 1.0 M
 php-pdo                        x86_64                 5.5.20-2.el6.remi                       remi-php55                 112 k
 php-pear                       noarch                 1:1.9.5-3.el6.remi                      remi                       375 k
 php-pecl-jsonc                 x86_64                 1.3.6-1.el6.remi.5.5.1                  remi-php55                  47 k
 php-pecl-zip                   x86_64                 1.12.4-1.el6.remi.5.5                   remi-php55                 269 k
 php-process                    x86_64                 5.5.20-2.el6.remi                       remi-php55                  57 k
 php-xml                        x86_64                 5.5.20-2.el6.remi                       remi-php55                 208 k

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

As you can see PHP is installing version 5.5.20-2.el6.remi from the remi-php55 repo! Once you have hit Y to confirm the install restart apache and magical unicorns you have a better version of PHP!

You can also change your mind in the future by going back into the remi.repo file and enable a different PHP version and then run yum update and if you have moved from 5.5 to 5.6 it will upgrade PHP for you. If you want to downgrade for any reason you will need to remove PHP (sudo yum remove php*) and then reinstall the PHP modules you want.

Hope this helps!

digital-ocean

40 comments

  1. I’m facing this issue in installation..

    Loaded plugins: product-id, refresh-packagekit, rhnplugin, security, subscription-manager
    This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
    There was an error communicating with RHN.
    RHN Satellite or RHN Classic support will be disabled.
    Error communicating with server. The message was:
    Temporary failure in name resolution
    Could not retrieve mirrorlist http://cdn.remirepo.net/enterprise/6/php56/mirror error was
    14: PYCURL ERROR 6 – “Couldn’t resolve host ‘cdn.remirepo.net'”
    Error: Cannot retrieve repository metadata (repomd.xml) for repository: remi-php56. Please verify its path and try again

  2. DOES NOT WORK

    updates | 3.4 kB 00:00
    No package php available.
    No package php-gd available.
    No package php-mysql available.
    No package php-mcrypt available.
    Error: Nothing to do
    root@tra [/usr/local/src]#

  3. thanks for document , i have using php 5.6 with apache 2.4 , i need to install mysqli extension , i have done with below commands
    $ cd extname
    $ phpize
    $ ./configure
    $ make
    # make install# its seem installed correctly
    and also i have added path in php.ini , but still its not install mysqli , php -i | grep mysqli is geven mysqli with details, plz help me to do correctly .

  4. my system in centos 6.7 installed and I want update php5.3.3 to 5.4 or above but not update how to solve my problem

  5. Thanks for the great post. I used your post to upgrade to php 5.4 on a CentOS 6 system. Unfortunately it broke my installation of mod_suphp. It doesn’t even seem to be using suphp anymore. If I change the file permissions on my index.php to 777 it works and shows the user as “apache”. If I change the permissions to 711 (as they should be for suphp) I get an error that the file can’t be read due to permission errors.

    Any ideas how I can get suphp running again?

  6. Thanks for your blog I manage to install php, php-mysql and php-mcrypt but i failed in php-gd there is an error message.

    [root@localhost ~]# yum install php-gd
    Loaded plugins: langpacks, product-id, subscription-manager
    This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
    Resolving Dependencies
    –> Running transaction check
    —> Package php-gd.x86_64 0:5.6.17-1.el7.remi will be installed
    –> Processing Dependency: gd-last(x86-64) >= 2.1.1 for package: php-gd-5.6.17-1.el7.remi.x86_64
    –> Processing Dependency: libt1.so.5()(64bit) for package: php-gd-5.6.17-1.el7.remi.x86_64
    –> Processing Dependency: libgd.so.3()(64bit) for package: php-gd-5.6.17-1.el7.remi.x86_64
    –> Running transaction check
    —> Package gd-last.x86_64 0:2.1.1-2.el7.remi will be installed
    —> Package php-gd.x86_64 0:5.6.17-1.el7.remi will be installed
    –> Processing Dependency: libt1.so.5()(64bit) for package: php-gd-5.6.17-1.el7.remi.x86_64
    –> Finished Dependency Resolution
    Error: Package: php-gd-5.6.17-1.el7.remi.x86_64 (remi-php56)
    Requires: libt1.so.5()(64bit)
    You could try using –skip-broken to work around the problem
    You could try running: rpm -Va –nofiles –nodigest
    [root@localhost ~]#

    1. Firstly it looks like you’re using RHEL not CentOS which I haven’t tested these instructions against. However the error is Requires: libt1.so.5()(64bit) I would start there as you look to be missing that as a dependency.

  7. Thanks for this guide. However I get stuck at the last bit:

    [root@hosted-by yum.repos.d]# sudo yum install php php-gd php-mysql php-mcrypt
    Loaded plugins: fastestmirror
    Setting up Install Process
    Loading mirror speeds from cached hostfile
    * base: centos.mirror.transip.nl
    * epel: mirror.nl.leaseweb.net
    * extras: centos.mirror.transip.nl
    * remi: rpms.remirepo.net
    * remi-php56: rpms.remirepo.net
    * remi-safe: rpms.remirepo.net
    * updates: centos.mirror.transip.nl
    * webtatic: uk.repo.webtatic.com
    remi | 2.9 kB 00:00
    remi-php56 | 2.9 kB 00:00
    No package php available.
    No package php-gd available.
    No package php-mysql available.
    No package php-mcrypt available.
    Error: Nothing to do

  8. Hi, thanks for the article!
    Unfortunately i can’t update, because once i type the last command (sudo yum install or sudo yum update) i get the following error:

    Error: Package: roundcubemail-ovh-0.9.5-1.el6.noarch (@/roundcubemail-ovh-0.9.5-1.el6.noarch)
               Requires: php-pecl-Fileinfo
               Removing: php-common-5.3.3-27.el6.2.x86_64 (@LocalRepo)
                   php-pecl-Fileinfo = 1.0.5-dev
               Updated By: php-common-5.5.29-1.el6.remi.x86_64 (remi-php55)
                   Not found
               Available: php-common-5.4.44-1.el6.remi.x86_64 (remi)
                   php-pecl-Fileinfo = 1.0.5
               Available: php-common-5.4.45-1.el6.remi.x86_64 (remi)
                   php-pecl-Fileinfo = 1.0.5
               Available: php-common-5.5.28-1.el6.remi.x86_64 (remi-php55)
                   Not found
    Error: Package: php-pecl-apc-3.1.9-2.el6.x86_64 (@base)
               Requires: php(zend-abi) = 20090626
               Removing: php-common-5.3.3-27.el6.2.x86_64 (@LocalRepo)
                   php(zend-abi) = 20090626
               Updated By: php-common-5.5.29-1.el6.remi.x86_64 (remi-php55)
                   php(zend-abi) = 20121212-64
               Available: php-common-5.4.44-1.el6.remi.x86_64 (remi)
                   php(zend-abi) = 20100525-x86-64
               Available: php-common-5.4.45-1.el6.remi.x86_64 (remi)
                   php(zend-abi) = 20100525-x86-64
               Available: php-common-5.5.28-1.el6.remi.x86_64 (remi-php55)
                   php(zend-abi) = 20121212-64
    Error: Package: php-pecl-apc-3.1.9-2.el6.x86_64 (@base)
               Requires: php(api) = 20090626
               Removing: php-common-5.3.3-27.el6.2.x86_64 (@LocalRepo)
                   php(api) = 20090626
               Updated By: php-common-5.5.29-1.el6.remi.x86_64 (remi-php55)
                   php(api) = 20121113-64
               Available: php-common-5.4.44-1.el6.remi.x86_64 (remi)
                   php(api) = 20100412-x86-64
               Available: php-common-5.4.45-1.el6.remi.x86_64 (remi)
                   php(api) = 20100412-x86-64
               Available: php-common-5.5.28-1.el6.remi.x86_64 (remi-php55)
                   php(api) = 20121113-64
     You could try using --skip-broken to work around the problem
     You could try running: rpm -Va --nofiles --nodigest
    
    

    Could you please help me fix it?
    Thanks 🙂

    1. It looks like Roundcube is the problem here it’s got some dependencies that can’t be fixed using newer versions of PHP. I would make sure you update your Roundcube installation first. Remove the repo added in this article, Update Roundcube and then try the steps again to get PHP up to date. Might be worth heading over to the Roundcube site to see if anyone else has had issues with PHP5.4+

  9. This was really helpful. I’m pretty sure this information doesn’t exist in one place like this anywhere else on the Internet!

    I did have one issue, which confuses me. All the installs went just as described on my CENTOS 7 Vagrant image as localhost. However after I started apache and add a basic `phpinfo` file in the DocumentRoot, the php code isn’t parsed and the contents (source) of the php file are just outputted to my user agent.

    Interestingly when I installed php 5.4 using:
    `sudu yum install php`
    …I did NOT have this problem. (however my app requires 5.6)

    On the command line I see php is installed:

    {{{
    [vagrant@localhost ~]$ php -v
    PHP 5.6.12 (cli) (built: Aug 6 2015 17:06:46)
    Copyright (c) 1997-2015 The PHP Group
    Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies
    [vagrant@localhost ~]$
    }}}

  10. On Centos 6.6 at least in my case usign Plesk 12 better not use the yum install php it will causes the plesk entire crash, that happened to me and I had to reinstall my VPS. The solution worked for me was install repos as you explain and then make a “yum update” this will update your php 5.3.x version to 5.4.x,

    Cheers.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.