优化Apache使其性能更佳的技巧

However, this same ease of flexibility and lack of tuning, is typically what leads Apache to becoming a memory hog.  Utilizing these easy to understand tips, you can gain a significant performance boost from Apache.

We all know and love Apache.  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 leads Apache to becoming a memory hog.  Utilizing these easy to understand tips, you can gain a significant performance boost from Apache.

Apache Specifics

 

1. Remove unused modules – save memory by not loading modules that you do not need, including but not limited to mod_php, mod_ruby, mod_perl, etc.

2. Use mod_disk_cache NOT mod_mem_cache – 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.

3. Configure mod_disk_cache with a flat hierarchy – ensure that you are using CacheDirLength=2 and CacheDirLevels=1 to ensure htcacheclean will not take forever when cleaning up your cache directory.

4. Setup appropriate Expires, Etag, and Cache-Control Headers – to utilize your cache, you must tell it when a file expires, otherwise your client will not experience the caching benefits.

5. Put Cache on separate disk – place your cache on a separate physical disk for fastest access without slowing down other processes.

6. Use Piped Logging instead of direct logging – directly logging to a file has issues when you want to rotate the log file.  It must restart apache to use the next log file.  This will cause significant slowness for your users during the restart.  Particularly if you are using Passenger or some other app loader.

7. Log to a different disk than disk serving pages – put your logs on physically different disks than the files you are serving.

8. Utilize mod_gzip/mod_deflate – 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.

9. Turn HostnameLookups Off – stop doing expensive DNS lookups.  You will rarely ever need them and when you do, you can look them up after the fact.

10. Avoid using hostname in configs – 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.

11. Use Persistent Connections – Set KeepAlive On and then set KeepAliveTimeout and KeepAliveRequests.  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.  This will prevent the client from having to reconnect between each request.

12. Do Not set KeepAliveTimeout too high – if you have more requests than apache children, this setting can starve your pool of available clients.

13. Disable .htaccess – i.e. AllowOverride None  This will prevent apache from having to check for a .htaccess file on each request.

14. Allow symlinks – i.e. Options +FollowSymLinks -SymLinksIfOwnerMatch.  Otherwise, apache will make a separate call on each filename to ensure it is not a symlink.

15. Set ExtendedStatus Off – Although very useful, the ExtendedStatus will produce several system calls for each request to gather statistics.  Better to utilize for a set time period in order to benchmark, then turn back off.

16. Avoid Wildcards in DirectoryIndex – use a specific DirectoryIndex, i.e. index.html or index.php, not index

OS Specifics

 

17. Increase Swappiness – particularly on single site hosts this will increase performance.  On linux systems increase /proc/sys/vm/swappiness to at least 60 if not greater.  This will try to load as many files as possible into the memory cache for faster access.

18. Increase Write Buffer Size – increase your write buffer size for tcp/ip buffers.  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.

19. Increase Max Open Files – if you are handling high loads increase the number of allowed open files.  On linux, increase /proc/sys/fs/file-max and run ulimit -H -n 4096.

Application Specifics

 

20. Setup Frontend proxy for images and stylesheets – allow your main web servers to process the application while images and stylesheets are served from frontend webservers

21. Use mod_passenger for rails – mod_passenger is able to share memory and resources amongst several processes, allowing for faster spawning of new application instances.  It will also monitor these processes and remove them when they are unnecessary.

22. Turn off safe_mode for php – it will utilize about 50-70% of your script time checking against these safe directives.  Instead configure open_base_dir properly and utilize plugins such as mod_itk.

23. Don’t use threaded mpm with mod_php – look at using mod_itk, mod_php tends to segfault with threaded mpm.

24. Flush buffers early for pre-render – 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.  A good place to do this is right after the HEAD section – so that the browser can start fetching other objects.

25. Use a Cache for frequently accessed data – memcached is a great for frequently used data and sessions.  It will speed up your apache render time as databases are slow.

And one more tip.  Your Apache performance is only as good as your benchmarks measuring it.  If you can’t get numbers or a graph to show that your performance has increased or load decreased, then you don’t know if your tuning has done you any good.  A great way to ensure that you are performing better than before is to use these techniques to hook up apache monitoring to Monitis hosted monitoring.

Do you know more tips? Disagree with some of the tips? Please share your comments bellow.

译文:

Apache 至今仍处于web服务器领域的霸主,无人撼动,没有开发者不知道。本篇文章介绍25个Apache性能优化技巧,如果你能理解并掌握,将让你的Apache性能有显著的提升!

Apache 部分:

1. 移除不用的模块。

2. 使用 mod_disk_cache NOT mod_mem_cache 。

3. 扁平架构配置mod_disk_cache。

4. 安装恰当的Expires, Etag, 和 Cache-Control Headers 。

5. 将缓存放在独立的磁盘

6. 使用管道日志替代直接记录

7. 将日志放在不同的磁盘

8. 使用 mod_gzip/mod_deflate 。

9. 将HostnameLookups关闭.

10. 避免在configs中使用主机名。

11. 使用持久连接。

12. 不要设置KeepAliveTimeout太高。

13. 禁用 .htaccess。

14. 允许symlinks。

15. 关闭ExtendedStatus。

16.
避免在DirectoryIndex中通配符。

OS 部分:

17. 提高Swappiness。

18. 提高写入缓冲器( Write Buffer)大小。

19. 提高最大打开文件。

应用部分:

20. 设置图像和样式表的前端代理。

21. 使用 mod_passenger for rails。

22. 关闭safe_mode for php。

23. 不要使用threaded mpm with mod_php。

24. 刷新缓冲区预渲染。

25. 频繁访问的数据设置缓存。

内容出处:,

声明:本网站所收集的部分公开资料来源于互联网,转载的目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。如果您发现网站上有侵犯您的知识产权的作品,请与我们取得联系,我们会及时修改或删除。文章链接:http://www.yixao.com/procedure/303.html

发表评论

登录后才能评论