Stackify is now BMC. Read theBlog

How to Host PHP on Windows With IIS

By: Peter
  |  March 17, 2023
How to Host PHP on Windows With IIS

Running PHP on IIS might not seem like a logical choice, but it can make sense. Luckily, getting PHP to run on IIS isn’t very hard, and gives you access to all the great PHP work that this community has produced.

What is PHP?

PHP was originally an acronym for personal home page, but now it’s short for PHP: Hypertext Processor.

PHP is a programming language used for web development. It’s also used as the name for the underlying engine that runs that application. Usually, this is the Zend Engine. Even though alternatives exist, the Zend Engine is the standard implementation and the only one regarded as “feature complete.”

The Zend Engine interprets the PHP code and compiles it on-the-fly to a format it can execute. This might seem inefficient, and it would be if the engine had to do so for every request, but some smart people have created optimizations as sufficient workarounds for most use cases. Incidentally, we will look at one such solution (WinCache) in this article. The advantages of PHP (like a great community and fast results) greatly outweigh the possible performance drawback most users won’t notice.

What is IIS?

IIS (Internet Information Services) is Microsoft’s web server and has been around since 1995. The latest version, IIS 10, comes bundled with Windows and as such, IIS is a free product.

There is a lightweight version of IIS called IIS Express that can be installed separately, but is intended for development purposes only. IIS Express only supports HTTP and HTTPS protocols and by default, merely supports local requests. In this article, I will be using the full version of IIS 10.

Since version 7, IIS has had a modular architecture, allowing us to add only the functionality we need. IIS 10 added support for HTTP/2, HSTS, and containers. IIS is the third most popular web server (after Apache and Nginx), running about 8.7% of all websites.

Reasons to run PHP on IIS

Most instances of PHP applications run on an Apache or Nginx web server, but there are valid reasons to choose IIS over other options:

  • Your system engineers have more experience with IIS.
  • You have a great support contract with Microsoft.
  • You have to run ASP.NET or classic ASP applications.
  • You develop Windows-only solutions locally on IIS, and also need to work on PHP solutions.
  • You need to integrate with other Windows services like Active Directory, Windows file shares, or Microsoft Exchange.

IIS only runs on Windows, but keep in mind that running PHP on IIS is not the same as running PHP on Windows. There are options to run PHP on Windows like XAMPP or WampServer. However, these two options make some additional choices for you. They run Apache as a web server and use MySQL or MariaDB as a database server.

This might not fit your needs. If you already have websites running in IIS, it’s a good idea to run your PHP websites there too. This way, they’re all in one place. Maybe you even want to use another database technology. Or you prefer the reliability of a solution provided by the big company that Microsoft is. We’ll look at installing PHP on IIS, and you’ll see that it isn’t that hard.

Try Stackify’s free code profiler, Prefix, to write better code on your workstation. Prefix works with .NET, Java, PHP, Node.js, Ruby, and Python.

Installing IIS

If your machine isn’t running IIS yet, you will need to activate it. How to do depends on the specific version of Windows that you have. On a non-server edition of Windows, this is usually done in the “Turn Windows features on or off” dialog. The easiest way to open this dialog (on Windows 8 and up) is to open the Start menu and search for “windows features.” There, you should then be able to open this dialog. Once the dialog is open, check “Internet Information Services” and “CGI.”

IIS PHP

On Windows Server, there are more steps involved. Open the Server Manager application and go through the “Add Roles and Features” wizard. On the Server Roles page, select “Web Server (IIS)”:

IIS Web Server Roles

When you are prompted to select the server roles, be sure to enable CGI:

Server Roles

Once you have installed IIS, navigating to http://localhost should render the default IIS page:

Default IIS Page

What is CGI/FastCGI?

CGI is short for Common Gateway Interface, and is a standard protocol for web servers to execute applications on the server. It is important that you enable CGI in your IIS instance. In our case, this means that IIS receives the incoming web request, and then passes that on to our PHP engine (a simple Windows executable). The PHP engine will then return the output that IIS should return to the client.

CGI starts and stops the application for every request. This can become quite costly in terms of performance if you receive many requests. CGI also can’t handle database connection reuse or in-memory caching very well. FastCGI is a newer and better version of CGI created in the mid-90s to address these issues. FastCGI can keep processes alive over multiple requests and can reuse other resources, making it a faster and more modern alternative to “classic” CGI.

When you enable CGI for IIS, it will include FastCGI by default. IIS will then execute PHP over FastCGI.

Installing PHP on IIS with Web PI

The easiest way to install PHP on IIS is to use Microsoft’s Web Platform Installer. Web PI is a free package management tool to install non-commercial web development tools and their dependencies. When you run this tool, you can select the latest version of PHP under the Frameworks section of the Products tab:

Microsoft Web Platform Installer

Web PI will add several items to the install list (three in my case); then click on the big Install button at the bottom. Next, you will need to accept the license terms of the different components. If any components fail to install, you might have to install them manually. In my case, there was an issue with the PHP Manager for IIS, which I then just downloaded (from here) and installed.

It is also possible to install PHP manually, if you want more control.

Installing PHP on IIS manually

Installing PHP for IIS with Web PI will install it to “C:\Program Files.” You might want some more control over this, which is entirely possible and also not that difficult.

First, download PHP from the PHP website. Be sure to download a non-thread safe version. It isn’t necessary for PHP to do thread-safety checks, because FastCGI ensures a single-threaded environment. Removing these checks improves performance.

Extract the files to the folder of your choice; for example, “C:\PHP.” Also, add this folder to the Path System variable.

Next, open the Handler Mappings screen in IIS Manager:

IIS Handler Mappings

In the Actions pane, you will be able to choose “Add Module Mapping.” Enter the necessary details to tell IIS that *.php files should be run by php-cgi.exe through FastCGI:

