Apache + SSL + PHP4 + PHP5
I’ve had to work with various web applications and systems, and while most web hosts do not offer PHP5 yet, we’ve been migrating our in-house applications to PHP5 to make use of its strengths. Of course, in the midst of migrating, we still have to make sure our previous apps still work, for maintenance purposes.
The result? We needed to be able to run PHP4 and PHP5 on our development machines, the latter mostly for maintenance purposes. I Googled around and after an insane amount of time spent trying to figure things out, I was finally able to make it work.
Mostly to document what I’ve done (so I can do it again later, if needed), I’ve written a tutorial on how I got the following setup working on my Windows (XP) system:
- Apache 2.2.3 with SSL
- PHP 5.1.6 as an Apache module (with both MySQL and MySQLi libraries)
- PHP 4.4.4 as CGI, under a virtual host (read: subdomain)
- Multiple MySQL servers (or even just one)
My reasons for choosing the following versions were that Apache 2.0 with SSL just wasn’t working for me — not on my work computer, not on my own laptop, and not with a few other people I’ve come across. The PHP versions were chosen because they were the most recent versions of both the PHP4 tree and the PHP5 tree.
I’ve decided to separate the tutorial into the following major parts:
- Part One - System Preparation
- Part Two - Apache Installation
- Part Three - PHP Installation
- Part Four - MySQL Installation
For the Apache and around half of the PHP install, I followed this particular thread, with their binaries. Credit for compiling those Windows binaries goes to them, of course! I will just be rephrasing the tutorial written by pnllan for that part.
Part One - System Preparation
It’s always best to start with a clean system — if you have Apache installed on your machine now, I’d highly suggest uninstalling them… or at least Apache and PHP (I kept MySQL running for my system while I fiddled with Apache and PHP, and then changed those over). If you used an installer, and will be uninstalling the system from the uninstaller available, it will usually leave your config files on your system. These config files aren’t very important, but make sure that if you customized your previous config files, you remember (more or less) what those where.
1.1 - Download the needed installers/zips/binaries
Download the following things:
- Microsoft VC++ Redistributable
- Apache 2.2.3 with SSL (
httpd-2.2.3-win32-x86-SSL.zip) - PHP 5.1.6 or later stable versions under the PHP5 tree
- PHP 4.4.4 or later versions under the PHP4 tree
- PHP 5 handler at Apache Lounge (
php5apache2.dll-php5.1.x.zip) - PHP 4 handler at Apache Lounge (
php4apache2.dll-php4.4.x.zip) - MySQL versions of your choice (4.1.x, or 5.0.x, etc.)
Store these somewhere for the meantime.
1.2 - Install the Microsoft VC++ Redistributable
Quoting pnllan, who wrote the initial forum topic:
The reason for this is because it is NOT included in the latest versions of Windows; and Steffen’s builds are compiled using Microsoft Visual C++ 2005.
Part Two - Apache Installation
Now we come to installing our Apache server.
2.1 - Unzip the Apache 2.2.3 archive to your Apache installation directory
The archive you downloaded from Apache Lounge contains the set of files for the Apache 2.2.3 server. It is not an installer, so you will need to configure a few things manually (which is fun, really!).
So, you need to unzip the archive to where you’d like your Apache install located. I unzipped/installed mine to C:\Apache2, although you can of course install it under Program Files or wherever you wish.
If you do install it somewhere else, you have to edit the Apache configuration file, found in the directory’s conf directory and named httpd.conf. Open this file in Notepad and look for ServerRoot and change its value to your Apache installation directory.
(For the rest of the tutorial, replace C:\Apache2 with the location where you chose to unzip/install your own.)
2.2 - Test if Apache works
Now we need to test if Apache is working. Open up a Command Prompt (under Accessories, or in the Run... dialog box, type in cmd) and then changing to the bin directory of your Apache 2 folder, such as C:\Apache2\bin. Once there, enter httpd at the prompt.
If there’s anything wrong, Apache will not start. Otherwise, the cursor will go to the next line and just stay there, blinking. Open up a browser window and go to http://localhost — if your browser tells you that it can’t find the server, then Apache isn’t working correctly.
Once it starts, you can stop the server by holding down the CTRL key and then the C key, and then releasing both. It should stop in a second or two.
Optionally, you could also install Apache as a service. Installing Apache as a service will keep it running in the background, even if you’re not using it. Personally, this is what I do, simply because I’m lazy that way. (Of course, it takes up resources.) To install the Apache service, at the prompt, enter httpd -k install.
If you do this, you will always be able to use net start Apache2 or net stop Apache2 to start and stop the service, respectively.
Do not start the service yet!
Also, Apache comes with Apache Monitor, which shows up in your system tray and lets you stop and start the service easily. To have this run when Windows starts, browse to the Apache bin directory using Windows Explorer and drag the Apache Monitor executable (ApacheMonitor.exe) to your Start Menu’s Startup folder. Windows will then create a shortcut to it.
Lastly, you might like to do a little customization of your Apache config at this point, like moving the document root somewhere else. By default, this is set to C:\Apache2\htdocs. Why would you prefer to move it somewhere else? Lots of reasons:
- If you have a separate partition for your data, it’s just not logical to have everything on your data partition but your website files is still in the system partition.
- If I needed to reformat my system partition suddenly, I don’t need to copy over my website files still.
- You might want to put your website files somewhere that’s more accessible than something like
C:\Program Files\Apache Software Foundation\Apache 2\htdocs:p
So, if you decide you’d like to move it, open up the file httpd.conf in Notepad — you can find this in the C:\Apache2\conf directory. Search for all occurrences of c:/Apache2/htdocs in the file, and modify the path to the directory you wish to use (for me, it’s d:\Site). You should find two occurrences usually.
Part Three - PHP Installation
Now we need to install the two versions of PHP and tell Apache how to access them in order to parse our PHP files.
3.1 - Unzip the PHP archives
Unzip the PHP archives into their respective directories. Since we want to run PHP4 and PHP5 at the same time, it’s better that we know which we’re running — so I unzipped my PHP5 archive to C:\php5 and my PHP4 archive to C:\php4.
3.2 - Overwrite the PHP handlers
Unzip the PHP handlers to a temporary location, and overwrite the file php5apache2.dll in the existing C:\php5 with the one found in the archive; and do the same with php4apache2.dll for C:\php4.
3.3 - Customize the PHP initialization files
Go to the PHP5 directory and open PHP.ini-recommended using a text editor like Notepad. Save it first as PHP.ini in the same directory. You will need to find the following lines (in the first column) and change it over (to the values in the second column):
| Old line | Modified line |
|---|---|
extension_dir = "./" |
extension_dir = "C:\php5\ext" |
;extension=php_mysql.dll |
extension=php_mysql.dll |
| Just below the above line | extension=php_mysqli.dll |
Remember to replace c:\php5 with the directory you unzipped the PHP5 archive to. Save your updates to the initialization file, of course.
Do the same for the PHP4 initialization file, but only the extension_dir directive needs to be “fixed”. You won’t find a mysql library (it’s built-in for PHP4), and you shouldn’t add the mysqli library either.
3.4 - Update the PATH environment variable
Now we need to update our PATH environment variable so that Windows knows about our PHP installations. Add C:\php5 and C:\php4 there by right-clicking on the My Computer icon, selecting Properties, going to the Advanced tab and clicking on the Environment Variables button. Scroll down the System Variables area and find the Path variable; select it and click on Edit. At the end of that line, add ;c:\php5;c:\php4.
3.5 - Decide which is your primary and secondary PHP installations
Before we continue, though, we have to decide which PHP version we’d like to use as our default version. At my work computer, I have PHP5 as the default version; at my own laptop I have PHP4 as the default. It all depends on what version you plan to work with the most. At work we’ll be porting all our applications to PHP5, hence it should be the default installation; but at home, where I work with my linkware scripts, websites, and other projects as a freelancer, I’d be using PHP4 more.
The rest of this section will have values for both setups, just pick the applicable one for you.
3.6 - Update the Apache config file for the primary PHP installation
Now we need to update our Apache configuration file to tell it how to use PHP to parse the .php pages. Open up C:\Apache2\conf\httpd.conf (again) in Notepad and scroll down to the bottom — I usually place these customizations at the bottom so that I can easily go back to them.
Place the following lines at the bottom for your default PHP version. Remember to replace C:\phpX with your own PHPX installation directory. The italicized “X” below stands for the version number, PHP4 or PHP5.
### Load PHPX module now
LoadModule PHPX_module "c:/phpX/phpXapache2.dll"
AddHandler application/x-httpd-PHP .php
# configure the path to PHP.ini
PHPIniDir "c:/phpX"
Optionally, you might like to tell Apache to also instantly load any index.php pages in case you browse to a directory instead of a file. To do this, look for the DirectoryIndex line and add index.php to that line, before index.html. Apache will load those pages in the order you put it in; i.e., if you have an index.php and an index.html in one directory, it will load index.php… but if you only have an index.html, then it will load that instead.
3.7 - Reboot and test Apache and the primary PHP installation
Before we add the secondary PHP version, we had better check if our default version is working. Reboot your PC! So that we’re sure everything loads. Once done, Apache 2 should have started as a service. If you didn’t install it as a service, start it now.
Open up Notepad and write the following lines, and save it as index.php into your document root (C:\Apache2\htdocs or wherever you set it to):
<?php
phpinfo();
?>
Browse to http://localhost and it should now show PHPX information. If you’ve set PHP5 as the default, scroll down this page and check if the mysql and mysqli libraries are there.
3.8 - Set up Apache for Virtual Hosts and the secondary PHP installation
Now we work with our secondary PHP installation. Open up httpd.conf again, and append the following lines at the bottom:
### We now instruct apache to run the secondary PHP installation
NameVirtualHost *:80
<VirtualHost *:80>
ServerName localhost
DocumentRoot "d:/Site"
</VirtualHost>
<VirtualHost *:80>
ServerName PHPX.localhost
DocumentRoot "d:/Site/_phpXscripts"
ServerAdmin youremail@address.com
DirectoryIndex index.php index.html index.htm
ErrorLog logs/phpX_error.log
CustomLog logs/phpX_access.log combined
<Directory "d:/Site/_phpXscripts">
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
<Directory "C:/phpX/">
Allow from all
</Directory>
# PHP X Stuff now
SetEnv PHPRC "c:/phpX"
ScriptAlias /cgi-bin/ "d:/Site/_phpXscripts/cgi-bin"
ScriptAlias /php/ "c:/phpX/"
# Uncomment the line immediately below if PHP4 is the secondary installation
#Action php4-script "/php/php.exe"
# Uncomment the line immediately below if PHP5 is the secondary installation
#Action php5-script "/php/php-CGI.exe"
AddHandler PHPX-script .php
</VirtualHost>
3.9 - Update your hosts file
Now we need to update our hosts<code> file. Go to the <code>C:\WINDOWS\system32\drivers\etc\ directory, and open the hosts file located there in Notepad. At the bottom, add this:
127.0.0.1 PHPX.localhost localhost
3.10 - Test the secondary PHP installation
Now, stop the apache server, and then restart it. Save the test script we used a while ago (the one with phpinfo();) and put it into the PHP htdocs directory you specified above, and go to: http://phpX.localhost/ — you should then see the PHP information for your secondary installation!
Part Four - MySQL Installation
(To be continued. I’m bushed!)
