PrimitiveType

Compiling PHP 4 from source on Ubuntu Linux 7.10 Gutsy


This article looks at compiling PHP 4 on Ubuntu Linux. The reason for compiling from source is that PHP 4 is no longer in the repository for Ubuntu (I'm using Ubuntu 7.10, or "Gutsy"), and a project I'm working on specifically requires PHP 4 and not PHP 5. The web server with which PHP is being used is Apache 2. Additionally, I have configured PHP to work with MySQL and PostgreSQL database systems.

Getting PHP 4

Locate and download the specific version of PHP 4 that you want from http://www.php.net/releases/ (I had to download 4.3.11 for technical reasons). Once downloaded, unpack the source code into a directory such as /usr/src/ (so in my case I ended up with a directory called /usr/src/php-4.3.11).

NOTE: I used the Synaptic package manager that comes with Ubuntu to install everything mentioned on this page apart from the PHP 4 source code itself.

Dependencies

To get the build process working, I had to install a component called Flex (listed simply as Flex in Synaptic).

To get PHP to work with Apache, I had to install a package called apache2-prefork-dev via Synaptic. This includes a file called /usr/bin/apxs2 which is referred to in the configure command below.

To get PHP to work with MySQL I also had to install some MySQL development files, which are listed as libmysqlclient15-dev in Synaptic. I think this download includes C header files needed by the PHP MySQL extension.

To get PHP 4 to compile with the latest version of libcurl, I had to install some libcurl development files, listed in Synaptic as libcurl4-gnutls-dev, and then dip into the PHP source code and make a couple of modifications to /usr/src/php-4.3.11/ext/curl/curl.c (as PHP 4.3.11 makes references to constants no longer in the new version of libcurl). The changes to curl.c that I made were:

  • Replacing
    REGISTER_CURL_CONSTANT(CURLOPT_FTPASCII);
    with
    REGISTER_CURL_CONSTANT(CURLOPT_TRANSFERTEXT);
  • Commenting out
    REGISTER_CURL_CONSTANT(CURLOPT_PASSWDFUNCTION);

Of course, Apache 2, MySQL and PostgreSQL will all need to have been installed too if you plan to use them!

Building PHP

In the example configure command that follows, I have listed the same options I use, which is by no means an exhaustive list; there are plenty more options that you can specify, though some may require that you configure other parts of your system too.

Open a terminal and change to the directory you just created (e.g. /usr/src/php-4.3.11).

  • - Run configure:
    $ sudo .configure --with-apxs2=/usr/bin/apxs2 --with-config-file-path=/etc \ --with-mysql=/usr --with-pgsql=/usr --with-gettext --enable-bcmath \ --with-curl --enable-mbstring --enable-mbstr-enc-trans --enable-zend-multibyte
  • - Run make:
    $ sudo make
  • - Run make install:
    $ sudo make install
  • If you make mistakes or want to change things, go back and start again, running make clean before running make:
    $ sudo make clean

Testing PHP

If you get no error messages, try running PHP from the console to see if it works. Using the --version option should show you some information about the version of PHP you are running:

$ php --version PHP 4.3.11 (cli) (built: Apr 10 2008 11:04:01) Copyright (c) 1997-2004 The PHP Group Zend Engine v1.3.0, Copyright (c) 1998-2004 Zend Technologies

Next try opening a PHP page from your localhost web server. Apache 2 will need to be started, but in case it is already running, stop & start it with these commands (this may be specific to Ubuntu):

$ sudo /usr/sbin/apache2ctl stop $ sudo /usr/sbin/apache2ctl start

or

$ sudo /usr/sbin/apache2ctl restart

Create a test PHP page under /var/www/ and see if it works. For example, if I had created a page called "test.php" under /var/www/, I could open it in a web browser by going to http://localhost/test.php. If everything is working correctly, you should see the output of your PHP script and NOT your PHP source code (whether right there in the browser or as a file you are prompted to download)!