Skip to content

How to Install PHP on IIS with FastCGI on Windows Server

5 min read

Applies to: IIS 10 on Windows Server 2016 to 2025 with PHP 8.2 to 8.5 (Non-Thread Safe, x64); steps from php.net and Microsoft’s documentation, php.ini directives checked in PHP 8.3.35, as Windows can’t run in our Docker test lab

To run PHP on IIS: install IIS with the CGI role service, unzip the Non-Thread Safe (NTS) x64 build of PHP to C:\PHP, install the Visual C++ Redistributable, create a php.ini, and add a Handler Mapping that sends *.php to C:\PHP\php-cgi.exe through the FastCgiModule. IIS then runs PHP through FastCGI.

1. Install IIS with CGI

In PowerShell as administrator:

Install-WindowsFeature -Name Web-Server, Web-CGI -IncludeManagementTools

In Server Manager: Manage, Add Roles and Features, then under Web Server (IIS), Web Server, Application Development, tick CGI. According to Microsoft, installing the CGI role service also sets up FastCGI; no other steps are needed.

2. Download PHP and the runtime

  • From the PHP for Windows download site (windows.php.net, linked from php.net), take the x64 Non Thread Safe zip of a supported version. php.net says the NTS build is the one for IIS and other FastCGI servers; the Thread-Safe build is for Apache with mod_php.
  • PHP needs the Microsoft Visual C++ runtime. According to php.net, the Visual C++ Redistributable for Visual Studio 2022 works for all PHP versions; use the x64 one for x64 PHP. Its installer accepts /quiet /norestart.
  • PHP for Windows runs only on x86 and x64, not on Windows on ARM, and from PHP 8.3 needs Windows Server 2012 or newer.

Unzip PHP to C:\PHP. The folder contains php-cgi.exe, the ext folder and two templates, php.ini-production and php.ini-development.

3. Create php.ini

Copy php.ini-production to php.ini, find these lines, remove the ; in front and set the values shown:

extension_dir = "ext"
cgi.force_redirect = 0
cgi.fix_pathinfo = 1
fastcgi.impersonate = 1
date.timezone = "Asia/Kolkata"
zend_extension=opcache
opcache.enable = 1
extension=curl
extension=mbstring
extension=mysqli
extension=openssl
extension=pdo_mysql

The comments in PHP’s own php.ini explain why: for cgi.force_redirect it says “You CAN safely turn this off for IIS, in fact, you MUST.”, and for fastcgi.impersonate “Set to 1 if running under IIS. Default is zero.” php.net also recommends enabling OPcache. On PHP 8.5 leave out the zend_extension=opcache line: according to the PHP 8.5 migration notes, OPcache is now always built in and loaded. Use your own time zone and only the extensions your application needs.

4. Map .php files to PHP in IIS Manager

  1. Open Server Manager, Tools, Internet Information Services (IIS) Manager.
  2. In Connections, select the server name (for all sites) or one site.
  3. Double-click Handler Mappings, then Add Module Mapping in the Actions pane.
  4. Request path: *.php; Module: FastCgiModule; Executable: C:\PHP\php-cgi.exe (not php.exe); Name: PHP-FastCGI.
  5. Under Request Restrictions, tick Invoke handler only if request is mapped to and choose File or Folder. Select OK.
  6. When asked whether to create a FastCGI application for this executable, select Yes.

Then open Default Document, choose Add... and add index.php, so a folder URL opens it.

The same from the command line

php.net and Microsoft both document appcmd for scripting this:

%windir%\system32\inetsrv\appcmd set config /section:system.webServer/fastCGI /+[fullPath='C:\PHP\php-cgi.exe']
%windir%\system32\inetsrv\appcmd set config -section:system.webServer/handlers /+"[name='PHP-FastCGI',path='*.php',verb='GET,HEAD,POST',modules='FastCgiModule',scriptProcessor='C:\PHP\php-cgi.exe',resourceType='Either',requireAccess='Script']" /commit:apphost
%windir%\system32\inetsrv\appcmd set config -section:system.webServer/fastCgi /+"[fullPath='C:\PHP\php-cgi.exe'].environmentVariables.[name='PHPRC',value='C:\PHP\php.ini']"
%windir%\system32\inetsrv\appcmd set config "Default Web Site" /section:defaultDocument /enabled:true /+files.[value='index.php']

PHPRC tells php-cgi.exe which php.ini to load.

5. Test

Create C:\inetpub\wwwroot\info.php containing <?php phpinfo(); and open http://localhost/info.php. Check that Server API shows CGI/FastCGI (as it did for php-cgi in our Linux test) and that Loaded Configuration File is C:\PHP\php.ini. Delete the file afterwards: it shows your configuration to anyone.

If it doesn’t work

  • The browser downloads the .php file or shows its code: the handler mapping is missing or on the wrong site.
  • C:\PHP\php-cgi.exe -v in a command prompt doesn’t print the PHP version and complains about a missing DLL: install the Visual C++ Redistributable (x64).
  • A 500 error: run php-cgi.exe -v and php -m in C:\PHP to spot php.ini or extension errors, and see 500 Internal Server Error for PHP in IIS.

Running several sites? Microsoft’s FastCGI reference shows how to give each site its own FastCGI application with a different open_basedir. For HTTPS, see SSL certificate installation in IIS. A Windows VPS gives you the administrator access IIS needs.

Official documentation: PHP installation with IIS, PHP Windows requirements and builds and IIS FastCGI configuration.

Related services

Hire an expert

engineers per 15 minutes, per hour or per month, with no contract.

Server management

on-demand administration, hardening, migrations and monitoring.

Was this answer helpful?

Still stuck? Open a ticket