Hosting PHP with FastCGI
IIS runs PHP through FastCGI, which keeps PHP processes alive between requests for good performance.
On this page · 5 min · Advanced
On this page
- Learning objectives
- Explanation
- Step-by-step screens
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Advanced
- Updated
- Nov 26, 2026
Related tutorials
Interview questions
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#
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();'
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#
- Web-CGI installs FastCGI support.
- The fastCGI entry registers php-cgi.exe and recycles each process after 10,000 requests.
- The handler sends *.php requests to it.
- index.php is added at the top of the Blog site’s default documents.
- 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#
Use the Non Thread Safe build with FastCGI.
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
Non Thread Safe (NTS) x64
Mini quiz#
Which IIS module handles PHP requests?
Want more? Practise all IIS MCQs · IIS interview questions
My notes#
Saved in this browser.