Compiling PHP 5.4 on Ubuntu 12.04

So recently I’ve been working with PHP 5.4 a LOT. Unfortunately Ubuntu (my main dev environment) is behind the times. So I’m resorting to compiling PHP manually.

Not a daunting as it may first appear. The really tricky part is working out your dependencies and `configure` script.

Hence the reason for this post as a reminder for myself and others that may want to do a quick compile. (I would recommend that if your compiling for a production/live environment that you make sure you understand what it is your compiling though before just using what’s here)

So where to start. Dependencies first I think

Ubuntu allows you to install dependencies for building source `apt-get build-deps`. We will use this and install any extras we may need.

apt-get install \
libxml2 \
libxml2-dev \
libssl-dev \
pkg-config \
curl \
libcurl4-nss-dev \
enchant \
libenchant-dev \
libjpeg8 \
libjpeg8-dev \
libpng12-0 \
libpng12-dev \
libvpx1 \
libvpx-dev \
libfreetype6 \
libfreetype6-dev \
libt1-5 \
libt1-dev \
libgmp10 \
libgmp-dev \
libicu48 \
libicu-dev \
mcrypt \
libmcrypt4 \
libmcrypt-dev \
libpspell-dev \
libedit2 \
libedit-dev \
libsnmp15 \
libsnmp-dev \
libxslt1.1 \
libxslt1-dev

And now the configure

./configure \
--prefix=/usr/local/php \
--with-apxs2=/usr/local/apache2/bin/apxs \
--enable-fpm \
--with-fpm-user=www-data \
--with-fpm-group=www-data \
--with-config-file-path=/usr/local/php/conf \
--with-config-file-scan-dir=/usr/local/php/conf.d \
--enable-debug \
--with-openssl \
--with-kerberos \
--with-zlib \
--enable-calendar \
--with-curl \
--with-curlwrappers \
--with-enchant \
--enable-exif \
--enable-ftp \
--with-gd \
--with-jpeg-dir=/usr \
--with-png-dir=/usr \
--with-vpx-dir=/usr \
--with-freetype-dir=/usr \
--with-t1lib \
--enable-exif \
--enable-gd-native-ttf \
--enable-gd-jis-conv \
--with-gettext \
--with-gmp \
--with-mhash \
--enable-intl \
--enable-mbstring \
--with-mcrypt \
--with-mysql \
--with-mysqli \
--enable-pcntl \
--with-pdo-mysql \
--with-pdo-pgsql \
--with-pgsql \
--with-pspell \
--with-libedit \
--with-readline \
--enable-shmop \
--with-snmp \
--enable-soap \
--enable-sockets \
--enable-sysvmsg \
--enable-sysvshm \
--with-xsl \
--enable-zip \
--with-pear \
--enable-zend-signals \
--enable-maintainer-zts

Once these are done then we follow the standard make process. Notice we are also running make test… very important as it givges more data for the developers to work with.

make && make test && make install

The next thing is configuring your php.ini file as the install doesn’t have one yet so we copy either the production or development default from the source code to the new conf dir and edit to suit your needs.

cp {php-source-dir}/php.ini-(development|production) /usr/local/php/conf

Thats it. All ready to roll… almost, this installation is the one I use for use with a webserver so you will want to add the appropriate directives to apache.

LoadModule php5_module        modules/libphp5.so

AddHandler php5-script .php
AddType text/html .php

9 thoughts on “Compiling PHP 5.4 on Ubuntu 12.04

  1. I’ve got a similar setup (Ubuntu 12.04, Apache2.2, php “–with-apxs2=/path/to/apxs2”) but I’m having a tough time getting past a C compiler error, something like `rodata.1.1 can’t be used when compiling shared objects. Set the -fPIC flag and compile again.`

    I’ve added -fPIC to gcc and g++ with environment variables with no luck. I’ve tried setting and unsetting the “–enable-shared” flag in the php configuration options, among other things that I can’t remember at the moment (somewhat frustrated :)). Do you have any recommendations for getting past this particular issue?

    1. Hi Carl,

      I’m afraid this ones a little beyond my own capabilities without more info. If you can provide a bit more of the output I’d be happy to have a look and see if I can spot something that might help.

      Matt

  2. If you already have PHP install on the sever how to you recompile it?

    Where is the source code where you run ./configure if PHP is already installed or must I download PHP and recompile from the source?
    The last option is a little extreme and I want to avoid that.

    thx

    1. Hi Frederick,

      With the instructions I have given your php install will be located at /usr/local/php.

      What I normally do when I want to update is download the latest copy of php and run through the same process again using the same `configure`, `make` & `make install` commands (obviously making the appropriate adjustments as required) which will then overwrite the existing install.

      What you may find you will need to do is to also re-install any pecl extensions you have installed but this can vary from extension to extension.

      Matt

  3. If you replace ‘make install’ with ‘checkinstall’ (you will have to ‘apt-get install checkinstall’ first) then you’ll get a .deb out of it which can cleanly be uninstalled in the future. Also, you can specify the version and the system package that it replaces (in this case ‘php5’) and it will uninstall the old package for you when installing the .deb, and your build will satisfy dependencies for the package you replace. It also means that if Ubuntu ever got around to releasing a newer version of PHP than you had built, an ‘apt-get upgrade’ should theoretically handle the update for you.

  4. Note that the –enable-debug option included in the flags passed to ./configure has a non-trivial effect on performance (~2x degradation in microbenchmarks). You probably want to remove it if you’re intending to use the resulting binary in production.

    1. You are absolutely correct in that –enable-debug is not a trivial setting.

      However In the post I do state “that if your compiling for a production/live environment that you make sure you understand what it is your compiling though before just using what’s here”

      I would have thought that this implies that the instructions here are for a development environment. Were I compiling for Live I would be a lot more particular about my compilation so that I can get the best performance from it.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.