Thursday, July 15, 2010

How to run PHP 4 and PHP 5 at the same time on Apache for Windows

Whilst the majority of web applications are hosted on Linux, most developers use Windows. Installing Apache, PHP, and MySQL on your Windows PC is recommended and has become much easier. However, few developers can test their pages in PHP 4 and PHP 5 on the same machine at the same time – but it is possible…
Environment overview

This article will not describe every step in detail, but it should give you enough information to create your own multi-PHP web development environment:

1. We’ll define the domains http://test/ and http://test4/. Both will navigate to the same php files located in C:\WebPages\
2. PHP 5 will be installed as an Apache SAPI module and will be used for pages in the domain http://test/.
3. PHP 4 will be installed as a CGI binary. PHP files will be executed using PHP 4 by changing the domain to http://test4/ or accessing via port 81, e.g. http://test:81/

Note that the latest versions of PHP 5 run very slowly if you use the CGI binary. Defining it as a module solves this issue.
Installation ingredients

The freeware/open source software you’ll need is:

* Apache HTTP server for Windows. Stick with version 2.0.x – although 2.2 is available, PHP doesn’t work with it yet.
* The latest PHP 4 and PHP 5 Windows binaries. Although installers are available, I’d recommend you download the ZIP packages.
* Optionally, a copy of the Windows MySQL database.
* A decent text editor – I recommend Notepad++.

Both Apache and MySQL provide Windows installers, so just run them. All going well, you should then be able to navigate to http://localhost/ in your browser.

Extract PHP 4 to C:\php\php4 and PHP 5 to C:\php\php5. Follow the instructions for configuring your php.ini file, but ignore the Apache setup details for now – we’ll cover that below. One setting that may catch you out is extension_dir – for PHP 5 use: extension_dir = "C:\php\php5\ext"

and for PHP 4 use: extension_dir = "C:\php\php4\extensions\"

You can now uncomment any extensions you wish to use, e.g. extension=php_mysql.dll in PHP 5’s php.ini.
Windows system setup


Now all the files are in place, we’ll define the local domains test and test4. This is done by editing the hosts file, located in %WINDOWS%/system32/drivers/etc. Add the following lines: 127.0.0.1 test 127.0.0.1 test4

You can update your system using the command ‘nbtstat -R’, but you’ll need a reboot soon so don’t worry about it!

Now go to the Control Panel, open System, click the Advanced tab, and click Environment Variables. In the System variables section, click “Path” in the list followed by Edit. Add “;C:\php\php4;C:\php\php5″ to the end of the Variable value line.

Now reboot to make sure the settings are applied.
Configuring Apache

The Apache configuration file is normally located at %PROGRAMS%\Apache Group\Apache2\conf\httpd.conf You’ll need to locate and edit the following settings.

Set the server to listen on port 80 (the default) and port 81: Listen 80 Listen 81

Ensure your root folder is set correctly (note the forward slash): DocumentRoot "C:/WebPages"

Add php files to the directory index, e.g. DirectoryIndex index.html index.php

Just before the Virtual Hosts section, define PHP 5 as a SAPI module: # PHP5 module LoadModule php5_module "c:/php/php5/php5apache2.dll" AddType application/x-httpd-php .php PHPIniDir "C:/php/php5"

Finally, we’ll define the localhost, test, and test4 virtual domain settings for ports 80 and 81. Where PHP 4 is required (test4 domain or port 81), it is defined as a CGI binary that will override the PHP5 module.

NameVirtualHost *:80
NameVirtualHost *:81

# localhost:80 - PHP5


DocumentRoot C:/WebPages


# localhost:81 - PHP4


DocumentRoot C:/WebPages
ScriptAlias /php/ "c:/php/php4/"
Action application/x-httpd-php4 "/php/php.exe"
AddHandler application/x-httpd-php4 .php


# test:80 - PHP5


ServerName test DocumentRoot C:/WebPages


# test:81 - PHP4


ServerName test
DocumentRoot C:/WebPages
ScriptAlias /php/ "c:/php/php4/"
Action application/x-httpd-php4 "/php/php.exe"
AddHandler application/x-httpd-php4 .php


# test4:80 - PHP4


ServerName test4
DocumentRoot C:/WebPages
ScriptAlias /php/ "c:/php/php4/"
Action application/x-httpd-php4 "/php/php.exe"
AddHandler application/x-httpd-php4 .php


# test4:81 - PHP4


ServerName test4
DocumentRoot C:/WebPages
ScriptAlias /php/ "c:/php/php4/"
Action application/x-httpd-php4 "/php/php.exe"
AddHandler application/x-httpd-php4 .php


You’ll now need to restart Apache. It may be best to use “net stop apache2″ followed by “net start apache2″ on the command line. This will allow you to see any configuration error messages, but several other helpful tools are provided in the Apache program group.
Testing your environment

Create a new file phpinfo.php in C:\WebPages\. Edit the file and add the line:

Save it, then visit the following addresses in your browser:

* http://test/phpinfo.php – should show PHP 5
* http://test4/phpinfo.php – should show PHP 4
* http://test:81/phpinfo.php – should show PHP 4

And that’s all there is to it!

Ref: http://www.optimalworks.net/blog/2007/web-development/php/php-apache-windows

7 comments:

  1. Thanks a lot for sharing such a nice and informative information on php.....
    glad i found ur site...really a very helpful site.....
    by the way check out my collection of php interview questions from here: php interview questions

    ReplyDelete
  2. This is really nice. Thanks for sharing this article
    PHP training in Chennai

    ReplyDelete
  3. Thanks for sharing this informative blog to my vision, i love programming, have an expectation about your future blogs so keep sharing.
    Web design training in Chennai

    ReplyDelete

  4. This is a wonderful informative blog here i learned many things in my interested domain keep posting...
    JAVA Training in Chennai|JAVA Training Institutes in Chennai

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. Your posts is really helpful for me.Thanks for your wonderful post. I am very happy to read your post.very nice blogs!!! i have to learning for lot of information for this sites...Sharing for wonderful information.
    PHP Training in chennai | PHP Training chennai | PHP course in chennai | PHP course chennai

    ReplyDelete