PHP4 and PHP5 Side-by-Side Installation on Breezy

This how I got to install PHP4 and PHP5 under Ubuntu Breezy. This should also work in other distributions which have at least two different installation options for PHP. It is also possible for you to install PHP4 or PHP5 from source1 if your distribution does not provide with you options (i.e. installing the PHP4 or PHP5 package would result in installing CLI, CGI, and Apache module versions).

Update July 6, 2006: This tutorial has been tested and is known to work under Ubuntu Linux 6.06 LTS (aka “Dapper Drake”).

Under Ubuntu (or Debian), there are three options:

  1. PHP is installed as an Apache (or Apache2) module

  2. PHP is installed as a CGI binary

  3. PHP is installed as a CLI binary

The first two installation options are for Web-based applications. The third option is used if you want to use PHP as a scripting language for command line programs.

With a side-by-side installation, you can install one version of PHP as an Apache module and the other version as a CGI binary. If you like, you can also install them both as CGI binaries. It requires a bit more work (at the moment) to have them both installed as an Apache module since you would have to forgo your distribution’s package management system and install PHP4 and PHP5 from source.

Your choice if installation is entirely up to you. You can choose from one of the following setups:

A. PHP4 is installed as an Apache module; PHP5 is installed as a CGI binary

B. PHP5 is installed as an Apache module; PHP4 is installed as a CGI binary

C. both PHP4 and PHP5 are both installed installed as CGI binaries

The easiest route to take is to install these packages using apt-get. For my setup, I chose setup A: PHP4 as an Apache module and PHP5 as a CGI binary. This setup is ideal for virtual hosting environments where you want your customers to have PHP4 as the default PHP version and PHP5 as an available alternative.

To begin, install PHP4 as an Apache (or Apache2) module:

Apache 1.3.x:

$ sudo apt-get install libapache-mod-php4

Apache 2.0.x:

$ sudo apt-get install libapache2-mod-php4

That should install the core PHP4, which is next to useless without some extensions:

$ sudo apt-get install php4-curl php4-domxml php4-gd php4-mcrypt php4-mhash php4-mysql php4-pgsql php4-sqlite php4-xslt

Once you have installed all the modules you need, check that you have Apache or Apache2 up and running and verify that your configuration was updated:

Apache 1.3.x:

$ sudo kill -0 `cat /var/run/apache.pid` && echo "Apache is running."

Apache 2.0.x:

$ sudo kill -0 `cat /var/run/apache2.pid` && echo "Apache is running."

You should see the message “Apache is running”. If you see an error message instead, then you should try to start Apache first:

Apache 1.3.x:

$ sudo invoke-rc.d apache start

Apache 2.0.x:

$ sudo invoke-rc.d apache2 start

Once you have Apache up and running, check to see if you have successfully configured PHP4, you can use the HEAD Perl program from libwww-perl:

$ HEAD http://localhost/ | grep PHP
Server: Apache/2.0.54 (Ubuntu) mod_fastcgi/2.4.2 PHP/4.4.0-3ubuntu2 mod_ssl/2.0.54 OpenSSL/0.9.7g mod_perl/2.0.1 Perl/v5.8.7

Or, you can use the curl client from the curl package:

$ curl --head http://localhost/ | grep PHP
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
Server: Apache/2.0.54 (Ubuntu) mod_fastcgi/2.4.2 PHP/4.4.0-3ubuntu2 mod_ssl/2.0.54 OpenSSL/0.9.7g mod_perl/2.0.1 Perl/v5.8.7

If you see output similar to the ones above (contains the text PHP) then your setup is now working properly.

Next step is to install PHP5 as a CGI binary:

$ sudo apt-get install php5-cgi

This will install all the packages needed to have a core PHP5 installation. Again this would be next to useless without the extensions:

$ sudo apt-get install php5-curl php5-gd php5-mhash php5-mysql php5-pgsql php5-sqlite php5-xmlrpc php5-xsl

Once you are done installing all the extensions you need, it’s time to configure your Apache webserver:

Apache 1.3.x:

$ sudo gedit /etc/apache/sites-enabled/default

Apache 2.0.x:

$ sudo gedit /etc/apache2/sites-enabled/default

Add the following just before the line that says </VirtualHost>:

Action php5-script /cgi-bin/php5
AddHandler php5-script .php5

This will define a new content handler and default action for files with a .php5 extension. This directive will apply to your default virtual host.

Then restart Apache:

Apache 1.3.x:

$ sudo invoke-rc.d apache restart

Apache 2.0.x:

$ sudo invoke-rc.d apache2 restart

Try it out:

Apache 1.3.x:

sudo gedit /var/www/apache-default/phpinfo.php5

Apache 2.0.x:

sudo gedit /var/www/apache2-default/phpinfo.php5

