http://www.developer.com/

Back to article

Learning PHP: Installing and Configuring


October 29, 2001

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

Apache
The latest win32 version of Apache can be downloaded from http://httpd.apache.org/dist/httpd/binaries/win32/

PHP
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.

Mysql
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)

Configuring the PHP.INI

The second file you need to edit is the php.ini, which contains all the settings to control how the PHP processor works. The php zip file contains two sample configuration files, php.ini-dist and php.ini-optimized, but we will be working with the first. There are several changes that need to made in this file:

Find the line that reads:

extension_dir = ./

and change it to:

extension_dir = ./extensions

This is a relative path to the directory which contains extensions to php. Extensions allow you to plug-in additional functions.

Find the line that reads:

;upload_tmp_dir =

and change it to:

upload_tmp_dir =./uploadtemp

This is the directory that will hold any temporary files created when uploading files with PHP. You will also notice an option right below it:

"upload_max_filesize = 2M"

that controls the maximum allowed file size for uploaded files, in megabytes. You can adjust this setting if you feel the need.

Next, look for a list of dlls commented out with ";". Commenting or un-commenting a dll will determine whether an extension is loaded when PHP is started. For the purposes of these tutorials, you should uncomment at least the following dlls:

  • php_gd.dll
  • php_mcrypt.dll
  • php_pdf.dll
  • php_domxml.dll

In the section titled "Module Settings", there are several settings that control how mail is handled. First, you will see a section that looks like this:

[mail function]
; For Win32 only.
SMTP =

A SMTP server is required for sending mail within PHP documents. You have the option of installing a mail server and using that, however the easiest solution is to use an existing SMTP server. If you have a POP3 mail account, you can use the same out-going mail server that you use for sending email. You should also enter your email address in the line that reads: "sendmail_from =."

Finally, scroll down to the section labeled "Session" and change the following:

; Argument passed to save_handler. In the case of files, this is the path
; where data files are stored.
session.save_path =

to:

; Argument passed to save_handler. In the case of files, this is the path
; where data files are stored.
session.save_path = . /sessiontemp

After you have made the changes, save the file as PHP.ini and move it to your Windows directory. If you are on Windows NT, or 2k, this would be C:\winnt\. If you are Windows 95-ME or XP, you would move it to C:\windows\.

(View a sample configured PHP.ini file - from PHP vs. 4.06)

Testing the Installation

If everything was configured correctly, Apache and PHP should be working. To test it, create a text file with the following contents:

<?php phpinfo(); ?>

and save it as test.php in your C:\apache\htdocs\ directory. The file is a simple script that calls a function which displays information about your php configuration.

Use the "Start Apache Server in Console" icon on your menu to start Apache. If you see something like "Apache/1.3.20 <Win32> PHP/4.0.6 running" in the window that pops up, it is a good bet that the installation was a success. To make sure, open up a browser and enter the URL http://localhost/test.php.

If you are not familiar with web servers, you are probably wondering why http://localhost/ resolved to your computer. The short, non-technical explanation is that localhost is a reserved name that always points to your computer. The IP address 127.0.0.1 also works the same way. Since these are internal addresses, if you want someone to be able to view your pages over the web, you need to use your actual IP address or host name.

Also keep in mind that C:\apache\htdocs\ is your root directory. If you want to test a page through the server, or make it accessible, it should go in this directory, or in a sub-directory of it.

Setting up Mysql

Now that Apache and PHP are set up, the final step is to install Mysql. Before you begin, be sure you have shut down Apache. Once you have, unzip and run the setup file for Mysql. The suggested installation location is C:\apache\mysql.

The latest version of Mysql includes a Windows GUI to administer the server and databases called WinMySQLAdmin. To start it up, run C:\apache\mysql\bin\winmysqladmin.exe. Because it is your first time running it, the program will ask you for a username and password. This username/password combination will be used to any time you need to connect to the server.

After you enter the information, the control panel icon will appear in your system tray with a red traffic light. Double click on the icon, and click the tab that says "my.ini Setup", choose which version of Mysql you would like to run, and save the modifications.

To start Mysql, right click on the traffic light in the left-hand corner, choose your operating system, and start the service.

Notes for Installation on IIS (version 4+)

Another popular web server choice for Windows is IIS. While the bulk of this article has focused on the installation of PHP with Apache, you will find some additional notes on IIS here. Much of the configuration process is the same, and there are no changes to the installation of Mysql. The install.txt packaged with PHP itself also includes additional information on installing PHP with IIS and other web servers.

If you are running IIS, you can download the version of PHP that includes the installer, which will install PHP and automatically configure IIS to work with it. However, if you would prefer to set up PHP manually, the installation is fairly easy.

After unzipping PHP to a directory on your computer like C:\php\, follow the same instructions for creating additional directories and copying files as detailed above for PHP configuration with Apache. You should also edit your PHP.ini in the same way, and save the file to your Windows directory.

With IIS, you have the choice of running PHP as a CGI, or as an ISAPI module. Like the Apache module, the ISAPI module is still relatively new, and can sometimes be even more unstable than the Apache module. For this reason, it is recommended that you choose the CGI version.

To complete the installation of the CGI version, start the IIS administrative console, right click on the Default Web Site, and choose Properties. Navigate to the "Home Directory" tab, and click the "Configuration" button. At this point you should see a listing of file extensions and associated handlers.

Next, click "Add". Where it asks for an Executable, enter in the path to PHP (c:\php\php.exe, for example) and enter .php as the Extension. Click okay until you have closed the Properties window and then restart the server.

After testing to be sure the install was successful, you can proceed to installing Mysql.

Finally…

Hopefully, your installations of Apache, PHP and Mysql have been successful. If you have encountered errors during your installation, make sure that you have remembered to copy the php dll file to your system directory, and that you have a PHP.ini in your Windows directory. While relatively minor, these are two of the most common installation problems.

Next time, we will jump into learning the language with an introduction to the basic structure and elements of PHP.


Liz Fulghum currently lives in Annapolis, MD where she works as a web designer for a custom shirt retailer. Liz was convinced to try PHP as an alternative to Perl; she has been a fan ever since and frequently works as a freelance developer.

# # #

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date