Run 2 Versions of PHP as Apache Modules

I have searched hundreds of forum threads, blogs and info channels searching for the ultimate way to run PHP4 and 5 together on an Apache web-server, so I can test and play at the same time. I am on Windows at home. My web server is Linux, but what the hell, this could be close enough for me. This article was originally published on 227net.

The Aim

Have multiple instances of Apache 2.0.xx (win32) running
PHP 4.3.x available
PHP 5.0.x available

Table of Contents

  1. Introduction
  2. Contents
  3. Background information
  4. First steps
  5. httpd.default.conf
  6. php.ini
  7. Apache as a windows Service
  8. Go go go!!
  9. Closing

Back to top

Just Do It

After failed attempts at running PHP5 as a CGI with PHP4 running as a module, I followed the path of services, and saw some comments about being able to run multiple instances of Apache. Perhaps this was the way forward. With lots of snippets of information at hand, the PHP and Apache manuals open in tabs (Firefox), command prompt running, windows explorer open and point to C:\Program Files\Apache Group\Apache2 and a large cup of coffee I got started.

What You Need

Apache

This article makes the assumption that you already have Apache and are running it as a service currently. I used 2.0.48 (win32) installer. If you haven't already installed it, please RTFM, don't blame me. Please make sure that you stop the service before proceeding - just to be on the safe side.

PHP

It also assumes that you have already downloaded PHP.
If you don't, then you can just follow the steps for PHP 5x and change names accordingly. the article doesn't go in depth into
PHP installation,
only a couple of suggestions - the rest is up to you!

WindowsXP or 2000 with admin rights

The following steps work on Windows XP and 2000. You need admin rights for the PC, this is essential.

Some savvy

It does help if you can use ms-dos a little, can copy & paste and make back-ups first!

Back to table of contents

Let's Get Started

So we have made the basic installations, we have Apache running. PHP 4x and 5x have been downloaded, but ini files not changed yet.
The base installation directories are as follows:

C:\Program Files\Apache Group\Apache2
C:\PHP\php-4.3.10-Win32
C:\PHP\php-5.0.3-Win32

Files We Will Create Or Edit

Create

  • 1 additional service - Apache8000 (to run concurrently with Apache2 [default installation])
  • Additional error and access logs (optional)

Edit Files

  • C:\PHP\php-4.3.10-Win32\php.ini-recommended (rename & edit)
  • C:\PHP\php-5.0.3-Win32\php.ini-recommended (rename & edit)
  • C:\Program Files\Apache Group\Apache2\conf\httpd.default.conf (rename & edit)

Back to table of contents

httpd.default.conf

First Steps

If you have Apache2 running already, go to the next step!

Open Windows Explorer and go to your Apache directory and look for your httpd.default.conf file. It will be located somewhere like C:\Program Files\Apache Group\Apache2\conf. Highlight or select that file, then make a copy by using
Ctrl+C, then Ctrl+P to paste. Right-click on your copy of httpd.default.conf file and rename it to httpd.conf. Now we can open the file and make the relevant changes.

httpd.conf

We need to ensure php module is running. To do this we need to locate the Dynamic Shared Object support section of file we have open. If you have the file open in notepad, you can use Ctrl+F and search LoadModule just at the end of section 1. Add the following 2 lines, changing the location to match your systems set-up.

# Add the following below the other LoadModule settings
LoadModule php4_module "c:/php/php-4.3.10-Win32/php4apache2.dll"
# The following makes Apache search this directory first for the php.ini file
PHPIniDir "c:/php/php-4.3.10-Win32"

Next we make sure that anything with a php extension .php runs as an application. After the AddCharset section there are some applications added, for example AddType application/x-gzip .gz .tgz. Below this, write the following:

# You can add other extensions here too, example below
AddType application/x-httpd-php .php
# AddType application/x-httpd-php .php .html

You may want also to have index.php as a possible default directory index. If so, then Ctrl+F and search DirectoryIndex and it whatever filename(s) you want.

# Order is important
DirectoryIndex index.php index.html index.html.var

The previous steps were all PHP releted. We have two major Apache tasks still to complete. In this example for PHP4
we will just check, we will make the change in php5.httpd.conf. We need to check what port Apache is listening to. By default it is port 80. Ctrl+F and search for Listen and verify that it is 80. You can it if you want, it is not necessary though. The second task it to set a unique Pidfile for the server. From the httpd.conf file: "The file in which the server should record its process identification number when it starts."

# The following exists already, rename
# PidFile logs/httpd.pid
PidFile logs/httpd.pid

That is essentially it. Now we will do the very same for php5.

Back to table of contents

php5.httpd.conf

