Optimalworks Ltd web design, SEO, training and consultancy 

the site completely surpassed my expectations GT, AutoDirector, we build websites that work, my website is great - I love it MP, XL Autos, reach more customers, make more sales, it's more than I ever could have expected LM, Trade Cars UK, we can help your business grow, 99% of business now comes from my website GV, Bash Events, promoting standards throughout the web industry, your contribution added a level of quality we would otherwise have struggled to achieve PS, Hemsley Fraser
  1. home
  2. Services
  3. Portfolio
  4. Contact us
  5. blog

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

written by craig, 20 July 2007

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 <VirtualHost *:80> DocumentRoot C:/WebPages </VirtualHost> # localhost:81 - PHP4 <VirtualHost *:81> DocumentRoot C:/WebPages ScriptAlias /php/ "c:/php/php4/" Action application/x-httpd-php4 "/php/php.exe" AddHandler application/x-httpd-php4 .php </VirtualHost> # test:80 - PHP5 <VirtualHost *:80> ServerName test DocumentRoot C:/WebPages </VirtualHost> # test:81 - PHP4 <VirtualHost *:81> ServerName test DocumentRoot C:/WebPages ScriptAlias /php/ "c:/php/php4/" Action application/x-httpd-php4 "/php/php.exe" AddHandler application/x-httpd-php4 .php </VirtualHost> # test4:80 - PHP4 <VirtualHost *:80> ServerName test4 DocumentRoot C:/WebPages ScriptAlias /php/ "c:/php/php4/" Action application/x-httpd-php4 "/php/php.exe" AddHandler application/x-httpd-php4 .php </VirtualHost> # test4:81 - PHP4 <VirtualHost *:81> ServerName test4 DocumentRoot C:/WebPages ScriptAlias /php/ "c:/php/php4/" Action application/x-httpd-php4 "/php/php.exe" AddHandler application/x-httpd-php4 .php </VirtualHost>

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: <?php phpinfo(); ?>

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!

Obviously, you can now define further virtual domains – perhaps one for every website you create.

  • categories: php, web servers
  • tags: apache, php
  • trackback: http://www.optimalworks.net/blog/2007/web-development/php/php-apache-windows/trackback
  • bookmark: del.icio.us, digg, facebook, twitter, reddit, Furl, Spurl, Blinklist, Slashdot, Technorati, Yahoo!

8 comments:

  1. 20 March 2008 jose commented

    Hi, I just installed lastest version of XAMPP and I have to install to different sites, one in PHP4 and the other in PHP5.

  2. 20 March 2008 jose commented

    I did all you said but using
    but I get an error
    Access forbidden! 403
    If I comment them the error disappear…any idea how to give rights to that directory?
    thank

  3. 20 March 2008 Craig commented

    XAMPP includes PHP4 and PHP5, but it’s set up differently: you need to switch between them using php-switch.bat. See the XAMPP help page for more information. I suspect you can configure it for both PHP4 and PHP5, but the purpose of XAMPP is to give you a stable server environment without the need for configuration.

    My instructions above assume you are installing everything separately. It takes longer, but I’d recommend it if you want to learn more about the software or you want to optimise your installation. Apache has a reputation for being tough to configure but I think it’s a little undeserved.

  4. 22 February 2009 manyobvo commented

    Bless,

    The php5 installation is running perfectly but when i try the php4 installation I am getting a 400 Bad Request error. I am not sure wasup… I understand this can’t be a bug

  5. 23 February 2009 Craig commented

    Try running “httpd.exe -t” from the command line – it will show any errors in your configuration file.

  6. 24 April 2009 madmax commented

    :)
    Thank you, it works :)

  7. 15 November 2011 John commented

    I’m getting could not load
    syntax error on line 993 of (Path to httpd.conf)
    C php php5 php5apache.dll into server: the specified module could not be found.

  8. 15 November 2011 John commented

    Nvm I fixed the last issue but now I’m getting

    Only one usage of each socket address (protocol network address port) is normally permitted make_sock could not bind to address 0.0.0.0 port 80
    unable to open logs

add your comments
please enter your name
please enter your email address - it will not be published anywhere
please enter your website address
please enter your comments (all are moderated and checked for spam)

  • next article: Easy rounded corner graphics 
  • previous article: Apache 2, PHP, caching, and relative file path problems 
search
  • all popular (5)
  • courses (1)
  • general (4)
  • hardware (1)
  • software (31)
    • blogs (1)
    • free download (8)
    • web browsers (12)
    • web servers (2)
  • web development (42)
    • accessibility (4)
    • ajax (1)
    • css (3)
    • graphic design (4)
    • html (1)
    • javascript (11)
    • php (5)
    • seo (2)

tags

  • accessibility
  • ajax
  • analytics
  • apache
  • award
  • book
  • chrome
  • computing
  • css
  • design
  • developers
  • dom
  • download
  • editor
  • emulation
  • firefox
  • funny
  • google
  • graphics
  • hardware
  • html
  • ie
  • javascript
  • opera
  • php
  • png
  • portfolio
  • review
  • ruby
  • safari
  • security
  • seo
  • server
  • software
  • spam
  • ssl
  • standards
  • wordpress
  • xml
  1. site map
  2. home
  3. Services
  4. Portfolio
  5. Contact us
  6. blog
  7. RSS feed
  8. project survey

XHTML 1.0 | CSS 2.1 | WAI AAA | printer-friendly

©2012 Optimalworks Ltd, Devon, UK. Registered in England and Wales No. 5922205.

This page can be viewed at http://www.optimalworks.net/blog/2007/web-development/php/php-apache-windows