How do I upgrade to the latest PHP version in CentOS with yum?

I found some blog posts about this, but it's rather lack of descriptions on possible side effects.

I could really use some detailed on these steps:

  1. How to add a repo that provides PHP 5.4 into yum
  2. Can this seamlessly replaces the current PHP version in CentOS?
  3. How can I switch back to the official repo when it supports PHP 5.4? (current 5.3.3 in my system)
  4. Will there be any potential to break PHP modules I currently using?

Note

People successfully upgraded with the same method on newer versions, and suggest removing specific versions in the question. While it is good to pin down versions in case newer versions actually breaks something, I'd like keep the latest succeed version suggested by the community as a note.

Feel free to update this if you have successfully upgraded on other versions.

  • PHP versions: 5.4, 5.5, 5.6
  • CentOS version: 5, 6

I followed the instructions from Install Apache/PHP 5.4.10 on Fedora 17/16, CentOS/RHEL 6.3/5.8 with a slight modification. It took maybe 10min. My exact commands are shown below. Note that the first command had to be changed from what is shown in the article. The change was from epel-release-6-7.noarch.rpm to epel-release-6-8.noarch.rpm.

  1. How to add a repo that provides PHP 5.4 into yum?

    yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm
    yum install http://rpms.remirepo.net/enterprise/remi-release-6.rpm
    
  2. Can this seamlessly replace the current PHP version in CentOS? For me the following commands worked and none of my existing PHP web pages broke. Your mileage may vary.

    yum --enablerepo=remi install httpd php php-common
    yum --enablerepo=remi install php-pecl-apc php-cli php-pear php-pdo php-mysql php-pgsql php-pecl-mongo php-sqlite php-pecl-memcache php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml
    service httpd restart
    
  3. How can I switch back to the official repo when it supports PHP5.4? I have not tested the commands to remove and re-install PHP from CentOS repositories, but these should work.

    # Remove the Remi packages. Note the reversed command order
    yum remove php-pecl-apc php-cli php-pear php-pdo php-mysql php-pgsql php-pecl-mongo php-sqlite php-pecl-memcache php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml
    yum  remove httpd php php-common
    # Install the CentOS packages. 
    yum install httpd php php-common
    yum install php-pecl-apc php-cli php-pear php-pdo php-mysql php-pgsql php-pecl-mongo php-sqlite php-pecl-memcache php-pecl-memcached php-gd php-mbstring php-mcrypt php-xml
    
  4. Will there be any potential to break PHP modules I [sic] currently using? Yes. Using a recent version of CentOS (6.2?) with Zend installed using the Zend installer, the above upgrade broke Zend.

All the above commands were run as root. Best practice is to login as a non-privileged user and use sudo. (This is a development VM with a current snapshot...)

Also, do NOT enable the Remi repository by default - in the past I got clever and enabled it by default and things broke, even with repository priority.


An improvement over the other Webtatic answer:

1 . Using the guide on http://www.webtatic.com/packages/php54/:

rpm -Uvh http://mirror.webtatic.com/yum/el6/latest.rpm

2 . You need to first see what existing installed packages need replacing, you can do this by using yum shell to combine removing php-common and installing php54w-common in one transaction (so shared dependencies aren't removed)

yum shell
> remove php-common
> install php54w-common
> run
…
Is this ok [y/N]: n

Don't say "y" to the results, but take note of all packages mentioned in "Removing for dependencies", e.g.

Removing for dependencies:
 php
 php-cli
 php-pear
 php-pecl-memcache
 php-mysql

If there are other packages than php* in this list then you can't seamlessly switch to Webtatic PHP 5.4, but will have to investigate alternatives. Webtatic has replacement packages for all base php packages (see the packages listed on the page linked to in 1. for confirmation), so there should be no issues, unless you use other 3rd party repositories that have packages installed dependent on the specific php version installed.

For the rest of the installation (still in yum shell), you just remove these packages and install their php54w-* counterparts:

> remove php-common php php-cli php-pear php-pecl-memcache php-mysql
> install php54w-common php54w php54w-cli php54w-pear php54w-pecl-memcache php54w-mysql
> run
…
Is this ok [y/N]: y

You should then only see the packages you've set to be removed and installed in the list, and you can confirm the installation to switch over. Any services currently running with php loaded will need to be restarted, for instance httpd or php-fpm.

As for this being 'seamless', any software changes happening while users are able to access the website should have the consequences fully understood. Shared libraries being removed and added when a process hasn't already loaded them could potentially load while the shared library isn't there. It's better to do software upgrades like this offline, and preferably tested on a non-production machine first to verify the process works as expected.

3 . To switch to a hypothetical CentOS base php54 package (CentOS 5 used php53 prefix), you just run the above steps replacing php removal with php54w removal, and php54w installation with php54 installation e.g.

yum shell
> remove php54w-common
> install php54-common
> run
…
Is this ok [y/N]: n
…
> remove php54w-common php54w php54w-cli php54w-pear php54w-pecl-memcache php54w-mysql
> install php54-common php54 php54-cli php54-pear php54-pecl-memcache php54-mysql
> run
Is this ok [y/N]: y

However as for the switch to the hypothetical base php54, there will likely be no alternatives for some packages (e.g. there was no php53-pecl-memcache extension in CentOS 5) and Webtatic has packages that aren't available in CentOS base (e.g. php54w-pecl-zendopcache). If they are missing, then you usually don't have any other option than to use pecl directly to install them.

4 . Any PHP upgrade may introduce bugs, and websites should be retested. This is not specific to Webtatic, but the general idea that new features introduce new bugs.

Unlike Remi's repository in the accepted answer, the Webtatic EL6 repository never uses the same package names as CentOS base repositories, so will not override installation/upgrade of packages you don't intend to switch to, and as such is enabled by default.

Disclaimer: I'm the owner/maintainer of Webtatic