Tuning Apache and PHP for Speed on Unix


Also read my essay Optimizing PHP for a more in depth coverage of these issues with case studies.

To tune well, you need to benchmark your Web server. You can get some benchmark figures using ApacheBench (ab) or httperf. If you are an OS agnostic like me, I recommend using Microsoft's excellent free Web Application Stress Tool  (WAST - requires M'soft Windows).  WAST is more flexible than ab because it allows you to define different GET parameters for each thread. This is important because it allows you to simulate multiple PHP sessions via the PHPSESSID GET parameter. Avoid benchmarks involving PHP sessions when using ab as the sessions will become an artificial bottleneck. More info on using WAST with PHP.

To monitor the Apache server, I use the command top d 1 which displays CPU and memory usage of all processes on the machine, and apachectl status.


  1. General rule of thumb for hardware upgrades: For PHP scripts, the main bottleneck is the CPU. For static HTML/images, the bottleneck is RAM and the network. According to Compaq benchmarks in 1999 (the original article is lost due to bitrot), a  slow 400 Mhz Pentium can saturate a T3 line (that's 45 Mbps) with static HTML pages.



  2. A PHP script will be served at least 2-10 times slower than a static HTML page by Apache. Try to use more static HTML pages and fewer scripts.



  3. Enable the compression of HTML by putting in your php.ini:

        output_handler = ob_gzhandler

    If you think about it, it might take you 0.1 seconds to generate  40K of HTML in your PHP page. However it probably takes 6 seconds for the user to download the page using a 56k modem without compresson. With compression, the download will probably take 2-3 seconds.

    So the time taken for page generation is miniscule in comparison to the transit time of the HTML from the server to the browser. Therefore the biggest speedup you can perform for modem users is using ob_gzhandler! This feature is only recommended for PHP 4.1.0 or later. This point was moved closer to the top of the list on 9 July 2002 when i personally experienced the benefits of compression.  More info...



  4. Your PHP scripts are recompiled every time unless the scripts are cached. Install a PHP caching product (I recommend Turck MMCache) to typically increase performance by 25-100% by removing compile times.



  5. Switch from file based sessions to shared memory sessions. Compile PHP with the --with-mm option and set session.save_handler=mm in php.ini. Informal benchmarks suggest that session management time is halved by this simple change. Added 1 Dec 2001. This hint should only be used for PHP 4.2.0 and above as there were bugs before this.



  6. An alternative caching technique when you have pages that don't change too frequently is to cache the HTML output of your PHP pages. Try Smarty or Cache Lite.



  7. Use output buffering (See ob_start). This will speed up your PHP code by 5-15% if you frequently print or echo in your code. Note that output buffering is already enabled if you are using the above ob_gzhandler hint. ASP does this in IIS 5. Added 26 Nov 2001.



  8. On Windows, FastCGI is the highest performance way of running PHP with Apache. Although PHP4 can run in a threaded environment, some global locks prevent it from making full use of threads. Also PHP is not very stable in threaded environments because many common extensions are not thread-safe. Added Feb 2004.



  9. In PHP4, objects and arrays should be passed in and out of functions by reference (with &), and everything else by value. In PHP5, objects are automatically passed by reference. For example the following gives best performance:

    function &test(&$obj_or_array)
    {
     return $obj_or_array;
    }
    $var =& test($obj);
    



  10. Be miserly and sparse with your server and web pages. Don't run X-Windows on the server and other unneeded processes. Apache Today has a guide on how to remove them.



  11. Don't use images when text will do. Reduce your image sizes with a software like Adobe ImageReady or MacroMedia Fireworks. Avoid dithered images as they tend to compress poorly.



  12. Spread the workload. Run your SQL server on another machine. Serve graphics and HTML from another low-end computer. If all static content is served from another server, then you can turn off KeepAlives in httpd.conf on the PHP server to speed up disconnects. 1 Feb 2002: I am currently using tux as the static web server, and have set it to pass all .php files to Apache which resides on the same machine. 15 March 2002: thttpd is another popular static web server.



  13. Use hdparm to tune your hard disk. If you are using a default Linux install, this could speed up your hard disk by 200%. This is mostly useful for IDE hard disks, but some hdparm settings work with SCSI also.



  14. Modify the following httpd.conf parameters to:

    # disable DNS lookups: PHP scripts only get the IP address
    HostnameLookups off 
    # disable htaccess checks <Directory /> AllowOverride none   </Directory>
    and turn on follow FollowSymLinks and turn off SymLinksIfOwnerMatch (correction by Joshua Slive) to prevent additional lstat() system calls from being made:
     Options FollowSymLinks 
     #Options SymLinksIfOwnerMatch
    
    There are many other httpd.conf tips below.



你可能感兴趣的:(linux,PHP,on,UP,Speed)