Installing PHP for IIS

written by: Tomy Winderfind; article published: year 2008, month 01;


In: Root » Computers and technology » Servers » Installing PHP for IIS

Dutch French Spanish Portuguese Italian German Japanese Chinese Korean Russian Arabic Bookmark and Share this Article

In this tutorial we'll describe how to add PHP functionality to IIS, so that you can use IIS to serve PHP pages. We assume that at this stage that your IIS server is set up and running correctly.

Downloading PHP

The first step to installing PHP is to download the package from the PHP web site. In your browser, go to http://www.php.net/downloads.php.

Next, scroll down to the section called Windows Binaries. At the time of this writing, there are two packages offered for Windows: the PHP 4.3.0 Installer and a larger package labeled PHP 4.3.0 Zip Package. The file that you should download is the larger of the two, the Zip Package. Whichever version of PHP is current when you visit the download page, it will always be the Zip Package that you need. This package offers support for MySQL and other PHP extensions, so is much better for developers.

Once you have the Zip Package, you'll be taken to another page that offers mirror sites in many different countries from which you can download the file. It's best to choose the country nearest to you, as it will offer a faster download by providing a shorter route between you and the download server.

Installing PHP

First unzip the PHP .zip file to your hard drive. It's a good idea to unzip the files to a directory called php on your C drive (that is, C:\php). If you don't use C:\php, it's important not to unzip the files into a directory with spaces in it, such as C:\Program Files\php, because spaces in the directory name can cause problems for PHP.

Next, open Windows Explorer and browse to the C:\php directory. You should see a subdirectory layout similar to the one shown in the next screen shot.

Don't worry if your directory structure isn't exactly the same as that shown in the image. Because PHP is a fast-moving language, with new versions released frequently, the structure may change.

You should see a file called install.txt. It's important to read this file, as it documents many aspects of installing and setting up PHP. Most important, it will contain any special instructions that are needed for the latest PHP version. There is also a Windows-specific installation page in the online PHP manual at http://www.php.net/manual/en/install.windows.php.

There are currently two ways of installing PHP: a CGI program and an ISAPI module. The ISAPI module integrates into IIS and offers improved performance, so it is the one we recommend. More details about both methods of installation are given at the previous address.

For the ISAPI module install, you need then to open the sapi directory and copy the file named php4isapi.dll into the php root directory (for example, from C:\php\sapi\php4isapi.dll to C:\php\php4isapi.dll). If you chose the CGI install, you can skip the previous step, as php4isapi.dll isn't used with this method.

If you opt for the ISAPI install, as recommended, then your PHP scripts are processed by the php4isapi.dll module. For a CGI install, the PHP scripts are processed by the php.exe file. Another important file used for processing the PHP scripts is the php4ts.dll file, already located in the PHP installation directory.

Next, inside the php directory, create a new folder named temp. Inside temp, create another two folders, one called sessions and one called uploads.The former will be used to hold temporary files associated with session variables, and the latter will contain any files that are uploaded from a web page.

Creating a php.ini File

The next stage is to configure PHP, which is done in a settings file called php.ini. Because php.ini contains so many options, you don't create the file from scratch; instead you use a default php.ini file, which is included in the installation, and customize it for your needs.

There are two versions of php.ini in the php directory:

  • php.ini-dist is used for running on a development server. It contains the best settings for development purposes, and so it is the one you'll be using.

  • php.ini-recommended contains settings that lock down PHP for security purposes. It contains the optimal set of options for running on a production web server. However, because it's locked down, some older PHP scripts may not run as intended. It's worth reading the comments at the top of the php.ini-recommended file, as they discuss the techniques used to lock down the server.

Although you can stick with the default values in the php.ini-dist file for most options, there are a few options you must change, as they supply PHP with the paths it needs to run properly.

doc_root

doc_root needs to be set to the root path of the web server, meaning the site's home directory in IIS. If you used IIS's default settings when you installed it, then doc_root should be set like this:

doc_root = "C:\Inetpub\wwwroot"  

Note that you need the quotation marks around the path.

extension_dir

This setting should contain the path to the PHP extension files. If you installed PHP in C:\php, then you should set extension_dir like this:

extension_dir = "C:\php\extensions\"  

so that it points to the path of the DLL files needed for the PHP extensions to run, which are in the extensions folder of your PHP installation. Note that it's important that you include the backslash (\) on the end.

session.save_path

session.save_path is responsible for most of the problems PHP users have, as a session can't be used unless it's set to a valid directory, and the default setting is /tmp, which isn't a valid directory on a normal Windows installation. In this case, you need to set it to the directory you set up earlier to hold temporary session information. If you unpacked the files to C:\php, this will be

