IIS (Internet Information Services) · Advanced Operations

Hosting PHP with FastCGI

IIS runs PHP through FastCGI, which keeps PHP processes alive between requests for good performance.

Advanced 5 min Lesson 40 of 45Covers IIS 10 on Windows Server 2019/2022/2025 and Windows 10/11.
0/45 in IIS
On this page · 5 min · Advanced

Learning objectives#

  • Install the CGI role service
  • Install PHP for IIS
  • Map *.php to php-cgi.exe
  • Tune FastCGI and php.ini

Explanation#

Install Web Server → Application Development → CGI (this also enables FastCGI). Download the Non Thread Safe x64 build of PHP from windows.php.net and extract it to C:\PHP. Install the matching Visual C++ Redistributable.

In IIS Manager: Handler Mappings → Add Module Mapping: Request path *.php, Module FastCgiModule, Executable C:\PHP\php-cgi.exe, Name PHP_via_FastCGI. Accept the offer to create a FastCGI application.

FastCGI Settings → the php-cgi.exe entry: set Instance MaxRequests to 10000 and the environment variable PHP_FCGI_MAX_REQUESTS to the same value, so processes are recycled before they leak memory.

Copy php.ini-production to php.ini and set extension_dir, enable the extensions you need (mysqli, pdo_mysql, openssl, mbstring), and set cgi.force_redirect = 0 and fastcgi.impersonate = 1. Add index.php to Default Document.

Step-by-step screens#

Drawings of IIS Manager in IIS 10 (Windows Server 2019/2022, Windows 10/11). The red numbers match the numbered actions under each picture; names on your computer will differ.

Loading pictures…

Code example: The same setup with appcmd#

PowerShellThe same setup with appcmd Illustrative
Install-WindowsFeature Web-CGI
$appcmd = "$env:windir\system32\inetsrv\appcmd.exe"
& $appcmd set config /section:system.webServer/fastCGI "/+[fullPath='C:\PHP\php-cgi.exe',instanceMaxRequests='10000']"
& $appcmd set config /section:system.webServer/fastCGI "/[fullPath='C:\PHP\php-cgi.exe'].environmentVariables.[name='PHP_FCGI_MAX_REQUESTS',value='10000']"
& $appcmd set config /section:system.webServer/handlers "/+[name='PHP_via_FastCGI',path='*.php',verb='*',modules='FastCgiModule',scriptProcessor='C:\PHP\php-cgi.exe',resourceType='Either']"
& $appcmd set config "Blog" /section:defaultDocument "/+files.[@start,value='index.php']"
Set-Content C:\inetpub\blog\info.php '<?php phpinfo();'
Expected output
http://blog.example/info.php shows the PHP information page with “Server API: CGI/FastCGI”. Delete info.php after checking.

This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.

Step-by-step explanation#

  1. Web-CGI installs FastCGI support.
  2. The fastCGI entry registers php-cgi.exe and recycles each process after 10,000 requests.
  3. The handler sends *.php requests to it.
  4. index.php is added at the top of the Blog site’s default documents.
  5. phpinfo() confirms the setup.

Real-world use case#

A company runs its WordPress blog next to its ASP.NET site on the same Windows server, both behind IIS with the same certificate management.

Common mistakes#

Thread Safe PHP build

Use the Non Thread Safe build with FastCGI.

Leaving info.php online

It reveals versions and paths. Delete it after testing.

Best practices#

  • Non Thread Safe x64 PHP
  • Recycle FastCGI processes
  • Production php.ini
  • Give the pool identity only the folder permissions it needs

Remember this#

CGI role service = FastCGI.

*.php → FastCgiModule → php-cgi.exe.

MaxRequests = PHP_FCGI_MAX_REQUESTS.

Practice exercise#

Your task: Which PHP build do you download for IIS FastCGI?

Show solution
Text Illustrative
Non Thread Safe (NTS) x64

Mini quiz#

Hosting PHP with FastCGI quizMultiple choice Question 1 of 1

Which IIS module handles PHP requests?

Score so far: 0/0

Want more? Practise all IIS MCQs · IIS interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.