Installing a manual build of PHP on 64-bit CentOS / Linux

Charles Roth, 11 Nov 2012       (Techblog top)

I. Introduction
I have a 64-bit server running a somewhat older version of CentOS (e.g. 5.8).  But I need a newer version of PHP (than that distro of CentOs provides).

The solution sounds simple: download and compile a newer PHP from source.  Turns out to be not as easy as it sounds.  My first several attempts produced a variety of disasters, the least of which was the obstinate (and invisible) failure of PHP's mysql_connect() function.

II. Solution

  1. First, remove all traces of the existing PHP.  A crude (but effective) approach is to grep for php in all of the yum packages, and then do a "yum remove" on each package.

    For example, as root:

       yum list all | grep php | (while read a b; do echo "yum remove $a"; done) > cleanup.sh
    
    Then examine cleanup.sh carefully before you run it:
       chmod 700 cleanup.sh
       ./cleanup.sh
    
    (and be prepared to answer 'y' to several yum prompts.)

  2. Install the Apache and MySQL development packages:
       yum install httpd-devel.x86_64
       yum install mysql-devel.x86_64
    

  3. Download and unpack the PHP source.  See www.php.net/downloads.php.

  4. Configure and build PHP thusly:
       ./configure --with-apxs2=/usr/sbin/apxs --with-mysql=/usr --with-libdir=lib64 --enable-mbstring
       make
    

  5. Install.  Assuming the 'make' succeeds, do:
       service httpd stop
       make install
       service httpd start