Learning PHP: Installing and Configuring
Just tuning in? If you missed the first installment in the series, you can read it here.
One of the questions I frequently get asked by new PHP programmers is "How do I install PHP on Windows so I can test my scripts?"
In this article, we will be taking a look at the answer to that question and examining the steps needed to set up a development environment for PHP on Windows. Even if you already have a web host that supports PHP, being able to test your scripts on your own computer as you write them makes the process of debugging easier and faster.
To set up a development environment for PHP, you need two basic elements: first, a web (httpd) server, and second, the php processor. Most likely, you will also want to install a database server. PHP has the ability to work with a range of different databases, however Mysql is by far one of the most popular, and the one that will be covered exclusively in this series.
PHP runs equally as well on native Windows servers such as IIS, PWS and 3rd party servers like Xitami and Apache. However, this article will primarily focus on configuring PHP to work with Apache. Apache has been a long time favorite on both Windows and unix because of its wide support, security and stability. Since IIS remains a popular professional choice, some minor notes on configuring PHP with IIS will also be included.
Downloading the Components
The latest win32 version of Apache can be downloaded from http://httpd.apache.org/dist/httpd/binaries/win32/
The PHP processor is available for download from PHP's website, http://www.php.net/. Currently, two versions for Windows are available. The first contains both the Module and CGI versions. Because the Module version runs directly integrated with Apache, rather than as an additional process, it can offer significant performance gains over the CGI version. Keep in mind that running PHP as a module is only stable on Windows NT, 2k, and XP.
The second version is a smaller package that includes an installer that installs and automatically configures PHP on IIS, PWS or Xitami. This version contains the CGI version only.
So which version do you choose? For the purposes of this article, and if you are going to be running Apache, or wish to manually install PHP, choose the first version. If you are running another server such as PWS, IIS, Xitami, and want to do the installation automatically, choose the second.
The latest version of Mysql can be downloaded from http://www.mysql.com/downloads/mysql.html.
Installing Apache and PHP
After downloading Apache, PHP and Mysql, you are ready to begin the installation. First, run the apache install program and choose C:\apache as your installation directory. Then, unzip PHP to C:\apache\php\.
C:\apache\php\ should now contain php.exe, php4ts.dll and several other files. There should also be a collection of subfolders, including \dlls and \extensions.
If you choose to run PHP as a module, you should copy php4ts.dll to your windows system directory (if you are on Windows NT or 2k, C:\winnt\system32\ and on Windows 95-ME, C:\windows\system\).
At this point, you should also create some additional directories for needed for PHP. In C:\apache\php\, create two new directories called "uploadtemp" and "sessiontemp".
Next, you need to edit two files, the apache configuration file, httpd.conf, and the php configuration file, php.ini
Editing the Apache Configuration File
Using a text editor, open C:\windows\apache\conf\httpd.conf. Scroll down to where you see "# Apache Modules compiled into the standard Windows build" and at the end of this section, add the following lines:
To run PHP as a module:
LoadModule php4_module c:/apache/php/sapi/php4apache.dll
AddType application/x-httpd-php .php
Or, to run PHP as a CGI:
ScriptAlias /php/ "c:/apache/php/"
AddType application/x-httpd-php .php
Action application/x-httpd-php "/php/php.exe"
Notice the line AddType application/x-httpd-php .php. This line tells the apache server that it should parse all files ending in the extension .php as PHP documents. It is a good idea to leave this as it is, since most servers follow similar naming conventions, but you could also add additional extensions if you wanted. For example, if you wanted to parse all html documents as php too, you would change the line to:
AddType application/x-httpd-php .php .html.
You could even include a line like:
AddType application/x-httpd-php .php .eeks
and expect a file called myphp.eeks to parse as PHP.
The rest of the configuration of the file was completed during the installation of Apache, so once you have made the changes, save and close the file.
(View a sample configured httpd.conf file - from Apache vs. 1.3.20)