There's been tremendous interest in the PHP articles on thesitewizard.com, and one question that I seem to be getting more often these days is whether the procedure for installing PHP4 is similar to that which I outlined in my article on how to install and configure PHP 3. This article is a quick guide to getting PHP 4 running on your Windows machine (be it Windows 95, 98, ME, NT or 2000). The later versions of PHP4 allow you to install it either as an Apache module or as a CGI binary.
If you want to install PHP 5 instead, please see the article How to Install and Configure PHP 5 to Run with Apache on Windows instead. My older article on installing PHP 3 is also available.
The usual reason why you would want to install PHP on your own computer is that it makes testing and debugging your PHP scripts very much easier. Instead of having to log onto your ISP just to upload and test a script, you can do it offline and fix any bugs before the script becomes "live".
Here is the step by step procedure:
PHP is free and comes complete with the source code. If you are not interested in hacking the PHP source code, you can simply download the Windows binaries. Both the binaries and the source code can be found at the PHP website.
At the time I wrote this, the relevant PHP binary to download comes in a zip file. The Windows installer version does not include the binaries to run PHP as an Apache module.
Installation is actually trivial.
Create a directory for PHP on your computer. For the purposes of this article, I will assume that you created "c:\php".
You don't need all the files provided in the archive, so unzip the PHP archive into a temporary location. For example, if you're using Windows 95/98/ME, one possible temporary place to extract the files is "c:\Windows\Temp". All the PHP files will be extracted into a folder of its own, with a name based on the version of PHP you downloaded. For example, if you downloaded php-4.2.3-Win32.zip and extracted it into "c:\Windows\Temp", there'll be a folder called "c:\Windows\Temp\php-4.2.3-Win32\" where the files can be found.
Copy the following files into "c:\php": php.exe, php4ts.dll and php4apache.dll. You can find php4apache.dll in the "sapi" subdirectory (eg, "c:\Windows\Temp\php-4.2.3-Win32\sapi\" in our above example). php4apache.dll is for use with Apache 1.3.X. You will need to use php4apache2.dll if you're installing it for Apache 2.X. Note however that I have not tested the procedure on Apache 2.X, so you're on your own there. Furthermore, php.exe is not strictly needed if you don't ever want to run PHP as a CGI binary or from the command line. I keep it around for those situations where I want to run a PHP script from an MS-DOS prompt without having to fire up the Apache web server.
If you plan to load PHP extensions, you will need to copy the files in the "extensions" directory to "c:\php" (or a sub-folder in "c:\php") as well. Note, though, that MySQL support is now integrated into php and no longer needs an extension dll. Follow the instructions below to make sure that your installation of PHP works before you start dabbling with the installation of extensions.
Copy "php.ini-dist" from the extracted files into your Windows directory, typically "C:\Windows" on Windows 95/98/ME, and rename it to "php.ini".
Use an ASCII text editor (such as Notepad, which can be found in the Accessories folder of your Start menu) to open "php.ini". You may need to make the following changes to the file, depending on your requirements:
Your SMTP Server If you want the mail() function to actually send out email when you test your own PHP scripts, search for the section of the file beginning with the line "[mail function]" (without the quotes). You should come across a section that looks something like the following:
Change it to point to your SMTP server and email account. For example, if your SMTP server is "mail.yourisp.com" and your email address is "firstname.lastname@example.org", change the code to:
Note that after you do this, when your script tries to use the mail() function, you will need to be connected to your ISP for the function to succeed. If you do not modify the above lines and attempt to use mail() in your script, the function will return a fail code, and display (or log) the error (depending on how you configure php.ini to handle errors).
Global Variables If you need to run scripts written for versions of PHP earlier than 4.1, search for "register_globals" in php.ini, and change it to the following:
If you leave it at its default of "Off" in PHP 4.2.X and above, you will need to access your form data variables and cookies using (for example) $_REQUEST["variable_name"] instead of directly as $variable_name. This may break scripts written for earlier versions (pre-4.1) of PHP.
There are numerous other things that you can change in php.ini to make PHP work as you prefer. If your intention is for your local setup to mimic your web host's installation of PHP, you should of course find out how your host sets up their PHP and modify your php.ini accordingly. You can easily find out how your host has set up their php.ini by using the phpinfo() function (see later in this article).
If you want PHP to work with your Apache server, you will need to modify your Apache configuration file to load it. Naturally, you will need to have already installed Apache on your machine and configured it. You can find instructions for installing and configuring Apache at https://www.thesitewizard.com/archive/apache.shtml.
Note that you do not need to have the Apache web server installed in order to test your scripts. You can simply run the interpreter (php.exe) on your scripts (just put your script as a command line argument to php.exe). I find it more convenient, however, to have the server installed, since the browser and server combination will take care of any setting of environment variables and the like which you would otherwise have to do manually. It's up to you, of course. The rest of this guide will assume you installed Apache and wish to configure it to use PHP.
There are two ways to configure Apache to use PHP4: one is to configure it to load the PHP interpreter as an Apache module. The other is to configure it to run the PHP interpreter as a CGI binary. This article will give instructions for both, but you should only implement one of them, either the module method or the CGI binary method. Unless you have a particular reason for running PHP as a CGI binary, you will probably want to load PHP as a module in Apache, since it runs more efficiently that way.
To configure Apache to load PHP as a module to parse your PHP scripts, use an ASCII text editor to open the Apache configuration file, "httpd.conf", typically found in "c:\Program Files\Apache Group\Apache\conf\".
Search for the section of the file that has a series of commented out "LoadModule" statements. (Statements prefixed by the hash "#" sign are regarded as having been commented out.) Add the following line after all the LoadModule statements:
If you are using Apache 2.X, remember to use "c:/php/php4apache2.dll" instead of "c:/php/php4apache.dll".
Next, for Apache 1.3.X users, search for the block of "AddModule" statements. Add the following line after the last "AddModule" statement:
Don't worry that you can't find a "mod_php4.c" file anywhere on your system. That directive does not cause Apache to search for the file on your system. For the curious, it specifies the order in which the various modules are enabled by the Apache server.
If you're using Apache 2.X, you do not have to insert the AddModule directive. It's no longer needed in that version. Apache 2.X has its own internal method of determining the correct order of loading the modules.
Finally, search for "AddType" in the file, and add the following line after the last "AddType" statement:
If you need to support other file types, like ".php3" and ".phtml", simply add them to the list, like this:
If you've configured PHP to run as an Apache module, skip forward to the next section. This section is for people who want to (or need to) configure PHP to run as a CGI binary.
Search for the portion of your Apache configuration file which has the ScriptAlias section. Add the line below after the ScriptAlias for "cgi-bin". Note that if you installed PHP elsewhere, such as "c:\Program Files\php\", you should substitute the appropriate path in place of "c:/php/" (for example, "c:/Program Files/php/").
Apache needs to be configured for the PHP MIME type. Search for the "AddType" comment block explaining its use, and add the following line after it:
As in the case of running PHP as an Apache module, you can add whatever extensions you want Apache to recognise as PHP scripts, such as:
Next, you will need to tell the server to execute the PHP executable each time it encounters a PHP script. Add the following somewhere in the file, such as after the comment block explaining "Action":
Note: the "/php/" portion will be recognised as a ScriptAlias, a sort of macro which will be expanded to "c:/php/" (or "c:/Program Files/php/" if you installed PHP there) by Apache. In other words, don't put "c:/php/php.exe" or "c:/Program Files/php/php.exe" in that directive, put "/php/php.exe".
This section applies to all users, whether you are loading PHP as a module or running it as a CGI binary.
If you want to make your PHP script execute as the default page for a directory, you have to add another line to the "httpd.conf". Simply search for the line in the file that begins with a "DirectoryIndex" and add "index.php" to the list of files on that line. For example, if the line used to be:
change it to
The next time you access "localhost" or "localhost/directory/" without a filename, Apache will deliver "index.php" if available, or "index.html" if "index.php" is not available.
Create a PHP file with the following line:
Save the file as "phpinfo.php" (or any other name that you fancy, but with the ".php" extension) into your Apache htdocs directory.
Next, restart your Apache server so that it can read the new configuration directives you placed into httpd.conf.
Open your browser, and access the file you just created by typing "localhost/phpinfo.php" into your browser's location bar.
You should see an entire pageful of information about your PHP setup. Congratulations - you have successfully installed PHP and configured Apache to work with it. You can also use this same file, phpinfo.php, to find out more about how your web host has set up his php.ini so that you can duplicate it on your local machine.
If for some reason it does not work, you can check to see whether your PHP setup or your Apache setup is causing the problem by simply running php on the file with a command line like "c:\php\php phpinfo.php" (without the quotes).
If invoking PHP from the command line causes a large HTML file with all the PHP configuration information to be displayed, then your PHP set up is fine. The problem probably lies with your Apache configuration. Follow closely my instructions on how to install and configure Apache in https://www.thesitewizard.com/archive/apache.shtml as well as check the instructions on configuring it for PHP given above.
The complete PHP reference manual can be obtained from the php website. You can refer to it online or download the entire set of HTML files for reference offline. As its name implies, it is a reference manual only. For tutorials, check out the PHP tutorials at thesitewizard.com.
Do you find this article useful? You can learn of new articles and scripts that are published on thesitewizard.com by subscribing to the RSS feed. Simply point your RSS feed reader or a browser that supports RSS feeds at https://www.thesitewizard.com/thesitewizard.xml. You can read more about how to subscribe to RSS site feeds from my RSS FAQ.
This article is copyrighted. Please do not reproduce or distribute this article in whole or part, in any form.
It will appear on your page as: