Building a PHP 5 Web Server on Windows IIS

A detailed tutorial on how to build a PHP 5 web server on Windows.

Table of Contents

1. Introduction
2. Installing IIS on Windows 2000
2.1 Installation Steps
2.2 Enabling Support for Detecting Usernames
3. Installing MySQL 4.1+
3.1 Installation Steps
3.2 Understanding the Windows Path Environment Variable
3.3 Securing the Root Account and Removing Anonymous Accounts
4. Installing PHP 5
4.1 Installation Steps
4.2 Working with the php.ini File
4.3 Enabling E-mail Support
4.4 Configuring IIS to use PHP
4.5 Enabling MySQL Support
4.6 Enabling GD Support for Graphics
4.7 Placing FPDF on the Include Path for PDF Support
5. Installing PEAR
5.1 Installation Steps
5.2 Installing PEAR::DB Database API
6. Installing Aspell for Spell-check Support
6.1 Installing the Aspell Program, English Dictionary and Pspell PHP Extension
7. Download Resources

1. Introduction

With the recent release of PHP 5.0, many PHP developers might be thinking about upgrading their existing PHP installations to the new standard. Although PHP 5.0 may not be ready yet for a live environment, this tutorial will show you how to build a PHP 5.0 server on Windows that would be suitable for hosting live applications with future builds of PHP 5.x.

In general, a "dot zero" version of a new system has not been tried and tested enough to be considered safe for production servers. With an application as complex as PHP 5.0, it is an absolute certainty that bugs will be found, and corrected promptly, within the coming months. I would expect a 5.0.1 version to be released shortly, which may prove to be stable enough for live servers.

In the following tutorial, I will be working with the following software:

Windows 2000 Professional: I consider this to be the best version of Windows that MS ever produced, I am using SP 3. These instructions should also be appropriate, with some variations for IIS configurations, for Windows XP Pro, Windows 2000 Server and Windows 2003 Server, but not Windows XP Home that is not a proper network enabled operating system, and should be avoided at all costs! Important Note: It is important to realise that IIS on Windows 2000 Pro has built-in 10 user connection maximum limit; as a result you should use Windows 2000 Server for a live server, but Windows 2000 Pro or Windows XP Pro should work fine for a test server. Thanks to Brian O'Flaherty for this information.

IIS 5: This comes on the Windows 2K CD; you can also use 5.1 or 6 depending upon which operating system you are using. I am using IIS rather than Apache 2 because IIS allows us to use pass through authentication in our PHP applications (I will cover this in detail later), and generally believe that Apache belongs on Unix/Linux for production servers; while if you must use Windows for a web server, you might as well use IIS too!

MySQL 4.1.3 Beta: As of writing, this is still beta so should not be used on a live server. I have been using it for the last couple of months, and found it to be quite stable; therefore I believe that MySQL 4.1 will be released as an official version soon. 4.1 provides some major improvements over 4.0.x, such as sub queries, so I would recommend this version strongly.

PHP 5.0.0 Win 32: It goes without saying; you should download the Windows version of PHP. I will be doing a manual build, so download the zip package instead, rather than the Windows installer which only provides a basic install of PHP 5.0.

PEAR::DB 1.6.5: I will be using the version of PEAR that comes with PHP 5.0, and downloading and installing DB 1.6.5, the PEAR database abstraction API (I will explain all this later).

Aspell 0.50.3 and Aspell en 0.50.2: This application, along with the relevant English dictionary (other languages are also available), allow us to use spell checking functions in our PHP 5.x applications.

FPDF 1.52: This is the free PDF generating class that we will use in our PHP 5.x applications to generate PDF document on the fly.

If you have never heard of some of these packages, don't worry I will explain each one in turn before I describe how to install them. In general with these installation tutorials, your system and version numbers will not exactly match mine, so there may be different options or steps required to get your configuration up-and-running, but hopefully these guidelines will provide all the instructions you need.

You will find a section of links at the bottom of this page to the download sites for all of the software you need to setup your server. Download the latest versions of everything you need (I estimate about 50 MB in total for all downloads), save them to a local folder on your C drive, then you are ready to begin.

