How to serve PHP code in IIS

walden systems, geeks corner, programming, languages, developer, mediawiki, customize, metatags, iis, web server, html, php, embed, server side, client side, web, htm, apache
Internet Information Services (IIS) for Windows® Server is a flexible, secure and manageable Web server for hosting anything on the Web. From media streaming to web applications, IIS's scalable and open architecture is ready to handle the most demanding tasks.

When building a complex page, at some point you may be faced with the need to combine PHP and HTML to achieve your needed results. At first point, this can seem complicated, since PHP and HTML are two separate languages, but this is not the case. PHP is designed to interact with HTML and PHP scripts can be included in an HTML page without a problem.

In an HTML page, PHP code is enclosed within special PHP tags. When a visitor opens the page, the server processes the PHP code and then sends the output (not the PHP code itself) to the visitor's browser. It is simple to integrate HTML and PHP. A PHP script can be treated as an HTML page, with bits of PHP inserted here and there. Anything in a PHP script that is not contained within ?php ? tags is ignored by the PHP compiler and passed directly to the web browser.

In order to embed PHP code into html, the web server needs to be set up to parse HTML files as PHP. The setup will depend on which web server you are using. In this blog, we will go through how to setup IIS web server to parse PHP code in HTML. The steps involves installing PHP, setting up the module handlers and finally creating a HTML file with embedded PHP.


The first thing we need to do is install PHP, PHP can be found at the sourceForge download site. Once PHP is downloaded, double click on the file to install PHP, take note of the installation path since we will need it in later steps.

Next, we have to configure IIS to be able to parse PHP files. To do so, we will enable FastCGI support by going to Server Manager->Roles->Add Role Services and selecting the CGI checkbox. Next, we need to configure IIS to handle PHP requests. For IIS to host PHP applications, we have to add a handler mapping that tells IIS to pass all PHP requests to the PHP application framework by using the FastCGI protocol. To finish configuring IIS, open IIS manager and double click on Handler Mappings. On the right side, click on Add Module Mapping. In the Add module mapping dialog box, enter the following information :

Request path: *.php
Module: FastCGIModule
Executable: PATH_TO_PHP_INSTALLATION_DIRphp-cgi.exe
Name: PHP via FastCGI

The above steps will allow IIS to serve PHP files, in order to embed PHP in HTML code, we need to add another handler. To do so, we will follow the same steps above to add a new Module Mapping. In this case, we will change the Request path to *.html so HTML files will be treated as PHP. We are done with the server settings so to test if everything is working, we can use the following test file and open file in a browser.

<!DOCTYPE html>
<html>
  <head>
    <title>Page Title</title>
  </head>
  <body>

    <?php echo "Hello World" ; ?>
  </body>
</html>


The above code will bring up a website with "Hello World."