Update CentOS 6 to PHP v5.4 without using third party repository

In this article, we are going to upgrade CentOS 6 to PHP 5.4 without using any third party repositories.

You might want to do this if you, like me, are using WordPress, and want to test your install before upgrading to CentOS 7, or if you have a plugin which requires PHP 5.4.

Note: I found some problem with my wordpress post feature only after the upgrade. I started playing with settings and ended up causing the dreaded White Screen of Death across all sites hosted on my server. After downgrading to 5.3, it was fine, however after upgrading again, it continued to work fine and wouldn’t you know, the post feature was fixed!

So with that said, be sure to take a full backup of your server before making this change, or setup a snapshot volume so you can merge back in the snapshot in case something really breaks!

For the steps below, I am going to assume you have not made any customization to your php.ini and /etc/php.d files. If you have, you will want to compare the differences afterward against what you are running now.

First thing, check what php RPMs are installed:

rpm -qa |grep -i php

Note these down

Next, install CentOS SCL:

yum install centos-release-SCL

Then install php54 RPM and the php5.4 version of the same RPMs as you wrote down above:

yum install php54 `rpm -qa |grep php |grep -v php54 |cut -d. -f1 |sed -e "s/\-[0-9]$//g" -e "s/php/php54-php/g"`

Once that is done, you can do another rpm -qa to check and ensure that all of the RPMs between the 2 versions match:

rpm -qa |grep -i php

Finally, remove php 5.3:

yum erase php php-\*

Now that the install is complete, let’s go ahead and make apache use it:

service httpd configtest && service httpd restart

If all is well, then you can create a phpinfo page and see that your php version is now php 5.4

Other sites will tell you that you need to source a file to enable php 5.4, however this only applies if you are planning to call php from the command line. Apache uses a PHP module, and this version of PHP automatically installs the correct configuration to make this work via apache.

However, in case you do want to call php from the command line, you can do the following:

cd /etc/profile.d
ln -s /opt/rh/php54/enable php54.sh

Then, you will want to logout and log back in to your SSH session.