设为首页 收藏本站
查看: 490|回复: 0

[经验分享] Tuning Apache and PHP for Speed on Unix-IT博客

[复制链接]

尚未签到

发表于 2018-11-23 13:16:30 | 显示全部楼层 |阅读模式
  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.


  •   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.
  •   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.
  •   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...
  •   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.
  •   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.

  •   An>
  •   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.

  •   On Windows, FastCGI is the highest performance way of running PHP with Apache.>
  •   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);
      

  •   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.

  •   Don't use images when text will do. Reduce your image>
  •   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.

  •   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>
  •   Modify the following httpd.conf parameters to:
    # disable DNS lookups: PHP scripts only get the IP address  
    HostnameLookups off
      
    # disable htaccess checks
      

      
    AllowOverride none  
      

      

    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.



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-638709-1-1.html 上篇帖子: Apache2.4+php5.3无法加载Zend问题 下篇帖子: apache 添加模块
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表