PHP and FastCGI Performance Optimizations


Published on

PHP is a first class citizen on IIS. A lot has been done in order to make sure that PHP can work well and fast on Windows. We will start by installing PHP with PHP Manager and discussing all the options including what thread safe vs non thread safe and VC6 vs VC9 means. Next we'll take a look at how to optimize the FastCGI IIS extension that Microsoft and Zend developed to provide a secure and performant environment for PHP applications. The last part of this webcast will show how to build PHP with Profile Guided Optimizations (PGO), a technique that can provide a significant performance boost in a wide range of applications.

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

PHP and FastCGI Performance Optimizations

  1. 1. PHP + FastCGIPerformance Optimizations Alessandro Pilotti Twitter: @alexpilotti MVP ASP.NET / IIS MCSD, MCAD, MCSE, MCDBA, MCT Red Hat Certified Engineer
  2. 2. PHP on IIS FastCGI  Best available option ISAPI  The way to go before FastCGI  Low reliability due to reentrance problems  DLL loaded at W3SVC instance level  Security issues CGI (not FastCGI)  Awful performance
  3. 3. How to get PHP on Windows Binaries  http://windows.php.NET/download/ Visual Studio C++ CRT DLL: VC6 or VC9?  Apache: VC6  IIS: VC9 Thread safe or not thread safe?  Thread safe for ISAPI  Non Thread Safe for CGI/FastCGI  Massive performance improvement! x86 vs x64  Do you need > 4GB of process space?
  4. 4. WebPI Microsoft Web Platform Installer (WebPI) is a free tool that simplifies deployment of web solutions  IIS features and extensions  WinCache, PHP Drivers for SQL Server, PHP Manager  Frameworks  .NET, PHP, etc  Web applications  Drupal, Joomla, Wordpress, DotNetNuke, Orchard, etc  Database  MS SQL Server Express, MySQL, etc  WebMatrix Both PHP 5.2 and 5.3 can be easily deployed via WebPI Local cache:  "%localappdata%Microsoftweb platform installer"
  5. 5. Deploy PHP via WebPI
  6. 6. Deploy PHP via WebPI
  7. 7. Deploy PHP via WebPI
  8. 8. Deploy PHP via WebPI
  9. 9. Compiling PHP If you want:  A PHP release available in sources  A development build for testing purposes What do you need:  Sources, e.g.: http://php.NET/downloads.php  Subversion client (e.g. TortoiseSVN)  Instructions:   PHP build extras   Deps and binaries: bison.exe, flex.exe, etc. and additional include and libs  Visual Studio 2008 or 2010  The Express edition (free) is enough
  10. 10. Compiling PHP Unpack the downloaded sources, e.g in c:php-src  Use e.g 7-zip to inflate tar.gz or Or via SVN:  svn checkout https://svn.php.NET/repository/php/php-src/branches/PHP_5_4 php-src-5.4 Unpack the Win32 build extras in the same dir Start a Visual Studio 2010 command prompt  cd php-src  set PATH=%PATH%;win32buildbin  set INCLUDE=%INCLUDE%;win32buildinclude  set LIB=%LIB%;win32buildlib  buildconf  configure –disable-zts (and required extensions)  nmake Copy binaries (php-cgi.exe and php5.dll) to your PHP directory
  11. 11. Profile Guided Optimizations (PGO) Feature of the MS C/C++ compiler (VS Professional or Premium) Patch to PHP build environment available here:  http://www.ksingla.NET/wp- content/uploads/2010/05/pgo_build.patch.txt Steps 1. compile PHP with instrumentation enabled --enable-pgi 2. collect training data (.pgc files) 3. compile PHP with PGO –with-pgo Expected performance improvement: 10-17% More details  http://www.ksingla.NET/2010/05/php-pgo-build-for-maximum- performance-on-windows/
  12. 12. Demo
  13. 13. CGI and FastCGI CGI (Common Gateway Interface) is the oldest way used to generate dynamic content on a web server  A separate process is spawned for each request (!)  Request input and response output are handled via standard input / output  Massive overhead due to process handling by the OS FastCGI is a variation  A separate process is spawned but persists among multiple requests  Request input and response output are handled via sockets or named pipes  Processes can be restarted independently  Good performance  Very popular in the Apache environment (mod_fastcgid), for PHP, Perl, Ruby, etc
  14. 14. IIS FastCGI 1.5 CGI is slow: every request generates a process instance! FastCGI employs the same process for many requests Supported on IIS 7.x and IIS 6.0 (the latter via fcgiext.dll ISAPI) Written in collaboration with Zend (PHP) but is generic In IIS 7.x: Enable CGI among the IIS role services Note: Install Hotfix KB980363 on IIS 7.0 to get version 1.5!
  15. 15. FastCGI Setup
  16. 16. IIS FastCGI - VersionsFeature 5.1/6.0 7.0 7.5Monitoring file Yes Yes YeschangesReal-Time max Inst. Yes Yes YestuningSTDERR support Yes Yes YesSIGTERM support Yes Yes YesEnv. variable Yes Yes Yes_FCGI_X_PIPE_Proc. Pool per Yes Yes YesapplicationUTF-8 variable Yes Yes NoencodingIIS CPU Limit support No Yes Yes
  17. 17. FastCGI - PHP Install PHP 5.2 or 5.3  During setup choose FastCGI As an alternative, deploy via WebPI WP3P.exe  Loads iisfcgi.dll  Executes php-cgi.exe Config: PHP.INI  E.g.: fastcgi.impersonate = 1
  18. 18. FastCGI - Settings
  19. 19. Handler Mappings
  20. 20. PHP.INI FastCGI Settings fastcgi.impersonate  supports the ability to impersonate security tokens of the calling client  Default: 1 fastcgi.logging  Turns on SAPI logging when using FastCGI  Default: 1
  21. 21. Web.config - FastCGI<configuration>… <system.webServer> … <handlers> <clear /> <add name="PHP53_via_FastCGI" path="*.php" verb="GET,HEAD,POST" modules="FastCgiModule" scriptProcessor="C:Program Files (x86)PHPv5.3php-cgi.exe" resourceType="Either" /> </handlers> </system.webServer></configuration>
  22. 22. PHP Manager A dedicated tool to support multiple PHP installations Features:  Easy management of the PHP version used by a web app  Easy management of the PHP parameters  No direct editing of PHP.INI needed  Easy management of PHP Extensions  Best practices  Quick inspections via phpinfo()  Easy registration of new PHP installations Deployable via WebPI or standalone setup
  23. 23. PHP Manager
  24. 24. Set PHP Limits
  25. 25. PHP Error Reporting
  26. 26. Manage PHP.INI Settings
  27. 27. phpinfo() Output