Enter the following lines and save the resulting document:

<?php
phpinfo();
?>

Then with your browser, open the URL http://localhost/phpinfo.php5:

$ sensible-browser http://localhost/phpinfo.php5

If you see the auto-generated phpinfo page, then congratulations! You now have PHP4 and PHP5 installed on your server.

If you want to use setup B, just exchange php4 and php5 in the installation commands above and when you setup the Action and AddHandler directive, you should use .php4 instead of .php5.

If you want to use setup B, do not install PHP4 as an Apache module as instructed above. Instead install the CGI binary:

$ sudo apt-get install php4-cgi

And then proceed to install the PHP4 modules. Then setup the Action and AddHandler directives for PHP4:

$ sudo gedit /etc/apache2/sites-enabled/default

Add the following just before the line that says :

Action php4-script /cgi-bin/php5
AddHandler php4-script .php4

If you want the server to handle the .php extension as PHP4, add another line:

AddHandler php4-script .php

Restart Apache:

Apache 1.3.x:

$ sudo invoke-rc.d apache restart

Apache 2.0.x:

$ sudo invoke-rc.d apache2 restart

PHP CGI Binary Installation and suExec with Apache2

One of the great things when you have setup PHP to run as a CGI binary with Apache 2 is that you get the capability to run it under suExec. What this means is that if your script requires write privileges to a directory under the document root, you no longer have to make that directory world writeable. If you use Smarty with caching enabled, then you are probably familiar with this requirement.

suExec should already be installed when you installed Apache2, since it comes in the apache2-common package. All you need to do is enable it:

$ cd /etc/apache2/mods-enabled
$ sudo ln -s /etc/apache2/mods-available/suexec.load .

Then start and stop Apache2 (force reload may not be enough to enable it):

$ sudo invoke-rc.d apache2 stop
$ sudo invoke-rc.d apache2 start

Check that Apache2 has indeed loaded suExec:

$ cat /var/log/apache2/error.log|grep suexec
[Sat Dec 24 15:42:14 2005] [notice] suEXEC mechanism enabled (wrapper: /usr/lib/apache2/suexec2)

You should see something like the output above to indicate that suExec is now properly installed. If not, check the output of the following command:

$ ls -l /usr/lib/apache2/suexec2
-rwsr-x---  1 root www-data 10332 2005-12-06 00:36 /usr/lib/apache2/suexec2

Note that the setuid bit must be set as above. If it is not, then:

$ sudo chmod +s /usr/lib/apache2/suexec2

And stop and start Apache2 again. Check that you have suExec loaded and if it is, then proceed with the following:

$ cd /var/www
$ sudo cp /usr/lib/cgi-bin -r
$ sudo chown www-data:www-data cgi-bin -R
$ sudo chown www-data:www-data apache2-default -R
$ sudo gedit /etc/apache2/sites-enabled/default

Insert the following lines before the line that says </VirtualHost>:

<IfModule mod_suexec.c>
    SuexecUserGroup www-data www-data
</IfModule>

Then restart Apache2:

$ sudo invoke-rc.d apache2 restart

To test your new suExec setup, try the following:

$ sudo gedit /var/www/apache2-default/suexectest.php5

Enter the following and save the resulting file:

<?php
$fp = fopen('example.txt', 'a+');

$hello = "Hello World!\n";

fwrite($fp, $hello, strlen($hello));

fclose($fp);
readfile('example.txt');
?>

Then on the command line enter the following:

$ sudo chown www-data:www-data /var/www/apache2-default/suexectest.php5
$ sensible-browser http://localhost/suexectest.php5

You should see the phrase Hello World! in your browser window to confirm that suExec works.

Note that the following directives are used in a <code<VirtualHost container2:

SuexecUserGroup
Action
AddHandler

So this means if you tweak the parameters a bit, you can enable it for other virtual hosts on your server. To enable suExec for a virtual host, remember the following:

  1. The virtual host document root and cgi-bin must be under /var/www/
/var/www/customer/html and /var/www/customer/cgi-bin

  1. The owner and group of the document root and cgi-bin directories must be the same as the owner and group you set with SuexecUserGroup for that virtual host.

    e.g.: If you have SuexecUserGroup ucustomer1 gcustomer1, then:

$ sudo chown ucustomer1:gcustomer1 /var/www/customer -R

NOTES:

1 Article is already in progress. I will post it here as soon as I find the time (and energy) to finish it.

2 You can also have the Action and AddHandler directives in your .htaccess file to provide per-directory configuration. However, your virtual host must be configured to AllowOverride at least FileInfo.

Leave a Reply

Comments are moderated by the administrator. If this is your first time posting a comment, your comment will go to a moderation queue and it may take a while for your comment to appear. Or it may get deleted.