Thursday, September 20, 2012

Windows Server 2012 (Server-Core) - Setup IIS 8 with PHP

What you need
  1. Microsoft Visual C++ 2008 Redistributable Package (x64)
  2. PHP 5.4.7 - Non Thread Safe Build
  3. Some zip software to extract the files (e.g. 7-Zip)
Pre-requisites
Install Microsoft Visual C++ 2008 Redistributable Package (x64) and copy the contents from the php zipped file to C:\PHP.

Installing IIS and CGI (FastCGI)
We use DISM (Deployment Image Servicing and Management) to enable IIS feature in Server-Core. The command is:

Start /WAIT DISM /Online /Enable-Feature /FeatureName:IIS-WebServerManagementTools /FeatureName:IIS-ManagementConsole /FeatureName:IIS-WebServerRole /FeatureName:IIS-WebServer /FeatureName:IIS-CommonHttpFeatures /FeatureName:IIS-StaticContent /FeatureName:IIS-DefaultDocument /FeatureName:IIS-DirectoryBrowsing /FeatureName:IIS-HttpErrors /FeatureName:IIS-HealthAndDiagnostics /FeatureName:IIS-HttpLogging /FeatureName:IIS-Performance /FeatureName:IIS-HttpCompressionStatic /FeatureName:IIS-Security /FeatureName:IIS-RequestFiltering /FeatureName:IIS-CGI

For the bulk of the command, it is to basically enable IIS 8 with common features. The only additional feature is IIS-CGI which is required for PHP.

Configuring PHP
Create a copy of php.ini from php.ini-production. As per the manual, follow the section on Changing the php.ini file

Configuring IIS for PHP
The following are the commands to configure IIS for PHP and these are derived from the PHP Manual at http://php.net/manual/en/install.windows.iis7.php.

%windir%\system32\inetsrv\appcmd.exe set config /section:system.webServer/fastCGI ^/+[fullPath='c:\PHP\php-cgi.exe']

%windir%\system32\inetsrv\appcmd.exe set config /section:system.webServer/handlers ^/+[name='PHP_via_FastCGI',path='*.php',verb='*',modules='FastCgiModule',scriptProcessor='c:\PHP\php-cgi.exe',resourceType='Either']

%windir%\system32\inetsrv\appcmd.exe set config ^-section:system.webServer/defaultDocument /+"files.[value='index.php']" ^/commit:apphost

%windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/fastCgi ^/[fullPath='c:\php\php-cgi.exe'].instanceMaxRequests:10000

%windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/fastCgi ^/+[fullPath='C:\php\php-cgi.exe'].environmentVariables.^[name='PHP_FCGI_MAX_REQUESTS',value='10000']

%windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/fastCgi ^/[fullPath='C:\php\php-cgi.exe',arguments=''].activityTimeout:"90"  /commit:apphost

%windir%\system32\inetsrv\appcmd.exe set config -section:system.webServer/fastCgi ^/[fullPath='C:\php\php-cgi.exe',arguments=''].requestTimeout:"90"  /commit:apphost

%windir%\system32\inetsrv\appcmd.exe set config  -section:system.webServer/fastCgi ^/+[fullPath='C:\php\php-cgi.exe',arguments=''].environmentVariables.^[name='PHPRC',value='C:\php\php.ini'] /commit:apphost


Firewall Ports
Since this is a new setup and by default Windows Firewall is turned on, you will need to perform changes to Windows Firewall. Here are some commands you can choose to run depending on your needs. Personally I do not recommend disabling Windows Firewall.

  • Allow ICMP pings to web server for ping tests

    netsh advfirewall firewall add rule name="ICMP Allow incoming V4 echo request" protocol=icmpv4:8,any dir=in action=allow
  • Allow Port 80 (HTTP)

    netsh advfirewall firewall add rule name="Allow Port 80 (HTTP)" dir=in action=allow protocol=TCP localport=80

  • Allow Port 443 (HTTPS)

    netsh advfirewall firewall add rule name="Allow Port 443 (HTTPS)" dir=in action=allow protocol=TCP localport=443
Testing
At C:\inetpub\wwwroot, create index.php and enter the following codes:

<?php
phpinfo();
?>

Finally, go to a machine with a web browser. Browse to http://{servername}/index.php. The setup is complete once the PHP info page displays successfully,



Closing Remarks: This is an update to an older post, a newer version of PHP is used and firewall commands are added.

1 comment: