Installing PHP 4 and PHP 5 concurrently on one WinXP computer, with a single Apache 2 installation, (both on the same, good old port 80)
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:
- A single Apache 2 installation
- PHP 4 to work on localhost, port 80, running as a Apache module,
- 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

Comments
Either way, good writeup. :) I'll be sure to link some people to it.
Its out 'off-the-box-no-hassle-no-learning' solution...
We can do better than that... at least i hope we can... :)
For example: To load extension you have to load dll from same build as php and because of it it's imposible to make working it properly - you can choose 1 that should work only. (this case can be fixed by loading both modules in 1 ini file, but it throws very much exceptions into log file and it's not solving the need of having 2 separate files)
Is it only my problem or is it same as yours?
Has someone solution? 'PHPIniDir' is not working due to max 1 existence per configuration tree. (or maybe I'm only unable to set it properly).
But I have launched PHP5 using Apache Module instead of PHP4. Here is the VirtualHost config I have applied :
<VirtualHost *>
ServerName php4.neovov.com
DocumentRoot "C:/www/php4"
ScriptAlias /php4-bin/ "C:/Program Files/Apache Group/PHP4/"
SetEnv PHPRC /php4-bin/
Action php4-script /php4-bin/php.exe
AddHandler php4-script .php
ErrorLog logs/error-php4.log
CustomLog logs/access-php4.log common
<Directory "C:/Program Files/Apache Group/PHP4/">
Allow from all
</Directory>
</VirtualHost>
I had to Allow access for "C:/Program Files/Apache Group/PHP4/" because my Apache configuration is very restrictive.
It work very well !
different php.ini files.
That is a must for this solution to work properly!
on my machine when i run phpinfo()
for php4:
Configuration File (php.ini) Path C:\WINDOWS\php.ini
for php5:
Configuration File (php.ini) Path d:\php5\php.ini
this is done with the 'magic' SetEnv PHPRC d:/php5 command...
Read the tutorial and try to first do everything like in is said,
and after you separate the php4 and php5 so they use different
config files, then continue with customizations...
have fun :)
Solution:
1. delete php4 and php5 directories from PATH environment variable. (maybe not needed, but i didn't find any problem without it)
2. move php.ini file from php4 directory to windows directory. If you keep this file here, php5 takes it unlogicly from here.
It really worked when I followed it properly.
But when I've installed Zend Studio Package with Zend Studio Server and Client, the php5 installation as a virtual host again referes to the php.ini file located at C:\WINNT directory not to the c:\php5 directory.
What is the workaround for this? How I can make this work with zend studio package.
I've documented this quite extensively in the past, and have posted a few scenarios.
I got to the point where I got fed-up going to the php.net website and continually doing the same old thing, and therefore created a JScript based application as I wanted total control over every single version of PHP, including minor versions. This resulted in me using a a website with an XML file that contained a series of CVS / SVN commands that the app sockets via AJAX then uses the Windows Script Host (WSH) to write config files to the filesystem, run cvs for php-web, extract the zip files and execute the installation of apache as a service.
This solution gives you total control over every single version of PHP and also allows you to maintain your other CVS / SVN projects, by simply running it as a scheduled service, then you can get on with your life, and run the PHP version as service. And downloads several others, as the idea was to create a website that each user could create there own XML...
http://blog.ajohnstone.com/archives/automate-php-install-on-windows/
You can download it from here.
http://blog.ajohnstone.com/wp-content/Build.zip
Others.
MySQL Install Multiple Instances
http://blog.ajohnstone.com/archives/mysql-install-multiple-instances/
Apache IIS Multiple Address on Port 80
http://blog.ajohnstone.com/archives/apache-iis-multiple-address-on-port-80/
Parallel Instances of PHP on Windows
http://blog.ajohnstone.com/archives/parallel-instances-of-acronym-titlehypertext-preprocessorphpacronym-on-windows/
This works on Apache/1.3.37 (Win32) PHP/4.3.10
I changed the virtualhost so that the document root is the same for both php4 and php5 but the listen port is changed to 8080 for php5
Also The phprc variable works perfectly.
Thank you for this tutorial again.
Now all i have to do is chage the port to find if my application works properly in php5.
got both the version running in my existing C:Program Files folder.. in 20 minutes straight and one restart...
thanks for the article
Thank you
Worked for me, I did the opposite installing php4 as a subdomain.
Thanks for the education.
Hong Ngoc