php and fastcgi performance optimizations

29
PHP + FastCGI Performance Optimizations Alessandro Pilotti Twitter: @alexpilotti MVP ASP.NET / IIS MCSD, MCAD, MCSE, MCDBA, MCT Red Hat Certified Engineer

Upload: alessandro-pilotti

Post on 13-May-2015

10.479 views

Category:

Technology


2 download

DESCRIPTION

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.

TRANSCRIPT

Page 1: PHP and FastCGI Performance Optimizations

PHP + FastCGI Performance Optimizations

Alessandro PilottiTwitter: @alexpilottiMVP ASP.NET / IIS

MCSD, MCAD, MCSE, MCDBA, MCTRed Hat Certified Engineer

Page 2: PHP and FastCGI Performance Optimizations

PHP on IISFastCGI

Best available option

ISAPIThe way to go before FastCGILow reliability due to reentrance problemsDLL loaded at W3SVC instance level

Security issues

CGI (not FastCGI)Awful performance

Page 3: PHP and FastCGI Performance Optimizations

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?

Page 4: PHP and FastCGI Performance Optimizations

WebPIMicrosoft 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%\Microsoft\web platform installer\"

Page 5: PHP and FastCGI Performance Optimizations

Deploy PHP via WebPI

Page 6: PHP and FastCGI Performance Optimizations

Deploy PHP via WebPI

Page 7: PHP and FastCGI Performance Optimizations

Deploy PHP via WebPI

Page 8: PHP and FastCGI Performance Optimizations

Deploy PHP via WebPI

Page 9: PHP and FastCGI Performance Optimizations

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: https://wiki.php.net/internals/windows/stepbystepbuild PHP build extras

http://windows.php.net/downloads/php-sdk/ Deps and binaries: bison.exe, flex.exe, etc. and additional include and

libs Visual Studio 2008 or 2010

The Express edition (free) is enough

Page 10: PHP and FastCGI Performance Optimizations

Compiling PHP Unpack the downloaded sources, e.g in c:\php-src

Use e.g 7-zip to inflate tar.gz or tar.bz

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%;win32build\bin set INCLUDE=%INCLUDE%;win32build\include set LIB=%LIB%;win32build\lib buildconf configure –disable-zts (and required extensions) nmake

Copy binaries (php-cgi.exe and php5.dll) to your PHP directory

Page 11: PHP and FastCGI Performance Optimizations

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

Steps1. 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/

Page 12: PHP and FastCGI Performance Optimizations

Demo

WebPI

PHP Deployment

Page 13: PHP and FastCGI Performance Optimizations

CGI and FastCGICGI (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

Page 14: PHP and FastCGI Performance Optimizations

IIS FastCGI 1.5CGI 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!

Page 15: PHP and FastCGI Performance Optimizations

FastCGI Setup

Page 16: PHP and FastCGI Performance Optimizations

IIS FastCGI - VersionsFeature 5.1/6.0 7.0 7.5

Monitoring file changes

Yes Yes Yes

Real-Time max Inst. tuning

Yes Yes Yes

STDERR support Yes Yes Yes

SIGTERM support Yes Yes Yes

Env. variable _FCGI_X_PIPE_

Yes Yes Yes

Proc. Pool per application

Yes Yes Yes

UTF-8 variable encoding

Yes Yes No

IIS CPU Limit support

No Yes Yes

Page 17: PHP and FastCGI Performance Optimizations

FastCGI - PHPInstall PHP 5.2 or 5.3

During setup choose FastCGI

As an alternative, deploy via WebPI

WP3P.exeLoads iisfcgi.dllExecutes php-cgi.exe

Config: PHP.INIE.g.: fastcgi.impersonate = 1

Page 18: PHP and FastCGI Performance Optimizations

FastCGI - Settings

Page 19: PHP and FastCGI Performance Optimizations

Handler Mappings

Page 20: PHP and FastCGI Performance Optimizations

PHP.INI FastCGI Settingsfastcgi.impersonate

supports the ability to impersonate security tokens of the calling client

Default: 1

fastcgi.loggingTurns on SAPI logging when using FastCGIDefault: 1

Page 21: PHP and FastCGI Performance Optimizations

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)\PHP\v5.3\php-cgi.exe" resourceType="Either" /> </handlers> </system.webServer></configuration>

Page 22: PHP and FastCGI Performance Optimizations

PHP ManagerA dedicated tool to support multiple PHP installations

Features:Easy management of the PHP version used by a web

appEasy management of the PHP parameters

No direct editing of PHP.INI neededEasy management of PHP ExtensionsBest practicesQuick inspections via phpinfo()Easy registration of new PHP installations

Deployable via WebPI or standalone setup

Page 23: PHP and FastCGI Performance Optimizations

PHP Manager

Page 24: PHP and FastCGI Performance Optimizations

Set PHP Limits

Page 25: PHP and FastCGI Performance Optimizations

PHP Error Reporting

Page 26: PHP and FastCGI Performance Optimizations

Manage PHP.INI Settings

Page 27: PHP and FastCGI Performance Optimizations

phpinfo() Output

Page 28: PHP and FastCGI Performance Optimizations

Deploy PHP via WebPI

Page 29: PHP and FastCGI Performance Optimizations

Optimize PHP with PGO