Chapter 15. PHP on Windows
Installing and Configuring PHP on Windows
There are many reasons to use PHP on a Windows system, but the most common is that you want to develop web applications on your Windows desktop machine without the hassle of telnetting into the central Unix server. This is very easy to do, as PHP is extremely cross-platform friendly, and installation and configuration are becoming easier all the time.
What can be confusing at first is the number of various configurations and choices available. There are many variants of the Windows operating system, and many web servers are available for those operating systems. PHP itself can run as either a dynamic link library (DLL) or a CGI script. It's easy to get confused or to misconfigure your system. This chapter explains how to install, configure, and make the best use of PHP on Windows systems. We also show how to take advantage of the features unique to the Windows platform—connecting to databases with ODBC and controlling Microsoft Office applications through COM.
15.1. Installing and Configuring PHP on Windows
This section shows you how to install PHP on Windows. We cover both manually configuring your web server to use PHP, and the use of the PHP installer, which will do the configuration for you.
15.1.1. Going Straight to the Source
The most recent version of PHP can always be found at http://www.php.net/downloads.php. While you could download the source and compile it yourself, chances are you don't have a compiler. Fortunately, the PHP downloads page has a binary distribution for Windows.
Download the latest Windows PHP distribution and extract it into a local directory. You'll need a program such as WinZip (http://www.winzip.com) to extract the ZIP file. At the root level of the distribution is php.exe, which you can run from a command prompt to test and experiment with PHP. If you have PHP code in a file (e.g., test.php), you can run that code with:
C:\> php -q test.php
15.1.2. Configuring PHP with a Web Server
Once you have PHP on your local computer, the next thing to do is to configure it into a web server.
The choices here are many. PHP can either be run as a standalone CGI script or linked directly into the server via the server's native Server API (SAPI). There's SAPI support for IIS, Apache, Netscape iPlanet, and AOLserver. PHP can even be configured to run as a Java servlet engine.
Because of the rapid change in the development of PHP, it is always best to check with mail lists and online resources to determine the best configuration for your specific application. In general, the CGI version is more reliable, but it is slower than SAPI implementations because it has to be loaded with each request. SAPI implementations load once and create a new thread for each request. Although this is more efficient, the tight coupling with the server can bring the entire server down if there are memory leaks or other bugs with an extension. SAPI support on Windows is considered to be unstable as of the writing of this book, and hence is not recommended for production environments.
For our discussion, we will look at and compare installation on Microsoft Personal Web Server (PWS) and Apache for Windows, both on Windows 98—two installations that help to contrast the differences in implementation while providing useful local development environments.
184.108.40.206. Configuration common to all Microsoft installations
Regardless of the server you use, there are a few steps common to all installations in a Microsoft environment:
DLL search order varies slightly between versions of Windows. In most cases, it is as follows:
220.127.116.11. Using the PHP installer to automatically configure PHP
The PHP development group offers an installer that configures a Windows web server to work with PHP. This is the recommended method of installation, as you don't need to learn how to edit the registry or how to configure Apache. It is available for download from http://www.php.net/downloads.php. PHP's installer will automatically configure your server for many of the more popular web servers for the Microsoft platform, as shown in Figure 15-1.
Figure 15-1. Choosing the server type in PHP's installer
After you install your preferred web server, running the installer will prompt you for some values for typical php.ini configuration and the desired web server and configuration to use. Modifiable parameters here include the install path for PHP (typically c:\php), the temporary upload directory (the default is c:\PHP\uploadtemp), the directory for storing session data (the default is C:\PHP\sessiondata), the local mail server, the local mail address, and the error warning level.
18.104.22.168. Manually configuring PWS
To configure PHP for Personal Web Server, you must add a line in the registry that associates .php files with the PHP engine. For Windows 98, that line is:
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\w3svc\parameters\Script Map] ".php"="C:\\PHP\\php.exe"
You must also enable execution of scripts in each directory in which you want to run PHP. The exact method of doing this varies between versions of PWS—it may be an option when you right-click on the directory from the Explorer or a Control Panel option, or it may be done through a separate PWS configuration program.
22.214.171.124. Manually configuring Apache
Apache uses a single configuration file, httpd.conf, rather than the system registry. This makes it a little easier to make changes and switch between CGI and SAPI module configurations.
Add this to httpd.conf to configure PHP as a SAPI module:
LoadModule php4_module c:/php/sapi/php4apache.dll AddType application/x-httpd-php .php
To execute PHP scripts via CGI, add the following to the httpd.conf file:
AddType application/x-httpd-php .php Action application/x-httpd-php "/php/php.exe"
126.96.36.199. Other installers and prepackaged distributions
There are also a variety of prepackaged Windows distributions of PHP available on the Web. These distributions can make it easier to get a web server and PHP running, and some offer more features or a smaller footprint. Table 15-1 shows some of the more interesting distributions available at the time of writing.
Table 15-1. Prepackaged distributions of PHP-related tools for Windows
15.1.3. Adding Extensions to the Base Distribution
PHP on Windows has out-of-the-box support for ODBC and MySQL. Most other extensions must be manually configured (i.e., you must tell PHP where to find the DLL files).
First tell PHP which directory contains the extensions by adding this to your php.ini file:
extension_dir = C:\php\extensions; path to directory containing php_xxx.dll
Then explicitly load the module with a line like this in the php.ini file:
extension=php_gd.dll; Add support for Tom Boutell's gd graphics library
You can determine what extensions are available for your particular version by looking in the extensions directory of your distribution.
Once you have made these changes, restart your server and check the output of phpinfo( ) to confirm that the extension has been loaded.
Copyright © 2003 O'Reilly & Associates. All rights reserved.