With the release of PHP 5.3 to the world, I wanted to be one of the first to try it. The problem is that the typical package managers for Ubuntu won’t include PHP 5.3 for some time – perhaps as long as a year. This is a problem, since I really want to try PHP’s latest and greatest features for myself.

The problem is, there seems to be a lack of clear, coherent instructions online about compiling PHP on Ubuntu from source. Either it’s so insanely simple that anyone who does it figures everyone else knows how, or everyone relies on the pre-built binaries released by the world that any time I search for “Ubuntu PHP source” I get “why don’t you just use the built-in package manager?” And so, I wanted to write a set of instructions for how I configured and compiled PHP, on Ubuntu Jaunty.

For the most part we’ll use standard out-of-the-box packages that Ubuntu provides. Since the features I’m looking for are in PHP 5.3, I’m fine with having slightly outdated packages for other software as long as they’re compatible with PHP 5.3.



Warning!!!! WordPress has added HTML entities in order to help display this post. Do NOT copy and paste the ./configure line as it will cause you to get strange results. Prepare your own configure line that matches your needs!

Warning: Messing with Apache and PHP on a live web server can cause serious problems! Do this only on a development system or on a system that is not critical to your operation! Trying to install a new version of PHP with a current version running might work, or it might sleep with your wife and eat your children. You’ve been warned!

Warning: Remove any existing instances of PHP, either compiled from source or installed by package, and all related files, before proceeding!

1. Install Apache.

The first step is to install Apache. You can feel free to use your own web server, but for this example I opted for Apache based on the simplicity of installing PHP as an Apache module. This command should also install the development tools, which PHP will need to properly configure itself for Apache.

aptitude install apache2 apache2-mpm-prefork apache2-prefork-dev apache2-utils apache2.2-common

2. Install MySQL and PostgreSQL

Most people use MySQL, but almost all PHP installations seem to have PostgreSQL installed as well. I think it’s always good to keep your options open, and you might take a look at Postgres at some point. If you opt not to install Postgres, that’s fine; just alter your configure statement to exclude it.

aptitude install postgresql-8.3 postgresql-client-8.3 postgresql-client-common postgresql-common postgresql-server-dev-8.3 aptitude install mysql-client mysql-client-5.0 mysql-common mysql-server mysql-server-5.0 mysql-server-core-5.0

3. Install required libraries

PHP itself is very easy to configure and install. However, if you want to get the most out of it you’ll need to install the dependency libraries it uses for various functions like mcrypt and gd. This can be the most challenging part of installing PHP; many hours can be spent issuing a configure command only to have it fail, have to install another library, and repeat the process.

These libraries are the libraries that were needed to install the options in the configure statement below. You may need more or fewer libraries depending on your individual needs and desires.

aptitude install libtidy-dev curl libcurl4-openssl-dev libcurl3 libcurl3-gnutls zlib1g zlib1g-dev libxslt1-dev libzip-dev libzip1 libxml2 libsnmp-base libsnmp15 libxml2-dev libsnmp-dev libjpeg62 libjpeg62-dev libpng12-0 libpng12-dev zlib1g zlib1g-dev libfreetype6 libfreetype6-dev libbz2-dev libxpm4-dev libmcrypt-dev libmcrypt4

4. Download the PHP Source Code

Visit http://www.php.net and download the latest version of PHP 5.3. The following commands may be helpful:

cd ~

wget http://us3.php.net/get/php-5.3.0.tar.gz/from/this/mirror

tar xvfz php-5.3.0.tar.gz

5. Configure.

Change your working directory to the unpacked PHP source code. Execute the following command.

./configure –with-apxs2=/usr/bin/apxs2 –with-mysql=/usr –with-mysqli=/usr/bin/mysql_config –with-pgsql=/usr –with-tidy=/usr –with-curl=/usr/bin –with-curlwrappers –with-openssl-dir=/usr –with-zlib-dir=/usr –enable-mbstring –with-xpm-dir=/usr –with-pdo-pgsql=/usr –with-pdo-mysql=/usr –with-xsl=/usr –with-ldap –with-xmlrpc –with-iconv-dir=/usr –with-snmp=/usr –enable-exif –enable-calendar –with-bz2=/usr –with-mcrypt=/usr –with-gd –with-jpeg-dir=/usr –with-png-dir=/usr –with-zlib-dir=/usr –with-freetype-dir=/usr –enable-mbstring –enable-zip –with-pear

Edit: Many commenters suggested using the –prefix=PREFIX directive to place your PHP files and limit the potential problems should you decide to remove or replace PHP. This is a good idea.

The screen should scroll quickly as it does a check of the files and libraries it will need for compiling, and, if everything is installed correctly, it will build the makefiles for the next step. If it fails, it will give you an error message (usually related to a missing library) and tell you to try again.

6. Make & Make Install

In order to have PHP installed, we have to first compile it from the source code. This involves the compiler accessing the libraries it needs. Once compiled, we’ll be able to install it. The first command in this process is easy enough:

make

There will once again be a lot of information on your screen. Take your time, go for a walk. This takes a bit of time, and the more you compile into PHP the longer the process takes. PHP is an advanced, complicated language, so be patient.

Once it completes successfully, you should see a warning to run “make test”. You may run this, though you don’t have to at this stage.

The final step in the PHP compilation process is invoking the installer.

make -i install

Why do we use -i on the installer? Ubuntu uses an unusual configuration for Apache which causes the installer not to know how to install PHP properly. This will produce a fatal error and cause the install to stop. The -i flag tells it to ignore the errors; we’ll set up Apache to use PHP 5.3 next.

7. Add the PHP Module to Apache

In order to have PHP run as a part of the web server, we’ll need to create a module for it. This is typically done automatically, but due to the unusual setup of the Ubuntu Apache installation, we’ll have to do it ourselves.

First, change directories:

cd /etc/apache2/mods-available

Create a new file called php5.load and paste the following line into it:

LoadModule php5_module /usr/lib/apache2/modules/libphp5.so

Save that file. Now create another file called php5.conf and paste the following into it:

AddType application/x-httpd-php .php .phtml .php3

AddType application/x-httpd-php-source .phps

Now we’re all set! We just need to enable the module and restart Apache:

# a2enmod php5

Enabling module php5.

Run ‘/etc/init.d/apache2 restart’ to activate new configuration!

# /etc/init.d/apache2 restart

* Restarting web server apache2

… waiting …done.

8. Test the PHP Installation

Now we just need to see that PHP is in fact installed!

First let’s check the PHP binary:

$ php -v

PHP 5.3.0 (cli) (built: Jul 1 2009 18:53:06)

Copyright (c) 1997-2009 The PHP Group

Zend Engine v2.3.0, Copyright (c) 1998-2009 Zend Technologies

If you get output similar to the above, your PHP installation is correct. Note that the point release may be different since these directions were written; however, you should see PHP 5.3.x as the version.

Next, let’s get the info from PHP to see what’s installed. Find your webroot, create (or edit) an index.php file and add the following:

<?php phpinfo(); ?>

Type the URL of your web server into the browser and you should see something that looks like this:

If you do, congratulations! You’ve successfully installed PHP 5.3 on Ubuntu from source.

Frustrated with your company’s development practices? You don't have to be! No matter what the issues are, they can be fixed. You can begin to shed light on these issues with my handy checklist. Plus, I'll help you with strategies to approach the issues at the organization level and "punch above your weight."

Great! We'll be updating you soon on best practices for your team!

Brandon Savage is the author of Mastering Object Oriented PHP and Practical Design Patterns in PHP