Building PHP from Source on Ubuntu

Aug 18 2010

This article describes how to build PHP from source on Ubuntu. I am doing this because I need to build PHP with the embeded SAPI -- an option not available by default. For my purposes, I don't want this PHP to replace the existing PHP. Instead, I want it to be available alongside my normal PHP. So the goal is to place this version in /usr/local/php-opt. <!--break-->

Building a Custom PHP (Quickly)

I am building a custom PHP instance to use with PHC (the PHP compiler). Rather than start from scratch, I'm going to begin from Ubuntu's distribution of PHP, and build my own varation. This way, I can take advantage of Ubuntu's dependency system. This will greatly minimize the amount of work involved in building.

1. Install Dependencies and Source

To begin, we use the APT system to get started with our base source code and headers. Get all of the right source code and libraries: ~~~ $ sudo aptitude install php5-dev libxml2-dev $ apt-get source php5 $ sudo apt-get build-dep php5 ~~~

Installing the php5 source will create a directory in your current location called something like php5-5.3.2. You will be doing most of your work from inside of this directory.

2. Configure PHP

Change directories into the PHP source folder. cd php5-5.3.2.

We need to execute the configure script, and I do this by hand-combining the right config options from php5-5.3.2/debian/rules (See, for example, COMMON_CONFIG in that file):

$ ./configure  --prefix=/usr/local/php-opt \
                --enable-embed \
                --with-regex=php \
                --disable-rpath \
                --disable-static \
                --with-pic \
                --enable-calendar \
                --enable-sysvsem \
                --enable-sysvshm \
                --enable-sysvmsg \
                --enable-bcmath \
                --with-bz2 \
                --enable-ctype \
                --with-db4 \
                --without-gdbm \
                --with-iconv \
                --enable-exif \
                --enable-ftp \
                --with-gettext \
                --enable-mbstring \
                --with-pcre-regex=/usr \
                --enable-shmop \
                --enable-sockets \
                --enable-wddx \
                --with-libxml-dir=/usr \
                --with-zlib \
                --with-kerberos=/usr \
                --with-openssl=/usr \
                --enable-soap \
                --enable-zip \
                --with-mhash=yes \
                --with-system-tzdata \
                --with-curl=shared,/usr \
                --with-enchant=shared,/usr \
                --with-zlib-dir=/usr \
                --with-gd=shared,/usr --enable-gd-native-ttf \
                --with-gmp=shared,/usr \
                --with-jpeg-dir=shared,/usr \
                --with-xpm-dir=shared,/usr/X11R6 \
                --with-png-dir=shared,/usr \
                --with-freetype-dir=shared,/usr \
                --enable-intl=shared \
                --with-ttf=shared,/usr \
                --with-t1lib=shared,/usr \
                --with-ldap=shared,/usr \
                --with-ldap-sasl=/usr \
                --with-mysql=shared,/usr \
                --with-mysqli=shared,/usr/bin/mysql_config \
                --with-pspell=shared,/usr \
                --with-recode=shared,/usr \
                --with-xsl=shared,/usr \
                --with-snmp=shared,/usr \
                --with-sqlite=shared,/usr \
                --with-sqlite3=shared,/usr \
                --with-tidy=shared,/usr \
                --with-xmlrpc=shared \
                --enable-pdo=shared \
                --without-pdo-dblib \
                --with-pdo-mysql=shared,/usr \
                --with-pdo-sqlite=shared,/usr \
                --with-pdo-dblib=shared,/usr

(Note that I did omit a few things like PostgreSQL and SQL Server support.)

There are two important things to note about the command above:

  • --prefix is set to /usr/local/php-opt. This will cause our PHP to get installed there, instead of in /usr/local/bin. If you are trying to build the embeded system, you almost certainly want this.
  • --enable-embed turns on the embedded SAPI. We want this for PHC.

3. Compile PHP

Once configuration is done, we can build and install:

$ make
$ sudo make install

Finally, we need to make a small configuration change. The Ubuntu source distribution puts PHP includes in a directory called php5/, but phc looks for php/.

$ cd /usr/local/php-opt/include
$ sudo ln -s php5 php

Now we have a SAPI-enabled version of PHP installed, and due to its location, we can run it alongside normal PHP without hassles.

=============

A Possible Alternative Route?

It may be possible (I'm not sure) to rebuild this as a Debian package. Instead of following step 2 and on above, you might want to try this.

Now edit the php5-5.3.2/debian/rules file for building. You need to find the line that begins COMMON_CONFIG and add an extra configuration flag, --enable-embed. NB: I am not sure that this is the right place for the --enable-embed option. It might make more sense to include it in the options for building the CLI. If anyone succeeds at this, please let me know.

COMMON_CONFIG=--build=$(DEB_BUILD_GNU_TYPE) \
                --host=$(DEB_HOST_GNU_TYPE) \
                --enable-embed \
                --sysconfdir=/etc \
                --mandir=/usr/share/man \

Once that is saved, make sure you are in the php5-5.3.2 directory and build a Debian package:

dpkg-buildpackage -b -j4 -D

When that is done, you will have a new PHP package. You can install it like this:

$ dpkg --instdir=/usr/local/php-opt -i php5-something.deb

Note that this will install your new package in /usr/local/php-opt, instead of under the root. This makes it possible for you to keep the default Debian version should you need it.