Dual-installing PHP: Running PHP 5 and 4 on the same local, Windows-based, Apache, server

In previous guides, we installed PHP 4.4.2 and later moved our installation to a different folder. This time, we'll be installing the current release of PHP 5 (5.1.4) so that we can still switch back to PHP 4.4.2 if we'd like.

Downloading PHP 5.x

The current version of PHP 5.x is 5.1.4, so we'll begin by downloading that from PHP.net. We’ll want to download the (Windows Binaries) zip file, even though it is significantly larger in size than the installer (the zip file is almost 9 MB, compared to less than 3 MB for the executable), but allows us a deal more flexibility.

Installing

Once you've downloaded the zip file, extract the contents to a folder called c:\php5\. Once done, php.exe will have the full path of c:\php5\php.exe.

As with the PHP 4 installation, the install.txt file will detail how to install PHP 5. However, since you're using this guide, you shouldn't need to consult it. As with our PHP 4 installation, we'll be installing PHP 5 as a module, instead of as CGI binary (again, you can do this instead, if you so desire).

Start by backing up all of your httpd.conf files, in C:\Program Files\Apache Group\Apache\conf (if using the defaults). Since these work, add '_php4' to each of these backups.

For example, I have three httpd.conf files that I'm backing up; httpd.conf, httpd_CFMX6.1.conf, and httpd_CFMX7.01.conf. I'll be creating copies of each of these files, and calling them httpd_php4.conf, httpd_php4_CFMX6.1.conf, and httpd_php4_CFMX7.01.conf. This way I know that these use PHP 4. Once we modify the httpd.conf file(s) below, it may be a good idea to create backups and then call name them with '_php5'.

For the next steps, I'll be assuming that you've already installed PHP 4.

Begin by doing a search for

LoadModule php4_module "c:/php4/php4apache.dll"

Change this to

LoadModule php5_module "c:/php5/php5apache.dll"

Now do a search for

AddModule mod_php4.c

And change this to

AddModule mod_php5.c

Save httpd.conf. Next, head over to c:\windows\ and find the php.ini file. Rename this file to php_php4.ini (to keep with our naming standard above). We're renaming the original file, in this case, because we actually want to copy the file php.ini-dist from the c:\php5 folder to the c:\windows folder, and rename it to simply php.ini.

Once you've renamed the file, open it up. Once the file has been opened, do a search for doc_root and change it from

doc_root =

to

doc_root = "c:\home"

Or where your document root is for Apache.

Now do a search for the following line

;session.save_path = "/tmp"

And change that to your temporary PHP folder (in my previous guide, it ended up being

session.save_path = "c:/windows/temp/temp_php"

Make sure you remove the semi-colon from the beginning of the line to uncomment this line. Make sure that this folder exists.

Finally, search for the following line

extension_dir = "./"

and change it to

extension_dir = "c:\php5\ext\"

If you've installed MySQL (this page also contains a script to check the connection), you can enable access by uncommenting the following line

;extension=php_mysql.dll

You'll also want to copy libmysql.dll from c:\php5\ to c:\windows\system32\.

If you've installed PostgreSQL (this page also contains a script to check the connection), you can enable access by uncommenting the following line

;extension=php_pgsql.dll

Save your changes to the php.ini file, and close out of the file. You may want to create a copy of this file as php_php5.ini - this way you have both a php4 and php5 file.

Finally, in c:\php5\, copy php5ts.dll into the c:\windows\ folder.

Restart Apache, and assuming it starts, hit the phpinfo.php page (which contains the following lines).

<?php
phpinfo();
?>

If you visit this page, you should be told that you're running PHP 5.1.4. You may notice that Zend Optimizer is no longer listed as being installed.

If you made a copy of the php.ini file for PHP 4 (php_php4.ini is what I recommended), then you can copy the last four lines from this file, which are as follows (I'm including the new line as a line the new line is absent from the code below, but it sits immediately before the first line below)

[Zend]
zend_extension_manager.optimizer_ts="C:\Program Files\Zend\ZendOptimizer-3.0.1\lib\Optimizer-3.0.1"
zend_extension_ts="C:\Program Files\Zend\ZendOptimizer-3.0.1\lib\ZendExtensionManager.dll"

Paste these lines at the end of your php.ini (and php_php5.ini file, if necessary) file. Restart Apache and Optimizer 3.0.1 should display on the phpinfo() page.

Until then, verify that you have backups of the files that we modified in this guide; httpd.conf and php.ini. In a later guide, or update, I'll detail how to setup a batch file to automatically move the necessary files to switch from PHP 4 to PHP 5, and vice versa (again, for updates, leave a comment).

To manually change from PHP 4 to PHP 5, and vice versa, simply delete the existing httpd.conf file. Next, make a copy of the httpd_php4.conf or httpd_php5.conf file, and rename it to httpd.conf. Do the same thing for the php.ini file. Finally, restart Apache. As long as you keep your backup copies, you can switch back and forth fairly easily. Again, a future batch file will make this much easier, by allowing one file to do all the work for you.

The batch files

Below is code for two batch files. Lines beginning with === should not be entered into the batch file - they merely contain the name of the file. Note that you may need to make some modifications, based upon where you installed programs and files.

c:\home\zblank.txt is simply a blank file. Including blank lines, each batch file contains 12 lines (2 of which are blank).

===_start php4.bat

net stop "Apache"
DEL "c:\home\php4 is ready.txt"
DEL "c:\home\php5 is ready.txt"
COPY "c:\windows\php.ini" "c:\windows\php_backup.ini"
COPY "c:\windows\php_php4.ini" "c:\windows\php.ini"

COPY “C:\Program Files\Apache Group\Apache\conf\httpd_php4.conf” “C:\Program Files\Apache Group\Apache\conf\httpd.conf” COPY “C:\Program Files\Apache Group\Apache\conf\httpd_php4_CFMX6.1.conf” “C:\Program Files\Apache Group\Apache\conf\httpd_CFMX6.1.conf” COPY “C:\Program Files\Apache Group\Apache\conf\httpd_php4_CFMX7.01.conf” “C:\Program Files\Apache Group\Apache\conf\httpd_CFMX7.01.conf”

net start “Apache” COPY “C:\home\zblank.txt” “C:\home\php4 is ready.txt”

===_start php5.bat

net stop "Apache"
DEL "c:\home\php4 is ready.txt"
DEL "c:\home\php5 is ready.txt"
COPY "c:\windows\php.ini" "c:\windows\php_backup.ini"
COPY "c:\windows\php_php5.ini" "c:\windows\php.ini"

COPY “C:\Program Files\Apache Group\Apache\conf\httpd_php5.conf” “C:\Program Files\Apache Group\Apache\conf\httpd.conf” COPY “C:\Program Files\Apache Group\Apache\conf\httpd_php5_CFMX6.1.conf” “C:\Program Files\Apache Group\Apache\conf\httpd_CFMX6.1.conf” COPY “C:\Program Files\Apache Group\Apache\conf\httpd_php5_CFMX7.01.conf” “C:\Program Files\Apache Group\Apache\conf\httpd_CFMX7.01.conf”

net start “Apache” COPY “C:\home\zblank.txt” “C:\home\php5 is ready.txt”

View all of the steps to creating a local Web server, for development.