Compiling PHP 5.2 for Ubuntu

There are a number of ways of installing PHP 5.2 on Maverick using Ubuntu’s existing package management framework or pre-compiled binaries, some of these methods are dotted across the internet on blogs and such.

I found most of these methods a little bit messy and the risk of unintended consequences stemming from altering the default package management for PHP doesn’t sit well with me. And besides, this is GNU/Linux, we can take the source of PHP and just compile it for our own machines!

I’ve already used the built in package manager to install Apache and some databases that I use so all I need to do is get the PHP source, compile it with the correct options and then get it working with Apache. Easy.

Please note: many of the commands below require sudo privileges. You can get a persistent sudo by using

$ sudo -i

From now until you use logout, you’ll be root for all the commands that you run.

Step 1. Get the PHP source

We can download the PHP source at http://www.php.net/downloads.php and at the time of posting the version I’m choosing is PHP 5.2.17. Download it and copy it somewhere useful

$ cp ~/Downloads/php-5.2.17.tar.gz /usr/local/src/.

Step 2. Prepare the source

If you haven’t compiled software on your Ubuntu machine you should head over to this useful primer on the Ubuntu community documentation website. (It has some handy hints on how to deal with dependencies and other things so if you’re following along at home, read that page and then come back).

Next, unpack the PHP source tarball:

$ cd /usr/local/src
$ tar xzvf php-5.2.17.tar.gz

Now all the relevant files are in a folder called php-5.2.17. The most useful documentation file I found was INSTALL. Have a look at that using your favourite text editor

$ gedit php-5.2.17/INSTALL

The INSTALL file recommends looking at the options for compiling PHP by using configure –help, and you can save the output of that command to a file for easy reference.

$ cd php-5.2.17
$ ./configure --help > ~/php-configure-help.txt

Open up that output and have a look at all the options that you can compile PHP with. When it comes to the information about extensions for PHP there is a difference between built-in extension support and shared extensions. As far as I can tell compiling with shared extensions would create additional *.so files and require those extensions to be loaded via directives in the php.ini file. I’m not too fussed about shared extensions because at the moment I don’t know any better.

After reading the configure help output here’s the command I come up with:

$ ./configure --prefix=/opt --with-apxs2=/usr/bin/apxs2 --with-curl=/usr/lib --with-pgsql --with-pear --with-mysql --with-gd

To break it down…

  • –prefix=/opt: I just like installing anything that I’ve compiled from sources into the /opt directory so I know it’s not part of some other package management.
  • –with-apxs2: creates a PHP module for Apache 2 which gets installed to /etc/apache2/mods-available
  • –with-curl: I just thought it might be good for PHP to have cURL support. Not sure if I’ve ever used it.
  • –with-pgsql, –with-mysql: so that PHP can work with the MySQL and PostgreSQL databases (which were previously installed).
  • –with-pear: I think PEAR is installed by default even without this option.
  • –with-gd: lets PHP do things with graphics.

After running that I get an error about a missing file or package or something and I go looking for that file using apt-file search.

# configure: error: xml2-config not found. Please check your libxml2 installation.
$ apt-file update
$ apt-file search xml2-config

Apt-file search finds the file I need in the package libxml2-dev

$ apt-get install libxml2-dev

It’s a matter of rinsing and repeating with the configure command until we don’t get any more errors. So run ./configure with the same options. The next error I get is:

# configure: error: Please reinstall the libcurl distribution - easy.h should be in <curl-dir>/include/curl/

So if we use apt-file search again we should find the package we need.

$ apt-file search easy.h
# libcurl4-gnutls-dev: /usr/include/curl/easy.h
$ apt-get install libcurl4-gnutls-dev

Try configure again…

$ ./configure --prefix=/opt --with-apxs2=/usr/bin/apxs2 --with-curl=/usr/lib --with-pgsql --with-pear --with-mysql --with-gd
# configure: error: libpng.(a|so) not found.
$ apt-file search libpng.so
# libpng12-dev: /usr/lib/libpng.so
$ apt-get install libpng12-dev

…and again.

$ ./configure --prefix=/opt --with-apxs2=/usr/bin/apxs2 --with-curl=/usr/lib --with-pgsql --with-pear --with-mysql --with-gd

This time I don’t get any errors, I just get a lovely “Thank you for using PHP” at the end of the output. Now we can compile the software using make.

Step 3. Compile the source

It’s as easy a single command:

$ make

… which takes forever. The output of the make says to run “make test” so lets do that. After endless screens of horrid output and 6950 tests later, make test is finished and it tells me that I may have found a problem with PHP. Some of the 6950 tests that got caried out failed and I get a prompt asking me if I want to send a report to the PHP maintainers about the results, I give my email and that’s the end of that. It doesn’t seem like that potential problem is anything serious so I press forward.

Step 4. Install the compiled package using Checkinstall

The last step is to install the compiled software and in the PHP INSTALL file it says to run “make install” but there’s a special tool that we can use call Checkinstall. If we look up the package on Synaptic we find the following:

CheckInstall keeps track of all the files created or modified by your installation script (“make install””make install_modules”, “setup”, etc), builds a standard binary package and installs it in your system giving you the ability to uninstall it with your distribution’s standard package management utilities.

Sounds good. Install it if you haven’t already and then lets use it on our newly made PHP.

$ checkinstall

Now this takes us through a very sexy process of building a .deb package. It’s all very user-friendly and provides great feedback. Change any details you feel are necessary and move forward. Once checkinstall finishes we get the following:

**********************************************************************
 Done. The new package has been installed and saved to
 /usr/local/src/php-5.2.17/php_5.2.17-1_i386.deb
 You can remove it from your system anytime using: 
 dpkg -r php
**********************************************************************

So PHP is now installed and we also have a redistributable .deb package to boot.

Step 5. Test PHP

We can check on the status of the installed package by running

$ dpkg-query -l php

It’s probably a good idea to also create a phptest.php page and see if PHP and Apache are working together. My default Apache2 website path is /var/www/

1 <?php
2 phpinfo();
3 ?>

The testphp.php page with the output of phpinfo()

That’s it, we’ve now got PHP 5.2.17 running on our Ubuntu Maverick install. I’ve also posted the .deb package online if you’d like to download it for your own use, but it comes with no guarantees and if it somehow eats your computer and then attacks your family I really can’t help you. Nonetheless here is the php_5.2.17-1_i386.deb for download.

If you’ve got any suggestions or you’d like to point out glaring errors please let me know in the comments.

Добавить комментарий

Ваш e-mail не будет опубликован. Обязательные поля помечены *