2. Installing IIS on Windows 2000

The first thing we need to do is set up IIS from our Windows 2K CD. IIS (Internet Information Service) is Microsoft's equivalent of Apache, a HTTP server that will run on your PC, listening to a network port (by default port 80) on your machine for HTTP requests, and responding to these requests, normally requests for HTML pages, by sending the HTML page to the client machine looking for the data.

You could use Apache 2.x, or some other web server, for this step instead, but I do not discuss Apache sever configuration options at any stage so you might get lost. I use a LAMP (Linux Apache MySQL PHP) test server at home, and Windows IIS servers in work, so am comfortable with either environment. Therefore, I may publish a tutorial on setting up a LAMP server with PHP 5.0 at a later date, which is a radically different process to the one we are about to follow.

2.1 Installation Steps

  1. Insert your Windows 2000 CD, and kill any start-up screen that pops up.
  2. Click Start -> Settings -> Control Panel, and then double click Add/Remove Programs.
  3. On the left panel, click Add/Remove Windows Components.
  4. On the pop-up window that appears, click on the tick box beside Internet Information Services (IIS).
  5. Click Next, Finish and then you may require a restart (hey, it is Windows!).

Now to test out IIS, start up Internet Explorer and type in http://localhost/ as the site to visit: you should get the default "Welcome to IIS 5.0" web page.

2.2 Enabling Support for Detecting Usernames

In my experience building Intranet-based PHP applications, I have found that many users prefer systems that can automatically log them in based on their network credentials. Here we are operating on the principal that if they were able to log onto a network workstation, then they must possess a valid username and password. Rather than asking them to login again to our PHP applications, we can grab the client's network credentials, including the NT username, and login the user automatically, a technique often termed "pass-through authentication".

Pass-through authentication is not possible for Internet-based applications; you will still have to use manual logins and session management. For Internet IIS server set-ups, you can ignore the following steps.

  1. Click Start -> Settings -> Control Panel.
  2. Double-click Administrative Tools.
  3. Double-click Internet Services Manager.
  4. On the Tree tab on the left, click the plus sign beside your computer's name, right-click the Default Web Site icon, then choose Properties.
  5. Click the Directory Security tab, and then in the anonymous access section choose Edit.
  6. Now in the dialog that appears, un-check the checkbox beside Anonymous access to disable anonymous access to your site. It is then safe to click OK in any subsequent window, and then close the IIS manager window when you are done.

Now when any browser attempts to connect to your default website, it must pass its network credentials to the IIS web server, otherwise the connection will be refused. Later on in this tutorial, I will explain to you how you can access the environment variables containing the NT username in you PHP scripts.

3. Installing MySQL 4.1+

Now that we have a functioning web server installed, we will install the next major component, our MySQL database server. The database server works on a similar principal to the HTTP server; that is it runs on Windows as a background service, while listening on a network port for SQL requests for data, then serving up the results of those queries to the clients that requested them.

As I stated above, I believe it is safe to start using MySQL 4.1.x at this stage, as I do not believe it will remain a beta (test) release for much longer. Installing MySQL is a simple process on Windows, follow the steps below.

3.1 Installation Steps

  1. Unzip the zip file you downloaded containing the latest version of MySQL 4.1.x to a local folder.
  2. Within the local folder, you will find SETUP.exe. Run this program to begin the installation.
  3. When asked, install the program to C:\mysql. (this should be the default option).
  4. Choose the Typical installation option.
  5. When the server is installed, we must start it. In C:\mysql\bin you will find a program called windmysqladmin.exe. Run this to start the server. If you like, you can add a shortcut to this program to your start menu to get it to start automatically when you start Windows.
  6. Now test the server as described below.

Start a DOS prompt, and type C:\mysql\bin\mysql. You should get the following screen:

At the MySQL prompt, type show databases; and press return. By default, you will only have two databases to begin with. Now type exit and return to exit the MySQL client.

3.2 Understanding the Windows Path Environment Variable

