Appendix A : Compiling and installing PHP and its extensions
There are a dozen different ways to get PHP, including downloading & compiling it yourself, downloading pre-compiled binaries, using package managers and software repositories, and finding it pre-installed by a forward thinking administrator. On most Linux distributions, PHP can be installed with a one-line command such as sudo apt-get install php5 or through graphical package managers like the Synaptic Package Manager or the Ubuntu Software Centre. Many common PHP extensions and add-ons are likewise available as pre-built packages or alternatively through the PECL and PEAR systems.
However there are times when it becomes necessary to do a little more work to install PHP, such as :
- when your project has requirements for a specific version of PHP that is different to the one shipped with your OS
- where you need extensions not available as packages,
- or when you want to compile a customised version of PHP specific to your needs.
Like anything involved in computers and software development, compiling PHP can take you down a rabbit-hole of options, customisations, compatibility issues, libraries and dependencies. A whole book could be written about the different possibilities (and possible headaches) involved. Luckily for us, in most use cases, the basics of compiling a standard version are quite straightforward. And like most things in life, it gets easier once you have done it once. The first section below will go over the steps necessary for getting, compiling and installing PHP and its core extensions. PHP is written in C, and as many of you may not be familiar with the process of compiling C programs, I have tried to explain each step below to give you an idea of what is happening. This makes the process seem a little more verbose, but in reality it is quite straightforward. Go ahead and try it if you don’t believe me! The next sections are also worth a read, as they cover installing extras like libraries and extensions from the PECL, PEAR and Packagist repositories.
Compiling and installing PHP itself
Note for Windows devs - These steps are for Linux/Unix type systems, and use free compiler tools almost always included with the OS. For Windows, the proprietary Visual Studio compiler is required, and the steps are somewhat different and beyond the scope of this book. Windows source code, pre-built binaries and instructions for compiling can be found at http://windows.php.net/download/ , with older versions in the archive at http://windows.php.net/downloads/releases/archives/.
Note for OS X devs - while the steps below are similar to those on a Mac, there are some issues you may run into depending on the version of OS X you are using. A great look at getting PHP (and related software) up and running on OS X Mavericks can be found at http://akrabat.com/computing/setting-up-php-mysql-on-os-x-mavericks/.
On *nix machines, our first step is to download the PHP source code from the PHP website at http://www.php.net/downloads.php . This page lists the current stable release and the previous supported stable release. Newer versions which are still under development are available at http://snaps.php.net/, and older end-of-life versions are available at http://museum.php.net/ . Git users can also pull the source code down from the official mirror at https://github.com/php.
When you have identified which version you want, make a note of the URL of the .tar.gz source code file which we will use later.
Compiling and installing (extra) core extensions
As we saw in the previous section, the most common way to install core extensions is to enable the relevant flags at the configure stage during compilation of the main PHP installation (note by default, many extensions are automatically enabled). However it’s not uncommon to come across the need to install an additional extension later on, for instance as the requirements for your program change from its initial design. There are two ways to approach this. The first, which you’ll find recommended a lot online, is to re-do the compilation/installation of PHP from scratch, adding the newly required modules at the configure stage (after issuing php -i to remember what configure options you used the first time). While this works perfectly well, compiling the full PHP binaries is a bit of a slog which can take older PCs in particular a long time to complete. There is a short cut however.
Each of the core extensions is actual a separate .so binary, and can be compiled independently. To do this, follow the first steps in the previous section to download and unpack the PHP source code and step into the directory. If you haven’t deleted it from when you compiled PHP itself, it should be ready to go. Within the source code is a directory called ext, inside of which are separate directories for each of the extensions.