You are previewing Pro PHP Application Performance Tuning PHP Web Projects for Maximum Performance.
O'Reilly logo
Pro PHP Application Performance Tuning PHP Web Projects for Maximum Performance

Book Description

This book contains tips, tricks, and techniques to make new and existing PHP applications much faster and less resource-hungry. Author Armando Padilla looks at all major sections of the PHP application stack—from server hardware to database and web server performance. PHP program application performance is the center of this book, and Padilla shows you how to improve performance by best practices, proper design, and best-of-breed tools.

Padilla starts out by establishing performance testing and benchmarking methodology using common open source and browser-based testing tools. He then discusses best practices for PHP development, and also explains refactoring existing code, both with performance in mind. In addition to best coding practices, this book will teach you how to use and tweak Opcode caches such as APC, XCache, and eAccelerator.

Padilla then highlights how to support the efficient running of PHP applications, tweaks and settings by taking advantage of the GNU/Linux and Microsoft Windows operating systems to improve hosting performance and reliability. Additional tips on web server improvements and database administration help round out the picture. Finally, Padilla presents some hardware example configurations tuned for high performance.

Table of Contents

  1. Copyright
  2. About the Authors
  3. About the Technical Reviewer
  4. Acknowledgments
  5. Introduction
    1. Overview
      1. Chapter 1 – Benchmarking Techniques
      2. Chapter 2 – Improving Client Download and Rendering Performance
      3. Chapter 3 – PHP Code Optimization
      4. Chapter 4 – Opcode Caching
      5. Chapter 5 – Variable Caching
      6. Chapter 6 – Choosing your web server.
      7. Chapter 7 – Apache web server optimization
      8. Chapter 8 – Database optimization.
  6. 1. Benchmarking Techniques
    1. 1.1. The PHP Application Stack
    2. 1.2. Benchmarking Utilities
    3. 1.3. Defining the Request/Response Lifecycle
    4. 1.4. Apache Benchmark
      1. 1.4.1. Installing Apache Benchmark
        1. 1.4.1.1. Unix and Mac Installation
        2. 1.4.1.2. Windows Installation
      2. 1.4.2. Running Apache Benchmark
      3. 1.4.3. Making Sense of the Response
        1. 1.4.3.1. Server Information
        2. 1.4.3.2. Script Information
        3. 1.4.3.3. Connection Information
        4. 1.4.3.4. Connection Metrics Breakdown
      4. 1.4.4. AB Option Flags
        1. 1.4.4.1. Concurrency Tests
        2. 1.4.4.2. Timed Tests
      5. 1.4.5. AB Gotchas
    5. 1.5. Siege
      1. 1.5.1. Installing Siege
      2. 1.5.2. Running Siege
      3. 1.5.3. Examining the Results
        1. 1.5.3.1. Individual Request Details
        2. 1.5.3.2. Test Metrics
      4. 1.5.4. Siege Option Flags
      5. 1.5.5. Testing Many URLs
        1. 1.5.5.1. URL Format and File
    6. 1.6. Affecting Your Benchmark Figures
      1. 1.6.1. Geographical Location
      2. 1.6.2. The Traveling Packets
      3. 1.6.3. Response Size
      4. 1.6.4. Code Complexity
      5. 1.6.5. Browser Behavior
      6. 1.6.6. Web Server Setup
    7. 1.7. Summary
  7. 2. Improving Client Download and Rendering Performance
    1. 2.1. The Importance of Optimizing Responses
    2. 2.2. Firebug
      1. 2.2.1. Installing Firebug
      2. 2.2.2. Firebug Performance Tabs
      3. 2.2.3. The Console Tab
        1. 2.2.3.1. Running JavaScript Profiler on a Web Page
      4. 2.2.4. The Net Tab
    3. 2.3. YSlow
      1. 2.3.1. YSlow v2 Rulesets
        1. 2.3.1.1. CSS Optimization Rules
        2. 2.3.1.2. Image Optimization Rules
        3. 2.3.1.3. JavaScript Optimization
        4. 2.3.1.4. Server Optimization
      2. 2.3.2. Installing YSlow
      3. 2.3.3. Starting YSlow
        1. 2.3.3.1. The Grade Tab
        2. 2.3.3.2. Statistics Tab
        3. 2.3.3.3. Tools Tab
    4. 2.4. Page Speed
      1. 2.4.1. Installing Page Speed
      2. 2.4.2. Page Speed at Work
    5. 2.5. Optimization Tools
      1. 2.5.1. JavaScript Optimization
      2. 2.5.2. JavaScript Placement
      3. 2.5.3. Minification of JavaScript
    6. 2.6. Minification Tools
    7. 2.7. YUI Compressor
    8. 2.8. Closure Compiler
      1. 2.8.1. Reduce Resource Requests
      2. 2.8.2. Use Server-Side Compression
    9. 2.9. Image Compression
    10. 2.10. Smush.it
    11. 2.11. Summary
  8. 3. PHP Code Optimization
    1. 3.1. PHP Best Practices
      1. 3.1.1. The PHP Economy
      2. 3.1.2. require vs. require_once
      3. 3.1.3. Calculating Loop Length in Advance
      4. 3.1.4. Accessing Array Elements Using foreach vs. for vs. while
      5. 3.1.5. File Access
      6. 3.1.6. Faster Access to Object Properties
    2. 3.2. Looking Under the Hood Using VLD, strace, and Xdebug
      1. 3.2.1. Reviewing Opcode Functions with VLD
        1. 3.2.1.1. Installing VLD
      2. 3.2.2. Using strace for C-level Tracing
        1. 3.2.2.1. Installing strace
    3. 3.3. Identifying Bottlenecks
      1. 3.3.1. Xdebug 2: PHP Debugging Tool
        1. 3.3.1.1. Installing Xdebug
        2. 3.3.1.2. Updating the PHP.ini File
      2. 3.3.2. Validating Installation
        1. 3.3.2.1. Running Our First Profiler
      3. 3.3.3. Installing the GUI-Based Tool
        1. 3.3.3.1. Installing WinCacheGrind
        2. 3.3.3.2. Installing KCacheGrind
        3. 3.3.3.3. Analyzing Data
    4. 3.4. Summary
  9. 4. Opcode Caching
    1. 4.1. Reviewing Our Roadmap
    2. 4.2. The PHP Life Cycle
    3. 4.3. Opcode Caching Tools
      1. 4.3.1. Alternative PHP Cache
        1. 4.3.1.1. Installing APC
      2. 4.3.2. Unix Installation
      3. 4.3.3. Windows Installation
        1. 4.3.3.1. Using APC
        2. 4.3.3.2. APC Settings
        3. 4.3.3.3. APC Admin Tool
        4. 4.3.3.4. Installing the Admin tool
      4. 4.3.4. XCache
        1. 4.3.4.1. Unix Installation
        2. 4.3.4.2. Windows Installation
      5. 4.3.5. Caching with XCache
      6. 4.3.6. XCache Settings
      7. 4.3.7. eAccelerator
        1. 4.3.7.1. Unix Installation
      8. 4.3.8. Creating the Cache Folder
      9. 4.3.9. Installing eAccelerator As a PHP Extension
      10. 4.3.10. Making Sure eA Is Installed
        1. 4.3.10.1. Windows Installation
      11. 4.3.11. Creating the eA Directory
      12. 4.3.12. Updating php.ini
      13. 4.3.13. eA Settings
    4. 4.4. Summary
  10. 5. Variable Caching
    1. 5.1. Application Performance Roadmap
    2. 5.2. The Value of Implementing Variable Caching
    3. 5.3. A Sample Project: Creating the Table
      1. 5.3.1. Fetching the Records
      2. 5.3.2. Calculating a Database Fetch
    4. 5.4. APC Caching
      1. 5.4.1. Adding Data to Cache
      2. 5.4.2. Benchmarking APC
    5. 5.5. Memcached
      1. 5.5.1. Installing Memcached
      2. 5.5.2. Starting Memcached Server
      3. 5.5.3. Using Memcached with PHP
        1. 5.5.3.1. Connecting to Memcached Server
        2. 5.5.3.2. Adding Data into Cache
        3. 5.5.3.3. Benchmarking Memcached
    6. 5.6. Summary
  11. 6. Choosing the Right Web Server
    1. 6.1. Choosing Which Web Server Package Is for You
      1. 6.1.1. Security and Stability Are Important to You
      2. 6.1.2. Availability of Engineers with Detailed Knowledge Is Important to You
      3. 6.1.3. Your Site Is Predominantly Static Content
      4. 6.1.4. You Are Hosting in a Managed Service
      5. 6.1.5. You Are Using Unusual PHP Extensions
    2. 6.2. Usage Figures for Web Servers
    3. 6.3. Web Server Request Handling
    4. 6.4. Web Server Hardware
    5. 6.5. Classifying Web Servers
    6. 6.6. Apache HTTPD
      1. 6.6.1. Apache Daemon Command Line
      2. 6.6.2. Apache Multi-processing Modules
        1. 6.6.2.1. The Prefork MPM
    7. 6.7. Understanding Apache Modules
      1. 6.7.1. Adding Dynamic Apache Modules
        1. 6.7.1.1. Using a conf.d Directory
        2. 6.7.1.2. Using a Module Management Helper
      2. 6.7.2. Removing Dynamic Apache Modules
        1. 6.7.2.1. Removing a Module Using a conf.d Directory
        2. 6.7.2.2. Removing a Module Using a Module Management Helper
    8. 6.8. Final Words on Apache
    9. 6.9. lighttpd
      1. 6.9.1. Installing lighttpd
        1. 6.9.1.1. lighttpd on Unix
        2. 6.9.1.2. lighttpd on Windows
      2. 6.9.2. lighttpd Configuration Settings
      3. 6.9.3. Comparing Static Load Content
      4. 6.9.4. Installing PHP on lighttpd
        1. 6.9.4.1. Verifying PHP Installation
        2. 6.9.4.2. Benchmarking PHP Content
        3. 6.9.4.3. Setting Tweaks
    10. 6.10. Nginx
      1. 6.10.1. Installing Nginx
        1. 6.10.1.1. Nginx on Unix
        2. 6.10.1.2. Compile-Time Options
        3. 6.10.1.3. Verifying Installation and Starting Up Nginx
      2. 6.10.2. Windows Installation
    11. 6.11. Nginx As a Static Web Server
      1. 6.11.1. Installing FastCGI PHP
        1. 6.11.1.1. Verifying FastCGI Installation
      2. 6.11.2. NGinx Benchmarking
    12. 6.12. Summary
  12. 7. Web Server and Delivery Optimization
    1. 7.1. Determining the Performance of Your Web Server
      1. 7.1.1. Using ApacheTop, a Real-Time Access Log File Analyzer
    2. 7.2. Understanding the Memory Footprint of Your Application
    3. 7.3. Optimizing Processes in Apache
      1. 7.3.1. Controlling Apache Clients (Prefork MPM)
      2. 7.3.2. Optimizing Memory Use and Preventing Swapping
    4. 7.4. Other Apache Configuration Tweaks
      1. 7.4.1. Using .htaccess Files and AllowOverride
      2. 7.4.2. Using FollowSymlinks
      3. 7.4.3. Using DirectoryIndex
      4. 7.4.4. Hostname Lookup Off
      5. 7.4.5. Keep-Alive On
      6. 7.4.6. Using mod_deflate to Compress Content
    5. 7.5. Scaling Beyond a Single Server
      1. 7.5.1. Using Round-Robin DNS
      2. 7.5.2. Using a Load Balancer
      3. 7.5.3. Using Direct Server Return
      4. 7.5.4. Sharing Sessions Between Members of a Farm
      5. 7.5.5. Sharing Assets with a Shared File System
      6. 7.5.6. Sharing Assets with a Separate Asset Server
      7. 7.5.7. Sharing Assets with a Content Distribution Network
    6. 7.6. Pitfalls of Using Distributed Architectures
      1. 7.6.1. Cache Coherence Issues
      2. 7.6.2. Cache Versioning Issues
      3. 7.6.3. User IP Address Tracking
      4. 7.6.4. Domino or Cascade Failure Effects
      5. 7.6.5. Deployment Failures
    7. 7.7. Monitoring Your Application
      1. 7.7.1. Some Monitoring Systems for You to Investigate
    8. 7.8. Summary
  13. 8. Database Optimization
    1. 8.1. About MySQL
    2. 8.2. Understanding MySQL Storage Engines
      1. 8.2.1. MyISAM: The Original Engine
      2. 8.2.2. InnoDB: The Pro's Choice
      3. 8.2.3. Choosing a Storage Engine
        1. 8.2.3.1. When Your Application Is Mostly Read (> 95 Percent)
        2. 8.2.3.2. When You Need Transactions and Consistency Is Important
        3. 8.2.3.3. When You Have a Complex Schema That Has a Lot of Joined Tables
        4. 8.2.3.4. When Non-stop Operation Is Important
    3. 8.3. Understanding How MySQL Uses Memory
      1. 8.3.1. InnoDB vs. MyISAM Memory Usage
      2. 8.3.2. Per Server vs. per Connection (Thread) Memory Usage
    4. 8.4. Locating Your Configuration File
      1. 8.4.1. Mysqltuner.pl: Tuning Your Database Server's Memory
      2. 8.4.2. Possible Issues with Our Example Server
      3. 8.4.3. Tuning InnoDB
    5. 8.5. Finding Problem Queries
    6. 8.6. Analyzing Problem Queries
    7. 8.7. Recommendations for PHP Database Applications
      1. 8.7.1. Maintaining Separate Read and Write Connections
      2. 8.7.2. Using "utf8" (Multi-byte Unicode) Character Set by Default
      3. 8.7.3. Using "UTC" Date Format
    8. 8.8. Summary
  14. A. Installing Apache, MySQL, PHP, and PECL on Windows
    1. A.1. Installing Apache
      1. A.1.1. Post–Apache Installation
    2. A.2. Installing MySQL
      1. A.2.1. Configuring MySQL
    3. A.3. Installing PHP
      1. A.3.1. Getting PHP5 and MySQL to Talk
    4. A.4. Creating a phpinfo() Script
    5. A.5. Installing PECL
  15. B. Installing Apache, MySQL, PHP, and PECL on Linux
    1. B.1. Fedora 14
      1. B.1.1. Component Versions and Locations
    2. B.2. Ubuntu 10.10
      1. B.2.1. Component Versions and Locations
      2. B.2.2. Tasksel
    3. B.3. PECL