Rather than having to type C:\mysql\bin\mysql, it would be nicer to simply have to type mysql like any other DOS command. We can do this by adding the C:\mysql\bin folder to the Path variable in Windows. Do the following:

  1. Right-click the My Computer icon on your desktop, then choose properties.
  2. Click the Advanced tab, and then click Environment Variables.
  3. In the list of System Variables, choose Path, and then click Edit.
  4. At the end of the list of local folders, type ;C:\mysql\bin (the semi-colon is required!), and press ok.
  5. It is safe to click OK to all subsequent windows.

3.3 Securing the Root Account and Removing Anonymous Accounts

When installed, MySQL does not set a password for the MySQL root account, which is the administrator account for the entire system. This account needs to be locked down immediately for security reasons. We will also remove the anonymous user accounts from the default install:

  1. At a DOS prompt, type mysqladmin -u root password yourpassword (where yourpassword is of your choosing).

Notice how you did not have to give the full path to the mysqladmin program? That is in the C:\mysql\bin folder also, which is now on the system path. Now we will connect to MySQL as the root user, and remove the anonymous access accounts:

  1. At a DOS prompt, type mysql -u root --password=yourpassword
  2. Run the following at the MySQL prompt:
    USE mysql
    DELETE FROM user WHERE User='';
    EXIT
  3. Type mysqladmin reload in the DOS window.

Now when you try to access the server by simply typing mysql and no username and password, you will get an access denied error. You must now use the root account, with the password that you have set.

4. Installing PHP 5

Installing PHP itself is quite straightforward; the difficulty lies in configuring IIS to use it! There are a number of programs contained within the standard Win 32 distribution, for example it contains two versions of the PHP interpreter that we can use with IIS. The first is a DLL (Dynamic Link Library) that contains an ISAPI module for IIS. This module will run as a persistent interpreter in IIS, one that does not need to be called externally, so in theory should provide the best performance.

In reality, I have found the ISAPI module to be unstable with PHP 5 and IIS on Windows, often causing access violations. These issues will most likely be resolved in time, and may be by the time that you are reading this.

The second version, a standard C executable, is the one that we will be using. This runs under IIS as an external CGI (Common Gateway Interface) call, similar to the way Perl/CGI works, so as a result is not as quick as the ISAPI module, but it is very stable. In the trade-off between speed and stability, we should always favour stability for a live production server.

4.1 Installation Steps

  1. Create a folder called PHP on the C drive root.
  2. Unzip the contents of the zip file you downloaded for PHP 5 into C:\PHP
  3. In C:\PHP, rename php.ini-dist to php.ini
  4. You may want to add C:\PHP onto the system path variable, as you did for MySQL in the previous section.

That's it! Now onto configuring PHP 5.

4.2 Working with the php.ini File

The C:\PHP\php.ini for is the main configuration file for PHP on our server, so we will be spending quite a bit of time working with this file. Open it up now with a text editor, you will find it to be very well commented and easy to work with. This is a good example of the difference between open source software and commercial software, for example IIS uses a difficult-to-navigate tab-based GUI (Graphical User Interface) to configure the server, while Apache uses a text file similar to the one that PHP uses called httpd.conf. Personally, I like this simple no-nonsense approach.

4.3 Enabling E-mail Support

If you have access to a mail sever, for example an Exchange server, then you can instruct PHP to use this server to send mails from its mail() function. Modify the following lines in your php.ini file to pint to the network address of your mail server, where available:

[mail function]
; For Win32 only.
SMTP = mailserver.mydomain.com
smtp_port = 25

PHP will now route e-mails from the mail() function to this sever.

4.4 Configuring IIS to use PHP

