{"id":26,"date":"2015-01-13T06:32:00","date_gmt":"2015-01-13T06:32:00","guid":{"rendered":"http:\/\/www.asianux.org.vn\/index.php\/2015\/01\/13\/25-apache-performance-tuning-tips-2\/"},"modified":"2015-01-13T06:32:00","modified_gmt":"2015-01-13T06:32:00","slug":"25-apache-performance-tuning-tips-2","status":"publish","type":"post","link":"https:\/\/www.asianux.org.vn\/index.php\/2015\/01\/13\/25-apache-performance-tuning-tips-2\/","title":{"rendered":"25 Apache Performance Tuning Tips"},"content":{"rendered":"<p>We all know and love Apache. &nbsp;Its great, it allows us to run websites  on the Internet with minimal configuration and administration.<br \/> However, this same ease of flexibility and lack of tuning, is  typically what leads Apache to becoming a memory hog. &nbsp;Utilizing these  easy to understand tips, you can gain a significant performance boost  from Apache.<span><\/span> <\/p>\n<h3 dir=\"ltr\">Apache Specifics<\/h3>\n<p> <\/p>\n<div style=\"padding-left: 30px;\">1. <strong>Remove unused modules<\/strong> \u2013 save memory by not loading modules that you do not need, including but not limited to mod_php, mod_ruby, mod_perl, etc.<\/div>\n<div style=\"padding-left: 30px;\">2. <strong>Use mod_disk_cache NOT mod_mem_cache <\/strong>&#8211;  mod_mem_cache will not share its cache amongst different apache  processes, which results in high memory usage with little performance  gain since on an active server, mod_mem_cache will rarely serve the same  page twice in the same apache process.<\/div>\n<div style=\"padding-left: 30px;\"><\/div>\n<div style=\"padding-left: 30px;\">3. <strong>Configure mod_disk_cache with a flat hierarchy<\/strong> \u2013 ensure that you are using CacheDirLength=2 and CacheDirLevels=1 to  ensure htcacheclean will not take forever when cleaning up your cache  directory.<\/div>\n<div style=\"padding-left: 30px;\">4. <strong>Setup appropriate Expires, Etag, and Cache-Control Headers<\/strong> \u2013 to utilize your cache, you must tell it when a file expires, otherwise your client will not experience the caching benefits.<\/div>\n<div style=\"padding-left: 30px;\">5. <strong>Put Cache on separate disk<\/strong> \u2013 place your cache on a separate physical disk for fastest access without slowing down other processes.<\/div>\n<div style=\"padding-left: 30px;\">6. <strong>Use Piped Logging instead of direct logging<\/strong> \u2013 directly logging to a file has issues when you want to rotate the log  file. &nbsp;It must restart apache to use the next log file. &nbsp;This will  cause significant slowness for your users during the restart.  &nbsp;Particularly if you are using Passenger or some other app loader.<\/div>\n<div style=\"padding-left: 30px;\">7. <strong>Log to a different disk than disk serving pages<\/strong> \u2013 put your logs on physically different disks than the files you are serving.<\/div>\n<div style=\"padding-left: 30px;\">8. <strong>Utilize mod_gzip\/mod_deflate<\/strong> \u2013 gzip your content before sending it off and then the client will  ungzip upon receipt, this will minimize the size of file transfers, it  generally will help all user experience.<\/div>\n<div style=\"padding-left: 30px;\">9. <strong>Turn HostnameLookups Off<\/strong> \u2013 stop doing expensive DNS lookups. &nbsp;You will rarely ever need them and when you do, you can look them up after the fact.<\/div>\n<div style=\"padding-left: 30px;\">10. <strong>Avoid using hostname in configs<\/strong> \u2013 if you have HostnameLookups off, this will prevent you from having to  wait for the DNS resolve of the hostnames in your configs, use IP  addresses instead.<\/div>\n<div style=\"padding-left: 30px;\">11. <strong>Use Persistent Connections <\/strong>&#8211;  Set KeepAlive On and then set KeepAliveTimeout and KeepAliveRequests.  &nbsp;KeepAliveTimeout is how long apache will wait for the next request, and  KeepAliveRequests is the max number of requests for a client prior to  resetting the connection. &nbsp;This will prevent the client from having to  reconnect between each request.<\/div>\n<div style=\"padding-left: 30px;\">12. <strong>Do Not set KeepAliveTimeout too high<\/strong> \u2013 if you have more requests than apache children, this setting can starve your pool of available clients.<\/div>\n<div style=\"padding-left: 30px;\">13. <strong>Disable .htaccess <\/strong>&#8211; i.e. AllowOverride None &nbsp;This will prevent apache from having to check for a .htaccess file on each request.<\/div>\n<div style=\"padding-left: 30px;\">14. <strong>Allow symlinks<\/strong> \u2013  i.e. Options +FollowSymLinks -SymLinksIfOwnerMatch. &nbsp;Otherwise, apache  will make a separate call on each filename to ensure it is not a  symlink.<\/div>\n<div style=\"padding-left: 30px;\">15. <strong>Set ExtendedStatus Off <\/strong>&#8211;  Although very useful, the ExtendedStatus will produce several system  calls for each request to gather statistics. &nbsp;Better to utilize for a  set time period in order to benchmark, then turn back off.<\/div>\n<div style=\"padding-left: 30px;\">16. <strong>Avoid Wildcards in DirectoryIndex<\/strong> \u2013 use a specific DirectoryIndex, i.e. index.html or index.php, not index<\/div>\n<h3 dir=\"ltr\" style=\"padding-left: 30px;\">OS Specifics<\/h3>\n<p> <\/p>\n<div style=\"padding-left: 30px;\">17. <strong>Increase Swappiness<\/strong> \u2013  particularly on single site hosts this will increase performance. &nbsp;On  linux systems increase \/proc\/sys\/vm\/swappiness to at least 60 if not  greater. &nbsp;This will try to load as many files as possible into the  memory cache for faster access.<\/div>\n<div style=\"padding-left: 30px;\">18.<strong> Increase Write Buffer Size<\/strong> \u2013 increase your write buffer size for tcp\/ip buffers. &nbsp;On linux systems  increase \/proc\/sys\/net\/core\/wmem_max and  \/proc\/sys\/net\/core\/wmem_default. If your pages fit within this buffer,  apache will complete a process in one call to the tcp\/ip buffer.<\/div>\n<div style=\"padding-left: 30px;\">19. <strong>Increase Max Open Files<\/strong> \u2013 if you are handling high loads increase the number of allowed open  files. &nbsp;On linux, increase \/proc\/sys\/fs\/file-max and run ulimit -H -n  4096.<\/div>\n<h3 dir=\"ltr\">Application Specifics<\/h3>\n<p> <\/p>\n<div style=\"padding-left: 30px;\">20. <strong>Setup Frontend proxy for images and stylesheets<\/strong> \u2013 allow your main web servers to process the application while images and stylesheets are served from frontend webservers<\/div>\n<div style=\"padding-left: 30px;\">21. <strong>Use mod_passenger for rails<\/strong> \u2013 mod_passenger is able to share memory and resources amongst several  processes, allowing for faster spawning of new application instances.  &nbsp;It will also monitor these processes and remove them when they are  unnecessary.<\/div>\n<div style=\"padding-left: 30px;\">22. <strong>Turn off safe_mode for php<\/strong> \u2013 it will utilize about 50-70% of your script time checking against  these safe directives. &nbsp;Instead configure open_base_dir properly and  utilize plugins such as mod_itk.<\/div>\n<div style=\"padding-left: 30px;\">23. <strong>Don\u2019t use threaded mpm with mod_php<\/strong> \u2013 look at using mod_itk, mod_php tends to segfault with threaded mpm.<\/div>\n<div style=\"padding-left: 30px;\">24. <strong>Flush buffers early for pre-render <\/strong>&#8211;  it takes a relatively long time to create a web page on the backend,  flush your buffer prior to page completion to send a partial page to the  client, so it can start rendering. &nbsp;A good place to do this is right  after the HEAD section \u2013 so that the browser can start fetching other  objects.<\/div>\n<div style=\"padding-left: 30px;\">25. <strong>Use a Cache for frequently accessed data <\/strong>&#8211; memcached is a great for frequently used data and sessions. &nbsp;It will speed up your apache render time as databases are slow.<\/div>\n<p>And one more tip. &nbsp;Your Apache performance is only as good as your  benchmarks measuring it. &nbsp;If you can\u2019t get numbers or a graph to show  that your performance has increased or load decreased, then you don\u2019t  know if your tuning has done you any good. &nbsp;A great way to ensure that  you are performing better than before is to use these techniques to hook  up <a href=\"http:\/\/blog.monitis.com\/2011\/07\/03\/integrate-apache-monitoring-into-monitis-com\/\" target=\"_blank\" rel=\"noopener\">apache monitoring to Monitis<\/a> hosted monitoring.<br \/> <em>Do you know more tips? Disagree with some of the tips? Please share your comments bellow. <\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>We all know and love Apache. &nbsp;Its great, it allows us to run websites on the Internet with minimal configuration and administration. However, this same ease of flexibility and lack of tuning, is typically what&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-26","post","type-post","status-publish","format-standard","hentry","category-optimization"],"_links":{"self":[{"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/posts\/26","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/comments?post=26"}],"version-history":[{"count":0,"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/posts\/26\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/media?parent=26"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/categories?post=26"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/tags?post=26"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}