There are three kinds of people: The ones that can count, and the ones that cannot. :)

Ok, that did not work, so I'll start again (no funny stuff this time, I promise):

Currently in PHP community there are many people that are still programming their code in PHP 4. PHP 5 is not yet completely adopted by the programmers (or hosting companies) in spite of having been around for quite some time now. This could be explained by many reasons, but one thing is sure,the fifth version of this great programming language is much better than all the previous ones, and sooner or later it will become ubiquitous. But what to do until then? Is it possible to have the 'best of both worlds'?

Is it possible to have the both versions of PHP installed on the same computer without conflicts, so one can maintain old PHP 4 projects, and develop new PHP 5 code? The answer is, yes.

Here's my solution:

Few days ago I tried to install both versions on a single computer running Windows XP. First (as always) I consulted our all-knowing friend Google.com. I found millions of links with all kinds of tutorials, suggestions and forum posts on how to do the similar thing to the one I wanted to do. But I could not find the solution that was totally acceptable to me. Most of the examples showed how to install PHP 4 and PHP 5 on a single computer, but on a different ports, one as a apache module on port 80, and the other one as CGI/FAST CGI on port 83 (for example). I did not liked that idea. It was complicated to put the port numbers in URLs.

Then there were solutions that were based on the idea that PHP 5 pages should have .php5 extension and the other pages (PHP 4 ones) would remain .php. This was better but not good enough…. So I searched again. And again. And then I got really angry, and decided to do this by myself, exactly the way I wanted it to be done.

Here are the things I wanted to accomplish:

  1. A single Apache 2 installation
  2. PHP 4 to work on localhost, port 80, running as a Apache module,
  3. PHP 5 to work on the same port (80), but only on the specific sub domains of the same localhost Apache instalation

No more, no less…

After 2 days of try / fail iterations, I managed to solve the problem. This tutorial will show you how to acheieve this on your computer. (That is if you are interested to do so).


PREPARATIONS:

First of all you must clean your computer from all the old Apache and PHP installations. Make a backup of the current /htdocs/ directory to save your current projects and completely uninstall the Apache and PHP installations. By this I mean a TOTAL clean-up. You must delete ALL the Apache and PHP DLLs and config files. If you do not do that you will end up pulling hair from your head, trust me on this one…

After this, make sure you reboot your computer, and create a new Apache 2 installation. I suggest you choose CUSTOM installation, and install it to the D:/Apache2/ directory.


THE WORKS:

Now we can download the latest PHP 4 and PHP 5 packages and unzip them to a separate directories. For example D:/php4/ and D:/php5/ respectively. After this it is very important to add those two directories to the PATH environment variable of your Windows installation.

You can do that by going to:

START MENU->SETTINGS->CONTROL PANEL->SYSTEM->ADVANCED->ENVIRONMENT VARIABLES,

and editing the PATH variable, and adding those two directories to the list, separated by a ';'

After another reboot (just to be on the safe side), we can start with configuring PHP 5 as a CGI/FAST CGI. First we need to setup a Apache sub domain for PHP 5. It can be php5.localhost or whatever you want it to be. To do this, create a subdir /php5 in htdocs dir of your Apache 2 installation. Edit the httpd.conf file (in Apache /conf directory) and enter these lines at the bottom of the file:

NameVirtualHost *:80 <VirtualHost *:80> ServerName localhost DocumentRoot "d:/apache2/htdocs" </VirtualHost> <VirtualHost *:80> ServerName php5.localhost DocumentRoot "d:/apache2/htdocs/php5" ServerAdmin webmaster@webmasterland.com DirectoryIndex index.php index.html index.htm SetEnv PHPRC d:/php5 ScriptAlias /cgi-bin/ "d:/php5/" Action php5-script /cgi-bin/php-cgi.exe AddHandler php5-script .php .html ErrorLog logs/error5.log CustomLog logs/access5.log combined </VirtualHost>

This will instruct the Apache that we have a sub domain php5 on our localhost and that PHP 5 installation from d:/php5 dir should work only on this sub domain. The SetEnv PHPRC command is very important as it tells the Apache that this PHP installation will use php.ini from that dir (d:/php5), and not the standard one from C:/windows directory. Last, don't forget to copy the recommended PHP 5 config file php.ini to the d:/php5 directory and restart the apache service.

Before we can test this in our browser, we must edit the Windows host file so our sub domain starts working. Go to the C:\WINDOWS\system32\drivers\etc\ directory and open the hosts file. By default there should be a single line there:

127.0.0.1 localhost

You should add another line under it:

127.0.0.1 php5.localhost

Now you can test our PHP 5 installation by creating a simple index.php file with code:

<?php phpinfo(); ?>

and placing it inside the d:/apache2/htdocs/php5/ directory and opening it from our browser.

The URL is http://php5.localhost/index.php

if you did everything right you should see a PHP 5 info page. Yaaahuuuuu!!! All the scripts you place inside that subdir and open via this sub domain will be interpreted by PHP 5.

Now we must instruct Apache so that all the other PHP scripts that are not on our 'special' sub domain are interpreted by the PHP 4 interpreter (as a Apache module). Here is how to do that:

Edit your Apache httpd.conf file and enter the following lines:

Under the LoadModule config lines add this one:

LoadModule php4_module "d:/php4/php4apache2.dll"

Under the AddType lines add this line:

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

Don't forget to copy the recommended PHP 4 config file php.ini to the c:/windows/ directory (or whatever dir you keep your Windows).

Restart the Apache and test the PHP 4 installation by copying the same index.php file you used for testing PHP 5 to the root of your domain (htdocs dir) and open it from your browser. http://localhost/index.php. If you set everything up correctly, you should see the PHP 4 info page.

This method has been tested multiple times on multiple Windows 2K and XP machines and works 100% with Apache 2.2.2 , PHP Version 4.4.3RC3-dev, and PHP 5.1.4.

I'm sure it can be done with other version combinations also, maybe with some minor modifications. This technique will most likely work on Linux servers as well, but I have not tested that configuration.

Just arm yourself with confidence and patience (cause you will need it) and give it a try.

...and good luck.

Slobodan Pavkov
webmaster [at] aspdotnetfaq.com