Now we need to get IIS using PHP 5 to interpret files with the .php file extension. As stated previously, we will be using the CGI executable version of PHP 5 with IIS for stability reasons.

  1. Click Start -> Settings -> Control Panel.
  2. Double-click Administrative Tools.
  3. Double-click Internet Services Manager.
  4. On the Tree tab on the left, click the plus sign beside your computer's name, right-click the Default Web Site icon, then choose Properties.
  5. Choose the Home Directory tab, then choose Configuration (near the bottom)
  6. Click Add, then Browse.
  7. Navigate to C:\PHP\php-cgi.exe, and select this.
  8. Add in .php as the extension, and tick Check that file exists.
  9. Now click OK on all subsequent windows, and exit Internet Services Manager.
  10. Open up C:\PHP\php.ini in a text editor, and replace the line:

    ; cgi.force_redirect = 1

    with:

    cgi.force_redirect = 0

    As stated in the comment in the php.ini file, you MUST turn this off to get PHP to work as a CGI in IIS.
  11. Save your modified php.ini file. Now in C:\Inetpub\wwwroot create the following PHP script:

    phpinfo();

    ?>

    And save it as info.php
  12. Start Internet Explorer, and visit the address http://localhost/info.php

If everything has been setup correctly, you should get a screen similar to the following one:

If you scroll down on this page, you will see that it details everything about your PHP installation, which is a very useful guide for installation purposes. I will refer back to this page frequently throughout the rest of this tutorial as "the PHP info page"; you can take this to mean http://localhost/info.php in your web browser.

You should also be able to find your username stored in an environment variable on this page, normally in something like $_SERVER["AUTH_USER"] or $_SERVER["LOGON_USER"], which you can access from your PHP scripts (assuming you followed the steps to disable anonymous access in IIS).

4.5 Enabling MySQL Support

There are two separate APIs (Application Programmers Interface) available for MySQL with PHP 5.x. The standard MySQL API is the one that came with PHP 4.x, while the new MySQL Improved API (or mysqli) contains all the new PHP functions for accessing the new features of MySQL 4.1 and above, such as stored procedures that will be introduced in MySQL 5.

To summarise, to support legacy PHP applications, we should install the mysql API, while to support future PHP applications, we should install the new mysqli API. The best solution is to install both.

  1. In the php.ini file, replace the line:

    extension_dir = "./"

    with

    extension_dir = "./ext"

    Or to reflect where your extension DLL files are for PHP 5, they are usually in C:\PHP\ext (hence the relative path "./ext" should work fine).
  2. In the php.ini file, replace:

    ; extension=php_mysql.dll

    with

    extension=php_mysql.dll

    Or if it is not already there, then add it in the same section as the other extension files listed.
  3. Now add:

    extension=php_mysqli.dll

    To the extension section of the php.ini file, to enable the mysqli extension.

Reload the PHP info page, and scroll down until you find a section on mysql and another on mysqli.

4.6 Enabling GD Support for Graphics

GD 2.x allows us to build PHP applications that can generate PNG and JPEG images on the fly, and send them to the client's Internet browser. This functionality is particularly useful for generating charts based on data stored in our MySQL database.

