{"id":10,"date":"2015-09-04T12:14:14","date_gmt":"2015-09-04T12:14:14","guid":{"rendered":"http:\/\/www.asianux.org.vn\/?p=10"},"modified":"2015-09-04T12:14:14","modified_gmt":"2015-09-04T12:14:14","slug":"25-apache-performance-tuning-tips","status":"publish","type":"post","link":"https:\/\/www.asianux.org.vn\/index.php\/2015\/09\/04\/25-apache-performance-tuning-tips\/","title":{"rendered":"25 Apache Performance Tuning Tips"},"content":{"rendered":"<p>We all know and love Apache. \u00a0Its great, it allows us to run websites on the Internet with minimal configuration and administration.<br \/>\nHowever, this same ease of flexibility and lack of tuning, is typically what leads Apache to becoming a memory hog. \u00a0Utilizing these easy to understand tips, you can gain a significant performance boost from Apache.<span id=\"more-2684\"><\/span><\/p>\n<h3>Apache Specifics<\/h3>\n<p>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.<\/p>\n<div>2. <strong>Use mod_disk_cache NOT mod_mem_cache <\/strong>\u2013 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><\/div>\n<div>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>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>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>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. \u00a0It must restart apache to use the next log file. \u00a0This will cause significant slowness for your users during the restart. \u00a0Particularly if you are using Passenger or some other app loader.<\/div>\n<div>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>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>9. <strong>Turn HostnameLookups Off<\/strong> \u2013 stop doing expensive DNS lookups. \u00a0You will rarely ever need them and when you do, you can look them up after the fact.<\/div>\n<div>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>11. <strong>Use Persistent Connections <\/strong>\u2013 Set KeepAlive On and then set KeepAliveTimeout and KeepAliveRequests. \u00a0KeepAliveTimeout 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. \u00a0This will prevent the client from having to reconnect between each request.<\/div>\n<div>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>13. <strong>Disable .htaccess <\/strong>\u2013 i.e. AllowOverride None \u00a0This will prevent apache from having to check for a .htaccess file on each request.<\/div>\n<div>14. <strong>Allow symlinks<\/strong> \u2013 i.e. Options +FollowSymLinks -SymLinksIfOwnerMatch. \u00a0Otherwise, apache will make a separate call on each filename to ensure it is not a symlink.<\/div>\n<div>15. <strong>Set ExtendedStatus Off <\/strong>\u2013 Although very useful, the ExtendedStatus will produce several system calls for each request to gather statistics. \u00a0Better to utilize for a set time period in order to benchmark, then turn back off.<\/div>\n<div>16. <strong>Avoid Wildcards in DirectoryIndex<\/strong> \u2013 use a specific DirectoryIndex, i.e. <span class=\"skimlinks-unlinked\">index.html<\/span> or<span class=\"skimlinks-unlinked\">index.php<\/span>, not index<\/div>\n<h3>OS Specifics<\/h3>\n<p>17. <strong>Increase Swappiness<\/strong> \u2013 particularly on single site hosts this will increase performance. \u00a0On linux systems increase \/proc\/sys\/vm\/swappiness to at least 60 if not greater. \u00a0This will try to load as many files as possible into the memory cache for faster access.<\/p>\n<div>18.<strong> Increase Write Buffer Size<\/strong> \u2013 increase your write buffer size for tcp\/ip buffers. \u00a0On 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>19. <strong>Increase Max Open Files<\/strong> \u2013 if you are handling high loads increase the number of allowed open files. \u00a0On linux, increase \/proc\/sys\/fs\/file-max and run ulimit -H -n 4096.<\/div>\n<h3>Application Specifics<\/h3>\n<p>&nbsp;<\/p>\n<div>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>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. \u00a0It will also monitor these processes and remove them when they are unnecessary.<\/div>\n<div>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. \u00a0Instead configure open_base_dir properly and utilize plugins such as mod_itk.<\/div>\n<div>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>24. <strong>Flush buffers early for pre-render <\/strong>\u2013 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. \u00a0A good place to do this is right after the HEAD section \u2013 so that the browser can start fetching other objects.<\/div>\n<div>25. <strong>Use a Cache for frequently accessed data <\/strong>\u2013 memcached is a great for frequently used data and sessions. \u00a0It will speed up your apache render time as databases are slow.<\/div>\n<p>And one more tip. \u00a0Your Apache performance is only as good as your benchmarks measuring it. \u00a0If 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. \u00a0A 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 \/>\n<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. \u00a0Its 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":[12],"tags":[],"class_list":["post-10","post","type-post","status-publish","format-standard","hentry","category-he-thong"],"_links":{"self":[{"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/posts\/10","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=10"}],"version-history":[{"count":0,"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/posts\/10\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/media?parent=10"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/categories?post=10"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/tags?post=10"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}