session_save_path = "C:\php\temp\sessions"  

upload_tmp_dir

Again, this needs to be pointed to the temporary directory you created earlier, which stores files that have been uploaded temporarily before they are moved to their proper location. If you unpacked the files to C:\php, this will be

upload_temp_dir = "C:\php\temp\uploads"  

If there is a semicolon (;) at the start of the setting, it will need to be removed so that the option is not treated as a comment.

cgi.force_redirect (CGI Install Only)

If you are installing PHP as a CGI module, you need to look for the following line. It is treated as a comment because of the semicolon at the front of the line.

;cgi.force_redirect = 1  

This option needs to be uncommented (by removing the semicolon) and set to 0 rather than 1, as follows:

cgi.force_redirect = 0  

That completes the main settings needed to get PHP running, so you can save the php.ini file and move on.

Adding PHP to IIS

Now that the PHP files are in the correct places and your php.ini file contains the correct paths, you can actually add PHP to IIS.

The first step is to open the IIS console as you did earlier. If the tree on the left side is minimized, expand it until you can see Default Web Site. Right-click Default Web Site and then select Properties from the drop-down menu that appears.

The Properties dialog box will open.

Select the Home Directory tab and click the Configuration button. A new dialog box similar to the following will open.

This section tells IIS which programs should handle pages with specific page extensions. You need to tell IIS to use the PHP interpreter to process .php page extensions, so click the Add button to add a new entry.

You now have two choices, depending on whether you want to install PHP as a CGI executable or as an ISAPI module. As discussed earlier, ISAPI is recommended because it can offer performance benefits, but we'll show how to add either type.

PHP As a CGI Executable

To install PHP as a CGI executable, click the Browse… button and navigate to the directory into which you unzipped the PHP files. If you have been following this guide, then this directory will be C:\php\. Once there, select php.exe, as shown in the adjacent screen shot.

For the Extension, enter .php. Make sure that the options for Script engine and Check that file exists are both selected, and then click OK.

You now need to restart IIS, as discussed after the next section on installing PHP as an ISAPI module.

PHP As an ISAPI Module

Installing PHP as an ISAPI module is a very similar process to installing PHP as a CGI executable, except that a different file is selected. Click the Browse… button and go to the directory into which you extracted the PHP files. Select the file php4isapi.dll that you copied from the C:\php\sapi directory into the C:\php\ directory earlier. You may need to change the drop-down menu for the file type so that it will display .dll files instead of .exe files.

For the Extension, enter .php. Make sure that the options for Script engine and Check that file exists are both selected, and click the OK button to add this new mapping.

Checking the Mapping

Whichever method you used, you should now have the PHP mapping added, as shown in the following screen shot. Note that only IIS version 5.0 and above display the mappings as in the screen shot.

Click OK to keep your changes. You now need to completely restart IIS as described below.

Restarting IIS

Whenever you make any changes to IIS settings, you need to restart IIS to make sure that it reads the new options and your changes take effect. How you restart IIS depends on which version of IIS you have.

All IIS Versions

Open a new DOS box, usually by selecting Command Prompt from the Windows Start menu. First enter the command

NET STOP IISADMIN  

This stops all the Internet services running on the server. You may be asked to confirm that you want to shut down the Internet services, in which case type y and press Enter.

Now that the Internet services have been stopped, you can restart the web server by using the command

NET START W3SVC  

You should now see a message stating that The World Wide Web Publishing service was started successfully.

Internet Information Services Version 5.1 or Above

If you have IIS version 5.1 or above, you can restart the server straight from the IIS console without having to open a DOS box, which is much more convenient.

Select the local computer entry at the top of the tree. Right-click the entry, select All Tasks, and then Restart IIS…. The following dialog box will display:

The default option is Restart Internet Services on …, so you can just click OK to restart the server.

Testing PHP Pages

The next step is to check that PHP is actually working properly. Create a page in Dreamweaver containing the following HTML and PHP to use as a simple test:

<html>  
<head>  
<title>PHP Test Page</title>  
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">  
</head>  
<body>  
<?php phpinfo(); ?>  
</body>  
</html>  

Save the page as phptest.php in the root directory for the default web site. If you installed IIS using the default settings, this will be

C:\Inetpub\wwwroot\  

Next, open a browser and type in the following address:

http://localhost/phptest.php  

If everything is working correctly, you should see a long page containing the entire PHP configuration, a small section of which is shown in the following screen shot.

