Building PHP from Source on Ubuntu
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
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.
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 \
--with-gd=shared,/usr --enable-gd-native-ttf \
(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:
$ sudo make install
Finally, we need to make a small configuration change. The Ubuntu source distribution puts PHP includes
in a directory called
phc looks for
$ 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.
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.