说来不好意思,服务器的性能一直都是个大问题,访问量不大,却经常死机。
调优了很多次,也没什么效果。
晚上加晚班,现在无事,又搜索了一番,修改了下,这下貌似有效果了,分享如下。
问题描述
RHL Linux 5.0 + Apache Httpd 2.2 + Mysql5.0 + PHP 5.0 环境
问题描述:服务器运行若干时间后,会无法响应请求,分析日志文件和进程,有如下情况
- 大量的404错误:盗链和后续处理有关
- 一些比较奇怪的Apache程序异常
[Sat Apr 25 02:06:40 2009] [notice] child pid 5534 exit signal Floating point exception (8)
[Sat Apr 25 02:06:41 2009] [notice] child pid 5499 exit signal Floating point exception (8)
[Sat Apr 25 02:06:41 2009] [notice] child pid 5527 exit signal Floating point exception (8)
………
强制重启进程时还有*** glibc detected *** /usr/sbin/httpd: free(): invalid pointer: 0×00359170 ***
*** glibc detected *** /usr/sbin/httpd: free(): invalid pointer: 0×00359170 *** - 系统负载平衡很高,甚至达到60
- CPU使用很高,物理内存使用达到90%,交换内存也将近满了
- 有大量的Apache Httpd进程,且多为Sleeping状态
原因分析
常见的引起服务器不稳定的原因:
- 程序问题引起的内存泄漏
- 不恰当的配置导致资源死锁
关于Apache Httpd的调优,有很多说法,不过个人推荐两篇文章,应该是通俗易懂的,呃,E文
Open Source: Configuring Apache – Don’t Succumb To The “Slashdot Effect” http://blogcritics.org/archives/2006/01/27/175740.php
Tuning Apache, part 1 http://virtualthreads.blogspot.com/2006_01_01_archive.html
还有有中文的
Apache Prefork和Worker模式性能比较
http://www.5dlinux.com/article/9/2009/linux_29722.html
官方文档的中文翻译 性能方面的提示
http://lamp.linux.gov.cn/Apache/ApacheMenu/misc/perf-tuning.html
说说对上面两个问题的方案。
内存泄漏。程序哪里错了,不好说,得跟踪定位,每个系统都有内存跟踪的工具,除此之外还要分析日志,可以通过Apache的日志配置来跟踪脚本运行的时间,找出可能的元凶。
除此之外,还有个很土的办法:定时重启。实在没招了可以参考。
服务器的配置么,那更有说法了。Apache httpd默认是Prefork模式(参考上面),这里就是论事的介绍几个参数:事实上我只是简单摘译一下上面的某篇文章,后面的设置是作者的推荐值,中文里面是我的推荐
- MaxKeepAliveRequests 0 The KeepAlive directive in httpd.conf allows persistent connections to the web server, so that new connection does not have to be initiated for each request. Setting the MaxKeepAliveRequests directive to 0 enables unlimited number of requests per connection, which makes sense if you think about it. Why allow persistent connections but then terminate them after a short period of time?
- 最大保持连接请求数:0 – 无限制。保持连接意味着不必为同一客户端的多次请求重新释放和分配资源,也意味着这些资源不会马上释放。这在现在这个Ajax漫天飞,外加无数图片和JS的时代很重要,但是如果控制不好资源的保持与释放,就意味着系统资源的耗尽。不过这里只是能复用的请求数,无限就好了。
- KeepAliveTimeout 15
Because persistent connections are allowed, it is important that they are not kept open indefinitely. This directive will close the connection after 15 seconds of inactivity. - 保持连接超时 3 秒
如果连接请求无活动,就有必要关掉它,默认15秒太长了点,3秒钟就够了。 - MinSpareServers 15
This is the minimum number of spare servers you want running at any given time. This way, if multiple simultaneous requests are received there will already be child processes running to handle them. Setting this number too high is a waste of system resources and setting it too low will cause the system to slow down. - MaxSpareServers 65
Same as above, but the maximum child processes running at any given time. - StartServers 15
This is the number of servers Apache will start initially. As more servers handle requests a minimum of 15 spare servers will run up to the maximum of 64. - 上面这三个参数放在一起说:其实就是Apache用于响应用户的子进程,一般一个子进程消耗20M左右内存,上面三个数字可以控制子进程的数量。于是根据可用的系统资源,就可以计算出个合理的取值了。
- MaxClients 500
This is the maximum number of simultaneous clients that can connect to the server at any given time. Setting this number too low will result in users being locked out of the server under normal traffic situations and setting it too high will result in your server being so overloaded that all the requests timeout anyway. I think 500 is about right for most people’s needs. - 最大客户端,其实就是并发处理量,这也要根据情况来配置,太大会导致服务器崩溃,太小会导致请求排队,响应缓慢。
- MaxRequestsPerChild 100000
Sets the maximum number of requests each child process will handle. This is mostly to prevent memory leaks and other mishaps but is important nonetheless. Setting this too low will cause a large portion of child processes to end for no real reason, thus slowing down the site. This could be set to 0 (unlimited) but that would negate any protection from valid issues like memory leaks. - 每个子进程可以处理的最多请求。做限制的好处就是,如果某个程序内存泄漏,那么一段时间后就可以把它释放掉。限制小会导致频繁创建进程,拖慢系统。
- HostnameLookups off
This prevents DNS lookups of all the visitors to the site, I am pretty sure it’s off by default. If it’s on in your httpd.conf I would recommend turning it off.这没啥好说的
默认的配置
<IfModule prefork.c>
ServerLimit 10000
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxClients 10000
MaxRequestsPerChild 0
</IfModule>
这个默认配置的缺点就是不能防止内存泄漏,结合KeepAlive的设置,占用过多资源。
KeepAlive on
KeepAliveTimeout 2
MaxKeepAliveRequests 0
MaxClients 150
MinSpareServers 5
MaxSpareServers 12
StartServers 5
MaxRequestsPerChild 100000
较短的保持连接释放,有利于应对无效的盗链请求。
客户端限制用于保护服务器正常运行。
实验继续进行中,本文将不定期更新。