OK, we haven't made this one yet, but follow the first few steps as we did with httpd.conf.

  1. Locate httpd.default.conf
  2. Copy & paste the file to the same directory
  3. Rename the file from copy of httpd.default.conf to php5.httpd.conf
  4. Open it and add the following lines following previous instructions
  5. Make sure that you adjust the paths accordingly
# Add the following below the other LoadModule settings
LoadModule php5_module "c:/php/php-5.0.3-Win32/php5apache2.dll"
PHPIniDir "c:/php/php-5.0.3-Win32"
# Check further on if AddType is set for PHP
AddType application/x-httpd-php .php
# Check further on that DirectoryIndex is set up
DirectoryIndex index.php index.html index.html.var
# Listen 12.34.56.78:80 (example) - Change to a different port than php4.httpd.conf
Listen 8000
# PidFile logs/httpd.pid
PidFile logs/php5.httpd.pid

OK, hopefully that was pretty simple. You may want to change the extensions to .php5 to differentiate between you php code - that is up to you. Do not start Apache yet - it will fail as the php.ini files haven't been created yet. That is the next step.

Back to table of contents

php.ini

C:\PHP\php-4.3.10-Win32

Using windows explorer, navigate to PHP 4x installation directory. It should be simliar to the one listed above. In this folder, there should be 2 files with ini in the filename - php.ini-dist and php.ini-recommended. Make a copy of php.ini-recommended using Ctrl+C & Ctrl+P, then rename the file to php.ini.

Open the file using notepad or wordpad or your favorite text editor (NOT MS Office). We need to make a couple of minor changes so there are no conficts between the 2 versions of PHP we will have running. We are firstly looking for include_path. This is optional, but you want to have something like the following:

; Windows: "\path1;\path2"
include_path = ".;C:\PHP\php-4.3.10-Win32\PEAR"

The main change is the extension_dir. Uncomment and add the following:

; Directory in which the loadable extensions (modules) reside.
extension_dir = "C:\PHP\php-4.3.10-Win32\extensions"

Then we do the same steps in the PHP5 folder.

Back to table of contents

C:\PHP\php-5.0.3-Win32

Exactly the same process as with the PHP4, but the extension directory should be as follows in your newly-created
php.ini.

; Directory in which the loadable extensions (modules) reside.
extension_dir = "C:\PHP\php-5.0.3-Win32\ext"

Please note that there are many configuration changes that you may want to make. These instructions only cover the extension_dir.

Almost there. The final steps are to create windows services for Apache.

Back to table of contents

Windows Services

The default

If you have previously had Apache running as a service, you will know that the configuration file that Apache reads from is httpd.conf. What we now want is for Apache to look for different config files dependent on which service is running. We want each service to point to its respective config file.

Command Prompt

A very quick way to bring up the DOS command window is to windows key+R -> Type command. Alternatively, Start -> Run -> Type command. Navigate to the bin directory in your Apache folder:

C:\PROGRA~1\APACHE~1\APACHE2\BIN>

We now want to create a service for each version of PHP. Copy, Ctrl+P and paste the following at the command line in the directory as listed above. As you should have a service called Apache2 already available through the default installation, we can ignore the first code block below!

apache -k install -n "Apache2" -f "C:\Program Files\Apache Group\Apache2\conf\httpd.conf"

We then do the same for PHP5.

apache -k install -n "Apache8000" -f "C:\Program Files\Apache Group\Apache2\conf\php5.httpd.conf"

Now open your services monitor to see the status of the services you have created. Modify them as you wish.

Back to table of contents

Apache Monitor

If you have Apache Monitor running, you should now be able to see these 2 services by right clicking the icon in the task bar, and Open Monitor. You can then start your servers.

Create a php file, eg: info.php and insert the following:

<?php phpinfo(); ?>

Then type in the following urls directly into your address / location bar or follow the links below if your server is running both / either service:

That's it. You should now have 2 versions of PHP running concurrently on your Apache server. Have fun!!

Back to table of contents

Summary

As previously stated, please back up relevant files before following these instructions. The instructions do not cover any aspect of server security and PHP set-up and security. Make sure you follow this up.

References

PHP
Apache
Devshed

Comments

But where is the Article itself ?

Hey, Its so depressing everywhere i go on the net they say your site has the best solution to running 2 Versions of PHP as Apache Modules BUT where is the article ? i mean the page for that on your site is this and it just gives a small preface but where to i see the method to do so ? very frustrating.

Maybe i am over looking, but can you help me please ?

Sorry

My server just got migrated recently and it appears that the article is no longer where it used to be.
I will check out my back-ups tonight and post as soon as possible.

Probably means that there is more articles missing, need to check that too.
Stay tuned and sorry again for the inconvenience.

Br

Jamie
Mobile Movie Database

Back

Found the file, and the page works again.
Sorry about that!!

Br

Jamie
Mobile Movie Database

Syndicate content