Build and install PHP 5.3.29 from source on an Ubuntu 14.04 LTS server

Few days ago, the PHP development team announced the availability of a new PHP 5.3.29 version. This release contains about 25 potentially security related fixes back-ported from PHP 5.4 and PHP 5.5. So, if you're running some older CMSes, scripts or web applications or whatever, I strongly recommend you make them compatible with the latest PHP version available.

But anyway, if for some crazy reason your application is not able to run on a PHP version newer than 5.3, then it is strongly recommended to upgrade your PHP 5.3 to the latest PHP patch-set available, which is 5.3.29.

In the following write-up, I'll cover the steps I used to upgrade PHP to version 5.3.29. I'm running two different PHP versions simultaneously on an Ubuntu 14.04 LTS based server and as you can imagine, the PHP 5.3 version has been built from source so I had to re-compile PHP in order to upgrade.


TABLE OF CONTENTS


UPDATE THE SYSTEM

Ok, first thing to do, is to make sure the Ubuntu 14.04 system is fully up-to-date by running the following commands in your terminal:

apt-get update
apt-get upgrade


INSTALL BUILD DEPENDENCIES

Next, to avoid compilation errors about missing build dependencies, fire-up your terminal and install the following packages using apt:

apt-get install libfcgi-dev libfcgi0ldbl libjpeg62-dbg libmcrypt-dev libssl-dev libbz2-dev libjpeg-dev \
    libfreetype6-dev libpng12-dev libxpm-dev libxml2-dev libpcre3-dev libbz2-dev libcurl4-openssl-dev \
    libjpeg-dev libpng12-dev libxpm-dev libfreetype6-dev libmysqlclient-dev libt1-dev libgd2-xpm-dev \
    libgmp-dev libsasl2-dev libmhash-dev unixodbc-dev freetds-dev libpspell-dev libsnmp-dev libtidy-dev \
    libxslt1-dev libmcrypt-dev libdb5.3-dev


DOWNLOAD PHP

Now let's download the latest PHP 5.3.29 archive from some PHP mirror using the commands below:

PHP_VERSION=5.3.29
wget -O /var/tmp/php-${PHP_VERSION}.tar.bz2 \
    http://bg2.php.net/get/php-${PHP_VERSION}.tar.bz2/from/this/mirror


EXTRACT PHP SOURCES

I'm going to extract the PHP source in the /opt/build directory and navigate to /opt/build/php-5.3.29 using:

mkdir -p /opt/build
tar jxf /var/tmp/php-${PHP_VERSION}.tar.bz2 -C /opt/build
cd /opt/build/php-${PHP_VERSION}


CONFIGURE PHP SOURCES

The configure options depend on your environment. In my case, I'm running PHP as FastCGI i.e PHP-FPM with many useful extensions enabled. You can run ./configure --help to find all available configure options. My configuration looks like the one below:

./configure \
    --enable-fpm \
    --with-mcrypt \
    --enable-mbstring \
    --with-openssl \
    --with-mysql \
    --with-mysql-sock \
    --with-gd \
    --with-jpeg-dir=/usr/lib \
    --enable-gd-native-ttf  \
    --with-pdo-mysql \
    --with-libxml-dir=/usr/lib \
    --with-mysqli=/usr/bin/mysql_config \
    --with-curl \
    --enable-zip  \
    --enable-sockets \
    --with-zlib \
    --enable-exif \
    --enable-ftp \
    --with-iconv \
    --with-gettext \
    --enable-gd-native-ttf \
    --with-t1lib=/usr \
    --with-freetype-dir=/usr \
    --prefix=/opt/PHP-5.3 \
    --with-config-file-path=/opt/PHP-5.3/etc \
    --with-fpm-user=www-data \
    --with-fpm-group=www-data

this will make PHP 5.3.29 installed within the /opt/PHP-5.3 directory.


NOTE

if you encounter an error like

configure: error: freetype.h not found

even-though you installed libfreetype6-dev, then execute the following commands and try the ./configure again:

mkdir /usr/include/freetype2/freetype
ln -s /usr/include/freetype2/freetype.h /usr/include/freetype2/freetype/freetype.h

and if you're still slapped with configure: error: freetype.h not found, please contact me & I'll be more than happy to look into this


COMPILE PHP

Now it's a compilation time. So, run make to build the sources:

make

once the compilation is completed, optionally you may want to run make test to test the set-up:

make test


INSTALL PHP

And finally, run make install to install the compiled PHP to /opt/PHP-5.3

make install


VERIFY PHP VERSION

To check the PHP version, run:

/opt/PHP-5.3/bin/php -v

PHP 5.3.29 (cli) (built: Aug 20 2014 07:33:45) 
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2014 Zend Technologies

That's it.