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

58,772

Solution 1

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.

Solution 2

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

Share:
58,772

Related videos on Youtube

Vicary
Author by

Vicary

Updated on September 18, 2022

Comments

  • Vicary
    Vicary over 1 year

    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
  • Vicary
    Vicary over 11 years
    You kinda answered 1. When it comes to server maintenance, it is not simply installing it and run tests, I could really use more info before doing anything.
  • Kazimieras Aliulis
    Kazimieras Aliulis over 11 years
    Things often get ugly with remi. I prefer to use iuscommunity repository for up-to-date php. Another way is to use Zend server repo, but it seems a bit overhead for me.
  • Michael Hampton
    Michael Hampton over 11 years
    @KazimierasAliulis Unfortunately the IUS repo induces dependency hell by using incompatible package and file names. I can't recommend using it.
  • Vicary
    Vicary over 11 years
    Very thorough description. Since I have no code related to Zend engine, it shouldn't matter in my case, right?
  • Jeff Benshetler
    Jeff Benshetler over 11 years
    @Vicary It depends on what third-party libraries/frameworks you are using. Generally, this is a conservative update so you should be pretty safe.
  • Vicary
    Vicary over 11 years
    @MichaelHampton I use yum --enablerepo=remi update and it takes care of all related php packages, looks like a better solution. The server runs without problems for some days now, thanks for all the info.
  • Vicary
    Vicary almost 11 years
    The yum shell method is really good for dependancy observing, thanks.
  • Markos karpodinis
    Markos karpodinis over 10 years
    Note : the guy forgot to add php to the install list, which caused me alot of problems as apache didn't servce php files, so don't forget to add this when using
  • Gaia
    Gaia over 9 years
    @Andy, great detailed write up, thank you. How do I change the default cli (and only the cli) of PHP to 5.4 in CentOS 6?