To enable GD 2.x support:

  1. Replace the line:

    ;extension=php_gd2.dll

    with

    extension=php_gd2.dll

    (or add it if it doesn't exist) in the extensions section of the php.ini file.

  2. Reload your PHP info page, you should now see an entry for GD 2.x.

4.7 Placing FPDF on the Include Path for PDF Support

FPDF is a PHP class that allows us to generate PDF documents, which is a great file format for saving documents in, or printing documents from. As with GD, the contents of our PDF files can be based upon the contents of our MySQL database.

FPDF does not need to be installed, it simply needs to be extracted from is zip into a suitable location. The best place to put this is on the include path for PHP.

The include path is the first place that PHP will look in for a file that is included without a location being specified, for example:

include('myfile.php');

When this line of code is executed, PHP will look for myfile.php in the current folder, and in the folder(s) specified in the include path directive in the php.ini file. If it cannot find the file, it will throw an error.

To make FPDF available universally on the server:

  1. In the php.ini file, replace:

    ;include_path = ".;c:\php\includes"

    with

    include_path = ".;c:\php\includes"
  2. Create the folder C:\PHP\includes
  3. Unzip the contents of the FPDF zip file you downloaded to C:\PHP\includes

Now when you want to use FPDF, all you have to do is use:

require_once('fpdf.php');

...to include the class from anywhere on your server.

5. Installing PEAR

PEAR (PHP Extension and Application Repository) is an object-oriented (OO) framework used for the distribution of various classes that can be used freely in your PHP applications. One of the most widely used PEAR extensions is DB, the database-independent abstraction layer that provides a common API for use with all of the major database severs, including Oracle, MySQL, PostgreSQL, MS SQL Server 2000 and many more.

Firstly, we will install the PEAR framework, and then we will install the DB extension. The process for installing the extensions from PEAR are all the same, so here we are using DB as an example.

5.1 Installation Steps

  1. Start a DOS prompt, then type cd C:\PHP
  2. Type go-pear
  3. Press enter to continue
  4. PEAR will ask you for details on your network proxy server (if you have access to one) so that it can automatically download packages from the PEAR website. Skip this, just press enter.
  5. PEAR will then give you a summary of the directory structure for your PHP installation. The key thing to confirm is that the PHP prefix is C:\PHP. Press enter if everything looks ok.
  6. On the next screen, it is important to choose n for no. If you choose Y, PEAR will attempt to connect to the Internet (hence the need for the proxy server account settings).
  7. PEAR will ask to change the include path for PHP, say no (n).
  8. When PEAR installs, change your include path in your php.ini file to:

    include_path = ".;c:\php\includes;c:\php\pear"

5.2 Installing PEAR::DB Database API

  1. At the DOS prompt, under C:\PHP, type pear list (enter) to display a list of all of the installed PEAR packages.
  2. Now copy the DB tgz file (the version I downloaded was called DB-1.6.5.tgz) to C:\PHP
  3. In DOS, type pear install DB-1.6.5.tgz
  4. You should get a message saying install ok.
  5. Type pear list to confirm DB is on the list

As with FPDF, you can now use:

require_once('DB.php');

From anywhere to start using PEAR::DB in your code, as C:\PHP\PEAR is now also on your include path (this is where PEAR will install all of its PHP class files).

6. Installing Aspell for Spell-check Support

The Aspell application allows you to use spell checking in your applications. Originally only available for Linux, it has been ported over to Windows and PHP support for it is available through the Pspell DLL extension. Installing the Aspell program and English dictionary are easy as they come as standard Windows setup programs:

6.1 Installing the Aspell Program, English Dictionary and Pspell PHP Extension

  1. First run the Aspell setup program, the version I downloaded was called Aspell-0-50-3-3-Setup.exe
  2. When Aspell is installed, install Aspell-en-0.50-2-3.exe, the English language dictionary (other languages are also available).
  3. Now in the extension section of the php.ini file, replace the line:

    ; extension=php_pspell.dll

    with

    extension=php_pspell.dll

    (or create this line if it does not already exist)
  4. You might want to add the path to the Aspell executable, normally C:\Program Files\Aspell\bin, to the path variable. In that location you will find the Aspell console application, as well as a couple of DLLs.
  5. Copy the DLL aspell-15.dll to C:\PHP.
  6. Now when you check your PHP info page, you should now an entry for pspell saying that it is enabled.

7. Download Resources

Here is a collection of URLs for downloading the packages mentioned in this tutorial. As always with the Internet, I cannot provide any guarantee that all of these sites will remain live, or change location, in time; so a bit of searching on Google may be required to find what you need.

Resource URL Description
PHP http://www.php.net/ The home site for PHP, from here you can access the download section, the online manual, and also keep in touch with other PHP-related news.
PEAR http://pear.php.net/ The PEAR home site, where you can browse and download all of the PEAR packages, as well as access the PEAR manual.
MySQL http://www.mysql.com/ The MySQL home site, which contains all of the downloads for MySQL, including various client GUI tools for your databases, as well as the excellent PDF version of the MySQL reference manual.
Aspell Win 32 Port http://aspell.net/win32/ Here is the Windows port for the Aspell GNU distribution; you can also download all of the language dictionaries from this location.
FPDF http://www.fpdf.org/ The home site for the FPDF project, as well as containing the zip file download, it also has an extensive collection of examples of how to use the class in your PHP code.

John Collins

I have been writing about web technology and software development since 2001. I am the developer of the Alpha Framework for PHP, and the five.today personal productivity app. I love open source, technology, and economics.