Installing PHP 5 from source

PHP 5.3 offers performance improvements and somewhat saner defaults. Unfortunately, neither Debian Lenny nor Centos ship it. If you want to install it you will need to compile it. The process is not totally straightforward, especially if you desire to install php as an Apache module.

Choosing php extensions

After downloading and unpacking php, run ./configure –help to examine the different extensions that you can build. Be careful when compiling you have to select the appropriate extensions. I prefer to enable the bare minimum, to speed up compilation and simplify extension management (if you have not built the extension at all, you do not run the risk of loading it when you do not need it). Many applications still do not make use of PHP 5-only feaures features. If you are using Wordpress or Drupal 6, for instance, you can disable pdo, even if from the description it may look like essential. Traditionally, the mysql and mysqli extensions needed to be compiled against the mysql client headers, as they had to use the MySQL client library to communicate with the server. Starting with PHP 5.3, a new native library has been introduced, called mysqlnd. Mysqlnd should offer better performance and allows you to avoid installing the mysql headers just to compile PHP, so you might want to enable it with these configure options:

Make and make install

Make itself runs without problems most of the time. Running make install may cause problems if you are installing the Apache module. Apache uses an utility called apxs during the install process. Apxs might fail once it attempts to configure the module, bringing the whole installation to a halt. You will get a message error containing the line

This problem affects distributions which split the Apache config files, so that the main Apache configuration file does not contain any LoadModule directive. This thread suggests adding a dummy LoadModule directive, but really, it's fine force the install to continue with

then add the LoadModule directive yourself. The sample php module configuration does not contain the recommended Apache directives. The correct directive is AddHandler, not AddType.

Wrap up

Now run

to check that the configuration is what you expected. This gives the same information as the phpinfo() function, but you do not need to use a web browser to make it readable. Among the listed information, you can find the location of the parsed php.ini files. A sample php.ini will have been installed, which configures the apc extension, but apc has to be installed first through pecl, so php will complain. Comment out the line activating apc in php.ini to resolve this problem (of course, you can also install apc if you wish so, in fact using an opcode cache is a huge boost to php performance). Now you should be all set to go. If you experience any difficulties, feel free to comment.