Apache performance tuning for PH
2020-12-26 本文已影响0人
芒鞋儿
apache 2.0 之后加入了MPM模块,multi-processing modules 多通道处理模块)以适应多平台不同环境。
Apache general optimization:
- Timeout: 时间尽可能设短一些,以尽量较快释放系统资源,处理更多请求
- KeepAlive: on/off 通常为on, 可减轻CPU和网络的overhead, 尤其是有DB连接的情况下
- MaxKeepAliveRequest: 会尽量设定较大数目提高请求处理数量,尤其是一些图形网站
- KeepAliveTimeout: keepAlive受到这个参数的制约,如果超过这里设置的timeout则会去处理下一个请求,同样是遵循尽量较快释放系统资源处理更多的请求为基本原则。
MPM Event/Worker Optimization
- ServerLimit 通常=CPU的Core数
- ThreadsPerChild 一个子进程拥有多少个线程,default=25,最高不超过64,(nginx可以1024)线程共享一个子进程的shared mem, 但不同子进程中的线程不可以共享,
Fewer threads mean fewer potential “stuck” threads during the recycle procedure, keeping the higher capacity of requests available overall children.
这个数字是需要调整的,thread 数高虽然可以在同一个进程下尽可能处理更多请求,但thread数调低也减少了thread 资源被一直占用的风险,在worker方式中thread资源会一直被占用知道请求处理结束。 - ThreadLimit: 64
- MaxRequestWorkers/MaxClients: ServerLimit x ThreadsPerChild = MaxRequestsWorks
It is typical of Worker based MPM systems to run an isolated third-party PHP handler like Mod_fcgid, PHP-FPM, and mod_lsapi.
一个子进程是一个isolated 环境,对于每个isolated 环境,相关的PHP 配置有Mod_fcgid, php-fpm, mod_lsapi - MinSpareThreads: 50% of MaxRequestWorkers idle的子线程
- MaxSpareThreads: peak-off过了尖峰时间段中系统会shutdown 超过这个参数的线程,避免消耗过多内存,这个参数对于较小的服务器比较有用,但对于硬件配置高的几乎没用
- StartServers: 这个是服务启动的时候启动的进程数目
- MaxConnectionsPerChild/MaxRequestsPerChild: 此参数用于防止memory leak, 强制释放内存,如果采用mod_fcgid, php-fpm, mod_lsapi,这个参数设置为0
MPM Prefork Optimization
-
MaxRequestWorkers/MaxClients default=256
-
MinSpareServers: 25% of MaxRequestWorkers
-
MaxSpareServers:
If the MaxSpareServers value is less than MinSpareServers, Apache will automatically adjust MaxSpareServers to equal MinSpareServers plus one. -
StartServers:
-
Serverlimit:
-
MaxConnectionsPerChild/MaxRequestsPerChild
MPM主要有三种模块:
-
prefork mpm:
预先fork 子进程,相关的参数有:- StartServers
- MinSpareServers
- MaxSpareServers
- MaxRequestWorkers
- MaxConnectionsPerChild
-
worker mpm:
采用socket的方式,使用线程响应请求,每个请求占用一个线程,直至线程timeout或者释放,对应的参数有:- StartServers
- MinSpareThreads
- MaxSpareThreads
- ThreadsPerChild
- MaxRequestWorkers
- MaxConnectionsPerChild
-
event mpm:
采用event 方式监听响应请求,也被称为hybrid worker mpm, 不会等到thread结束而会尽量多启用线程响应请求,但无法对应https请求,相关的参数有:- StartServers
- MinSpareThreads
- MaxSpareThreads
- ThreadsPerChild
- MaxRequestWorkers
- MaxConnectionsPerChild
参考:
- https://www.liquidweb.com/kb/apache-performance-tuning-mpm-directives/
- https://blog.csdn.net/xiaoyi23000/article/details/79868544
要继续的课题:
1.PHP程序中的multi-thread 或者multi-process 实现方法以及和performance 相关内容
2.以下和performance相关的内容的考察:
- cakephp performance 相关
- PHP编程中的performance 注意点