Programmers' Pain
26Oct/111

Howto upgrade PHP on CentOS 5.x

PHP LogoCentOS 5.x comes with PHP in version 5.1.6 which is pretty old those days. There are already quite some web applications that have dropped PHP 5.1.x support which makes it impossible to install them on a CentOS 5.x installation. With the release of CentOS 5.6 additional packages called php53, php53-common, php53-ldap, etc. have been released which could replace the 5.1.6 default version of PHP if you don’t have other RPM packages that depend on the original php packages. Since the php and the new php53 packages do install their resources into the same directories you can’t install both of them in parallel. Instead of faking empty php packages to be able to install all php53 packages without conflicts and missing dependencies I decided to go for a third party PHP installation provided by AtomiCorp.

If you need to find out which CentOS version you’re running you should have a look at the content of the following file:

1
2
cat /etc/redhat-release
CentOS release 5.6 (Final)

You can use yum to get a list of all currently installed PHP packages so that you know which packages of the AtomiCorp PHP installation you have to install and which one you might have to install manually:

1
2
3
4
5
6
7
8
9
10
11
12
13
yum list installed | grep php
php.x86_64             5.1.6-27.el5_5.3      installed
php-cli.x86_64         5.1.6-27.el5_5.3      installed
php-common.x86_64      5.1.6-27.el5_5.3      installed
php-devel.x86_64       5.1.6-27.el5_5.3      installed
php-gd.x86_64          5.1.6-27.el5_5.3      installed
php-imap.x86_64        5.1.6-27.el5_5.3      installed
php-mbstring.x86_64    5.1.6-27.el5_5.3      installed
php-mysql.x86_64       5.1.6-27.el5_5.3      installed
php-pdo.x86_64         5.1.6-27.el5_5.3      installed
php-pear.noarch        1:1.4.9-6.el5         installed
php-pear-Date.noarch   1.4.7-2.el5.centos    installed
php-xml.x86_64         5.1.6-27.el5_5.3      installed

Now that you’re aware of which RPMs are currently installed we can install the alternative PHP installation provided by AtomiCorp following their installation instructions. First we have to install their repository definitions to allow yum to download the new RPMs:

1
wget -q -O - http://www.atomicorp.com/installers/atomic | sh

Since I also want to upgrade my MySQL installation from the CentOS default version 5.0 to 5.1 I added also the mysql package to the yum update command:

1
yum update php mysql

This results in the following packages being updated or installed due to additional dependencies of the new PHP and / or MySQL versions:

1
2
3
4
5
6
7
8
9
10
11
12
13
Dependency Installed:
  libICE.x86_64 0:1.0.1-2.1                                     libSM.x86_64 0:1.0.1-3.1
  libXaw.x86_64 0:1.0.2-8.1                                     libXext.x86_64 0:1.0.1-2.1
  libXmu.x86_64 0:1.0.2-5                                       libXt.x86_64 0:1.0.2-3.2.el5
  libedit.x86_64 0:3.0-2.20090923cvs.el5.art                    mysql-libs.x86_64 0:5.1.58-2.el5.art
  mysqlclient15.x86_64 0:5.0.90-1.el5.art                       t1lib.x86_64 0:5.1.2-2.el5.art
Updated:
  mysql.x86_64 0:5.1.58-2.el5.art                              php.x86_64 0:5.3.6-5.el5.art
Dependency Updated:
  mysql-server.x86_64 0:5.1.58-2.el5.art    php-cli.x86_64 0:5.3.6-5.el5.art      php-common.x86_64 0:5.3.6-5.el5.art
  php-devel.x86_64 0:5.3.6-5.el5.art        php-gd.x86_64 0:5.3.6-5.el5.art       php-imap.x86_64 0:5.3.6-5.el5.art
  php-mbstring.x86_64 0:5.3.6-5.el5.art     php-mysql.x86_64 0:5.3.6-5.el5.art    php-pdo.x86_64 0:5.3.6-5.el5.art
  php-xml.x86_64 0:5.3.6-5.el5.art