Note that if you installed PHP as a CGI module, the Server API setting will show as CGI/FastCGI or similar. If you opted for an ISAPI install, it will show ISAPI.

If you can see the preceding PHP configuration screen, PHP has been successfully installed. If PHP doesn't appear to work, check back over this section to make sure that everything is correct, and also have a look at the "Troubleshooting PHP" section.

Installing PHP Extensions

One of the great features of PHP is that it has a wide range of extensions that can be added to include extra commands and functionality in PHP. Note that the MySQL extension no longer needs to be installed separately; it is installed by default by PHP.

If you open php.ini (from C:\windows\php.ini) in a text editor and scroll down, you'll come to the extension section, a small block of which is shown below (and can be found around line 439):

;extension=php_bz2.dll  
;extension=php_ctype.dll  
;extension=php_cpdf.dll  
;extension=php_curl.dll  

You'll notice that each extension has a semicolon (;) at the start of the line, meaning that the line is treated as a comment and ignored. To make the extension active, you need to remove the semicolon and then save the file.

Next, you need to copy the relevant extension DLL file from its home in the extension folder in the directory into which you extracted PHP (C:\php\extensions if you installed to the recommended location), into your Windows\System32 folder. For example, if you wanted to activate the PDF extension that allows you to create PDF files, which is called php_pdf.dll, you would copy

C:\php\extensions\php_pdf.dll  

to

C:\Windows\System32\php_pdf.dll  

If your System32 directory is in a different folder such as C:\winnt\, then you need to copy the file there instead. You should then restart IIS as described in the previous section.

Note that it's very important that before you enable an extension, you read its entry in the online PHP manual at http://www.php.net/docs.php. Some extensions will need extra files or packages installed, and this will be mentioned in the manual.

Troubleshooting PHP

If you're having problems with getting PHP working successfully, first check back over all the topics covered so far and make sure that each stage is correct. You can then work through the checks and advice in this section.

CGI and ISAPI

  • Check that php.ini exists in your main Windows directory.

  • Check in your php.ini file that the paths for the doc_root and extension_dir settings are correct. Make sure that the case of the paths is correct and matches the actual directory name, and that the path is surrounded by quote marks. For example:

    doc_root = "C:\Inetpub\wwwroot"
  • In the IIS console, right-click Default Web Site and select Properties. Click the Home Directory tab and click the Configuration button. On the Mappings tab, select the entry for .php extensions and click Edit. Check that the path points to the correct file: php.exe for a CGI module install or php4isapi.dll for a ISAPI module.

  • Try copying the following files from the folder you extracted PHP into to your Windows System32 directory (assuming you extracted the files to C:\php):

    C:\php\php4ts.dll  All files in C:\php\dll  All files in C:\php\extensions  
  • Make sure that every time you change an option in php.ini, for example, you restart IIS so that the new changes will take effect.

  • If you still can't get one version of PHP working, it's always worth trying the alternative version. For instance, if you can't get the CGI version to work, try using the ISAPI version.

CGI Specific

  • Open your php.ini file and check that following setting exists:

    cgi.force_redirect = 0  

ISAPI Specific

  • Copy the file C:\php\sapi\php4isapi.dll to your System32 directory, usually C:\windows\system32 or C:\winnt\system32.

Sessions

  • If you can't get sessions to work under PHP, open your php.ini file and check that the path is correct and the directory exists for the setting:

    session.save_path = "C:\php\temp\sessions";  

    This directory is where the data for session variables is stored—for session variables to work, this directory must exist, and PHP needs to be able to write to it.

File Uploads

If you're having problems uploading a file through a browser, open your php.ini file and check the following settings:

  • file_uploads = On

    This setting governs whether file uploads are allowed or not, and it should be set to On if you want to allow file uploading through a browser.

  • upload_tmp_dir = "C:\php\temp\uploads"

    This setting tells PHP where it can temporarily store files that have been uploaded. Check the path is correct and that the directory it points to exists.

  • upload_max_filesize = 2M

    This setting sets the maximum file size of files that can be uploaded through a browser, specified in megabytes.

Disclaimer

1) E-articles is not responsible for the information contained by this article as well for any and all copyright infringements by authors and writers. E-articles is a free information resource. If you suspect this article for any copyright infringement, please read the terms of service and contact us to investigate the problem.
2) E-articles is not responsible for inaccuracies, falsehoods, or any other types of misinformation this article may contain and will not be liable for any loss or damage suffered by a user through the user's reliance on the information gained here.

link to this article