PHP and FastCGI Performance Optimizations
-
Upload
alessandro-pilotti -
Category
Technology
-
view
10.484 -
download
2
description
Transcript of PHP and FastCGI Performance Optimizations
![Page 1: PHP and FastCGI Performance Optimizations](https://reader037.fdocuments.net/reader037/viewer/2022102805/55529913b4c9051f108b4fdf/html5/thumbnails/1.jpg)
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](https://reader037.fdocuments.net/reader037/viewer/2022102805/55529913b4c9051f108b4fdf/html5/thumbnails/2.jpg)
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](https://reader037.fdocuments.net/reader037/viewer/2022102805/55529913b4c9051f108b4fdf/html5/thumbnails/3.jpg)
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](https://reader037.fdocuments.net/reader037/viewer/2022102805/55529913b4c9051f108b4fdf/html5/thumbnails/4.jpg)
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](https://reader037.fdocuments.net/reader037/viewer/2022102805/55529913b4c9051f108b4fdf/html5/thumbnails/5.jpg)
Deploy PHP via WebPI
![Page 6: PHP and FastCGI Performance Optimizations](https://reader037.fdocuments.net/reader037/viewer/2022102805/55529913b4c9051f108b4fdf/html5/thumbnails/6.jpg)
Deploy PHP via WebPI
![Page 7: PHP and FastCGI Performance Optimizations](https://reader037.fdocuments.net/reader037/viewer/2022102805/55529913b4c9051f108b4fdf/html5/thumbnails/7.jpg)
Deploy PHP via WebPI
![Page 8: PHP and FastCGI Performance Optimizations](https://reader037.fdocuments.net/reader037/viewer/2022102805/55529913b4c9051f108b4fdf/html5/thumbnails/8.jpg)
Deploy PHP via WebPI
![Page 9: PHP and FastCGI Performance Optimizations](https://reader037.fdocuments.net/reader037/viewer/2022102805/55529913b4c9051f108b4fdf/html5/thumbnails/9.jpg)
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](https://reader037.fdocuments.net/reader037/viewer/2022102805/55529913b4c9051f108b4fdf/html5/thumbnails/10.jpg)
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](https://reader037.fdocuments.net/reader037/viewer/2022102805/55529913b4c9051f108b4fdf/html5/thumbnails/11.jpg)
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](https://reader037.fdocuments.net/reader037/viewer/2022102805/55529913b4c9051f108b4fdf/html5/thumbnails/12.jpg)
Demo
WebPI
PHP Deployment
![Page 13: PHP and FastCGI Performance Optimizations](https://reader037.fdocuments.net/reader037/viewer/2022102805/55529913b4c9051f108b4fdf/html5/thumbnails/13.jpg)
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](https://reader037.fdocuments.net/reader037/viewer/2022102805/55529913b4c9051f108b4fdf/html5/thumbnails/14.jpg)
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](https://reader037.fdocuments.net/reader037/viewer/2022102805/55529913b4c9051f108b4fdf/html5/thumbnails/15.jpg)
FastCGI Setup
![Page 16: PHP and FastCGI Performance Optimizations](https://reader037.fdocuments.net/reader037/viewer/2022102805/55529913b4c9051f108b4fdf/html5/thumbnails/16.jpg)
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](https://reader037.fdocuments.net/reader037/viewer/2022102805/55529913b4c9051f108b4fdf/html5/thumbnails/17.jpg)
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](https://reader037.fdocuments.net/reader037/viewer/2022102805/55529913b4c9051f108b4fdf/html5/thumbnails/18.jpg)
FastCGI - Settings
![Page 19: PHP and FastCGI Performance Optimizations](https://reader037.fdocuments.net/reader037/viewer/2022102805/55529913b4c9051f108b4fdf/html5/thumbnails/19.jpg)
Handler Mappings
![Page 20: PHP and FastCGI Performance Optimizations](https://reader037.fdocuments.net/reader037/viewer/2022102805/55529913b4c9051f108b4fdf/html5/thumbnails/20.jpg)
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](https://reader037.fdocuments.net/reader037/viewer/2022102805/55529913b4c9051f108b4fdf/html5/thumbnails/21.jpg)
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](https://reader037.fdocuments.net/reader037/viewer/2022102805/55529913b4c9051f108b4fdf/html5/thumbnails/22.jpg)
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](https://reader037.fdocuments.net/reader037/viewer/2022102805/55529913b4c9051f108b4fdf/html5/thumbnails/23.jpg)
PHP Manager
![Page 24: PHP and FastCGI Performance Optimizations](https://reader037.fdocuments.net/reader037/viewer/2022102805/55529913b4c9051f108b4fdf/html5/thumbnails/24.jpg)
Set PHP Limits
![Page 25: PHP and FastCGI Performance Optimizations](https://reader037.fdocuments.net/reader037/viewer/2022102805/55529913b4c9051f108b4fdf/html5/thumbnails/25.jpg)
PHP Error Reporting
![Page 26: PHP and FastCGI Performance Optimizations](https://reader037.fdocuments.net/reader037/viewer/2022102805/55529913b4c9051f108b4fdf/html5/thumbnails/26.jpg)
Manage PHP.INI Settings
![Page 27: PHP and FastCGI Performance Optimizations](https://reader037.fdocuments.net/reader037/viewer/2022102805/55529913b4c9051f108b4fdf/html5/thumbnails/27.jpg)
phpinfo() Output
![Page 28: PHP and FastCGI Performance Optimizations](https://reader037.fdocuments.net/reader037/viewer/2022102805/55529913b4c9051f108b4fdf/html5/thumbnails/28.jpg)
Deploy PHP via WebPI
![Page 29: PHP and FastCGI Performance Optimizations](https://reader037.fdocuments.net/reader037/viewer/2022102805/55529913b4c9051f108b4fdf/html5/thumbnails/29.jpg)
Optimize PHP with PGO