After yum has successfully installed the new PHP version you might want to disable the new AtomiCorp Repository to not interfere with the usual CentOS updates. To do so you have to edit the following file and set the enabled property to 0:

1
2
3
4
5
6
7
8
9
10
vi /etc/yum.repos.d/atomic.repo
[atomic]
name = CentOS / Red Hat Enterprise Linux $releasever - atomicrocketturtle.com
mirrorlist = http://www.atomicorp.com/mirrorlist/atomic/centos-5-$basearch
#mirrorlist = http://www.atomicorp.com/channels/atomic/centos/5/mirrors-atomic
enabled = 0
priority = 1
protect = 0
gpgkey = file:///etc/pki/rpm-gpg/RPM-GPG-KEY.art.txt
gpgcheck = 1

Now you should be able to call the PHP interpreter to check for the PHP version:

1
2
3
4
5
php --version
PHP Deprecated:  Directive 'safe_mode' is deprecated in PHP 5.3 and greater in Unknown on line 0
PHP 5.3.6 (cli) (built: Aug  3 2011 12:13:41)
Copyright (c) 1997-2011 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies

So version 5.3.6 is now the default PHP version of your CentOS installation. But as stated by the PHP interpreter you also have to update you php.ini file to fit the new PHP version:

1
2
mv /etc/php.ini /etc/php.ini_backup
mv /etc/php.ini.rpmnew /etc/php.ini

Be sure to update all custom settings of your old php.ini file to the one provided by the new RPM packages. After this has been done you should be able to call the PHP interpreter without an error message complaining about deprecated configuration values:

1
2
3
4
php --version
PHP 5.3.6 (cli) (built: Aug  3 2011 12:13:41)
Copyr<strong></strong>ight (c) 1997-2011 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2011 Zend Technologies

In that context you might want to set the default timezone for all date functions which is a behaviour of PHP that has changed from version 5.1 to 5.3. To do so you have to find the line containing the date.timezone property and set it to your desired default timezone:

1
2
3
4
5
vi /etc/php.ini
[Date]
; Defines the default timezone used by the date functions
; http://www.php.net/manual/en/datetime.configuration.php#ini.date.timezone
date.timezone = Europe/Berlin

Since we’ve also updated the MySQL installation from 5.0 to 5.1 we have to upgrade our existing databases to the new MySQL version. MySQL should still be able to start up without those upgrade tasks applied to it but it will remove several errors from the MySQL logfiles. To do so you have again to backup you existing main MySQL configuration file and replace it with the new one:

1
2
mv /etc/my.cnf /etc/my.cnf_backup
mv /etc/my.cnf.rpmnew /etc/my.cnf

Again make sure you adapt all old settings to the new MySQL configuration file. To upgrade your existing databases to the new MySQL version you just have to call the mysql_upgrade command which will than connect as the root user to your running MySQL instance to apply the needed upgrade statements. If you don’t know you root password or if you don’t have local root user access set up in your MySQL permissions you could just call the mysqld_safe script with the –skip-grant-tables parameter to allow full access to your MySQL instance and apply the upgrade statements afterwards:

1
2
/usr/bin/mysqld_safe --skip-grant-tables
mysql_upgrade

After that you should be able to start up your upgraded databases without any errors in the MySQL log file. For my CentOS 5.6 installations I wasn’t able to find any problems after upgrading PHP following this Howto. I’m still able to install any CentOS RPM that has a dependency to the original PHP version of CentOS since the names of the RPMs haven’t changed. Of course you should check your currently used web applications that they are compatible with a PHP 5.3.x installation which is also the main reason that the RHEL/CentOS guys decided to provide PHP 5.3.x as additional packages since some of the provided web applications still depend on a 5.1.x PHP version. But if you’re sure that you don’t want to use any of those older web applications I’d say that this Howto is the easier solution to upgrade your PHP installation instead of dealing with two PHP installations at the same time.

Comments (1) Trackbacks (0)

Leave a comment

Connect with Facebook

No trackbacks yet.