Add Module Mapping

Now, select your server in the Connections pane of IIS Manager and choose “Default Document.” In the subsequent Actions pane, click “Add…” and add “index.php.” You can also add other files, like “Default.php.” This tells IIS to look for such a file, if no file is specified in the URL.

Finally, go to your PHP folder (e.g. “C:\PHP”) and rename either “php.ini-development” or “php.ini-production” to “php.ini,” depending on your environment.

Configuring PHP on IIS

If you’ve installed PHP on IIS by using Web PI, the defaults will probably be fine, but it doesn’t hurt to check. If you’ve installed PHP manually, you will definitely want to check the configuration. You can open the “php.ini” file and edit it if you’re familiar with configuring PHP. You can also open the PHP Manager in IIS Manager.

If you haven’t already, install PHP Manager for IIS from here. Then you will see it in IIS Manager:

PHP Manager

When you open it, you will immediately see a warning about your php.ini file. Clicking on the “View recommendations” link will give you an easy way of fixing any issues:

IIS PHP Configuration

WinCache and extensions

PHP allows extensions to be installed and enabled. In my default installation, I already had 40 extensions installed. You can install PHP extensions in the “ext” folder under your PHP installation folder. You can also install them through the PHP Manager. Likewise, you can enable and disable extension by editing the php.ini file or by using the PHP Manager.

For example, WinCache is a popular extension to improve the speed of PHP on Windows systems. It includes caching mechanisms like user data cache, session cache, file system cache, and relative path cache.

If you’ve installed PHP by using Web PI, you won’t need to do anything here. If you’ve installed PHP manually, download the correct version from SourceForge and save it to some temporary folder. Be sure to download from the development folder, because the other files are for the Web PI version of WinCache. Execute the “exe” file and then copy the “php_wincache.dll” file to the “ext” folder in your PHP installation folder.

In PHP Manager, you can then click on the “enable or disable an extension” link and enable WinCache:

PHP Extensions

The other option is to add “extension=php_wincache.dll” at the end of your “php.ini” file.

Make sure you download the WinCache version that corresponds with your PHP version. When I was writing this article, WinCache was only supported up to PHP 7.2, even though PHP 7.3 was already released. In the WinCache forums, you can read that support for 7.3 is on the radar, but requires some work.

Testing your installation

Now that we have installed PHP for IIS, we can test it easily. Create a new file in your favorite text editor and add only the following line:

<?php phpinfo(); ?>

Save this file to C:\inetpub\wwwroot. This is where IIS will be hosting your websites. Be sure to run your editor as Administrator, or you won’t be able to save the file to that folder.

Now, navigate to http://localhost/phpinfo.php in your browser and you should see a lengthy overview of all the current PHP settings:

IIS PHP Settings

This means everything is working great!

Keep in mind that putting a phpinfo.php file on a public website is a bad security practice. It exposes too much information about your server that hackers could use to attack you.

Combining the power of IIS and PHP

PHP is used by almost 80% of the websites on the internet, according to estimates. It has also stood the test of time very well. It has a vast and vibrant community and includes some tried and tested products such as WordPress and Drupal.

IIS isn’t the web server with the most significant market share, but it has the support and dedication of Microsoft behind it, meaning it will continue being developed and supported for quite some time. If you already have websites running in IIS, you can now add your PHP sites to it, keeping everything in one place.

In the example above, we added our (tiny one-file) PHP website to the wwwroot folder. This maps to the “Default Web Site” in IIS. However, nothing is stopping you from using the possibilities of IIS and putting your websites in other folders, then running them in separate application pools. It’s a best practice.

For example, I’ve extracted the Drupal files on my local machine and pointed IIS to that folder:

Drupal Files

Note: it is possible that this doesn’t work for you. I had to do some extra work because the default web.config generated by IIS includes a rewrite section that didn’t work for me. I had to either remove it, or install the rewrite module for IIS.

Here you can see the Drupal setup running on my local IIS instance:

Drupal on IIS

This is Drupal and PHP running on IIS 10 on Windows 10!

Further considerations

Just like any other application running on IIS, you want to take some things into account when running PHP applications on IIS:

  • Isolate applications from each other by running them in separate application pools.
  • Only let the application pool identity access the folder and files that belong to the website.
  • Consider having a php.ini file per site, instead of one global php.ini file.
  • Consider installing PHP in a folder that indicates the version (“C:\PHP7.2” instead of “C:\PHP”) so that different sites can rely on different versions of PHP.

There’s a lot more to running PHP on IIS, because of the power of both platforms (just like there are many possibilities when running PHP on Apache or Nginx).

An interesting thing to note is that Retrace will support running PHP on IIS soon. Retrace already supports many platforms and frameworks that run on or are often associated with PHP. But adding support for PHP on IIS will make it a great solution for your PHP APM needs, regardless of which web server is running your PHP applications.

PHP and IIS: easy!

Not many people know IIS supports running PHP applications and how simple it is to set it up. Using Web PI, you can have it working in just a few clicks. Installing it manually isn’t too hard, and it gives you some extra control.

Linux and Apache or Nginx seems to be the default choice for PHP, but in just a few steps, you can have your PHP applications running on your IIS web server. If for whatever reason, you don’t have the option of running Apache or Nginx and feel “stuck” with IIS, or you just prefer IIS, it shouldn’t stop you from using the power of PHP, still the most popular language on the internet.

Improve Your Code with Retrace APM

Stackify's APM tools are used by thousands of .NET, Java, PHP, Node.js, Python, & Ruby developers all over the world.
Explore Retrace's product features to learn more.

Learn More

Want to contribute to the Stackify blog?

If you would like to be a guest contributor to the Stackify blog please reach out to [email protected]