Apache performanceoptimization
Apacheserver performanceoptimization is 确保网站 fast 速response and high 效run 关键. 随着网站traffic 增 long , 合理 performanceoptimization可以显著提升server processingcapacity, reducingresponse时间, improvinguser体验. 本文将详细介绍Apache performanceoptimization策略, includingconfigurationparameter调优, cacheconfiguration, 连接management and best practices, helping您构建一个 high performance Apacheserver.
Apacheperformanceoptimizationoverview
Apacheserver performanceoptimization涉及 many 个方面, including:
- MPMconfiguration: 选择合适 many processingmodule(MPM)并optimization其parameter.
- cacheconfiguration: 启用 and configuration各种cachemechanism, reducing重复processing.
- 连接management: optimization连接processing and 保持mechanism.
- in 容压缩: 启用HTTP压缩, reducing传输data量.
- modulemanagement: 禁用不必要 module, reducingmemory占用.
- 硬件 and systemoptimization: 合理configurationserver硬件 and operationsystem.
performanceoptimizationprinciples
- in foroptimization之 before , 先建立performance基准, 以便assessmentoptimization效果.
- 一次只modify一个parameter, 然 after test其效果.
- 根据server practicalload and 硬件configurationfor调整.
- 定期monitorserverperformance, 及时发现 and 解决issues.
many processingmodule(MPM)configuration
Apacheproviding了 many 种 many processingmodule(MPM), 用于processing客户端连接. 不同 MPM适用于不同 场景, 选择合适 MPM并optimization其parameter is performanceoptimization Basics.
常用MPMmodule
- prefork: 每个连接using一个单独 process, stable reliable , 适用于processing few 量concurrent连接.
- worker: using many process and many thread, 每个processpackage含 many 个thread, 适用于processing big 量concurrent连接.
- event: worker improvementversion, usingevent驱动model, 适用于processing long 连接 and big 量concurrent连接.
查看当 before using MPM
# 查看当 before using MPM
apache2ctl -V | grep MPM
# or
httpd -V | grep MPM
configurationprefork MPM
# 编辑/etc/apache2/mods-available/mpm_prefork.conf
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxRequestWorkers 150
MaxConnectionsPerChild 0
configurationworker MPM
# 编辑/etc/apache2/mods-available/mpm_worker.conf
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestWorkers 150
MaxConnectionsPerChild 0
configurationevent MPM
# 编辑/etc/apache2/mods-available/mpm_event.conf
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestWorkers 150
MaxConnectionsPerChild 0
Notes
configurationMPMparameter时, 应根据server 硬件configuration (特别 is memory) and 预期 concurrentconnectionsfor调整:
MaxRequestWorkers: 最 big concurrentconnections, 应根据servermemoryfor调整.MaxConnectionsPerChild: 每个子processprocessing 最 big connections, 设置 for 非零值可以防止memory泄漏.- for 于prefork MPM, 每个process big 约需要2-10MBmemory.
- for 于worker and event MPM, 每个thread big 约需要256KB-1MBmemory.
cacheconfiguration
启用cache可以显著improvingApache performance, reducing重复processing and datalibraryquery, 加 fast response速度.
启用mod_cachemodule
# Ubuntu/Debian
sudo a2enmod cache cache_disk
# CentOS/RHEL
sudo yum install mod_cache mod_cache_disk
# 重启Apache
sudo systemctl restart apache2
configurationdiskcache
# in 虚拟主机configurationin添加以 under in 容
<IfModule mod_cache.c>
<IfModule mod_cache_disk.c>
CacheEnable disk /
CacheDirLevels 2
CacheDirLength 1
CacheRoot /var/cache/apache2
CacheDefaultExpire 3600
CacheMaxExpire 86400
CacheIgnoreHeaders Set-Cookie
</IfModule>
</IfModule>
启用filedescribes符cache
# 编辑apache2.conffile
FileETag MTime Size
# 启用sendfile (such as果operationsystemsupport)
EnableSendfile On
# 启用TCP_NODELAY
EnableMMAP On
in 容压缩
启用HTTP压缩可以reducing传输data量, improvingresponse速度, 特别 is for 于文本 in 容.
启用mod_deflatemodule
# Ubuntu/Debian
sudo a2enmod deflate
# CentOS/RHEL
sudo yum install mod_deflate
# 重启Apache
sudo systemctl restart apache2
configuration压缩
# in 虚拟主机configurationin添加以 under in 容
<IfModule mod_deflate.c>
# 压缩HTML, XML, CSS, JavaScript
AddOutputFilterByType DEFLATE text/html text/xml text/css text/plain
AddOutputFilterByType DEFLATE application/xml application/xhtml+xml application/rss+xml
AddOutputFilterByType DEFLATE application/javascript application/x-javascript
# 压缩字体file
AddOutputFilterByType DEFLATE application/font-woff application/font-woff2
AddOutputFilterByType DEFLATE application/vnd.ms-fontobject application/x-font-ttf
# 压缩JSONdata
AddOutputFilterByType DEFLATE application/json
# 设置压缩级别 (1-9, 9 for 最 high 压缩率)
DeflateCompressionLevel 6
# 避免压缩已经压缩 file
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|rar|zip|7z|pdf|mov|avi|wmv|mp3|wav)$ no-gzip dont-vary
</IfModule>
连接management
optimization连接management可以reducing连接建立 and 关闭 开销, improvingserver processingcapacity.
启用Keep-Alive
# in apache2.conffilein添加以 under in 容
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
configurationTCPparameter
# 编辑/etc/sysctl.conffile
# 增加TCP连接queue long 度
net.core.somaxconn = 4096
# reducingTIME_WAITstatus 时间
net.ipv4.tcp_fin_timeout = 15
# 启用TCP连接重用
net.ipv4.tcp_tw_reuse = 1
# application更改
sudo sysctl -p
modulemanagement
禁用不必要 Apachemodule可以reducingmemory占用 and 启动时间, improvingserverperformance.
查看已启用 module
# Ubuntu/Debian
sudo a2query -m
# CentOS/RHEL
sudo httpd -M
禁用不必要 module
# Ubuntu/Debian
sudo a2dismod status autoindex negotiation include userdir cgi
# 重启Apache
sudo systemctl restart apache2
# CentOS/RHEL
# 编辑/etc/httpd/conf/httpd.conffile, comment掉不需要 module
# LoadModule status_module modules/mod_status.so
# LoadModule autoindex_module modules/mod_autoindex.so
# LoadModule negotiation_module modules/mod_negotiation.so
# LoadModule include_module modules/mod_include.so
# LoadModule userdir_module modules/mod_userdir.so
# LoadModule cgi_module modules/mod_cgi.so
# 重启Apache
sudo systemctl restart httpd
硬件 and systemoptimization
memoryoptimization
- 增加servermemory: Apache is memory密集型application, 足够 memory可以显著improvingperformance.
- configuration交换空间: 合理configuration交换空间, 避免memory不足导致 performanceissues.
- usingmemory分配器: 考虑usingtcmallocetc. high 效 memory分配器.
diskI/Ooptimization
- usingSSD: SSD比HDD具 has 更 fast 读写速度, 显著improvingI/Operformance.
- 启用diskcache: 合理configurationdiskcache, reducing物理I/Ooperation.
- usingRAID: for 于 high traffic网站, 考虑usingRAID 0 or RAID 10improvingI/Operformance.
- optimizationfilesystem: 选择合适 filesystem (such asext4, XFS) 并optimization其parameter.
CPUoptimization
- 选择合适 CPU: Apache performance and CPUcore数 and 频率密切相关.
- 启用CPU亲 and 性: 将Apacheprocess绑定 to specific CPUcore, reducing on under 文切换.
- 关闭不必要 service: 关闭server on 不需要 service, 释放CPUresource.
monitor and 调优
定期monitorApacheserver performance, 及时发现 and 解决issues, is performanceoptimization important 组成部分.
usingmod_statusmonitor
# 启用mod_statusmodule
# Ubuntu/Debian
sudo a2enmod status
# 编辑status页面configuration
sudo nano /etc/apache2/mods-available/status.conf
# 添加以 under in 容
<Location /server-status>
Setprocessingr server-status
Require ip 127.0.0.1
Require ip 192.168.1.0/24
</Location>
# 重启Apache
sudo systemctl restart apache2
# 访问status页面
# http://your-server/server-status
usingabforperformancetest
# installationabtool
# Ubuntu/Debian
sudo apt install apache2-utils
# CentOS/RHEL
sudo yum install httpd-tools
# forperformancetest
# 1000个request, 10个concurrent连接
ab -n 1000 -c 10 http://your-server/
usingtop and vmstatmonitorsystemresource
# monitorCPU and memoryusingcircumstances
top
# monitor虚拟memory and I/Ocircumstances
vmstat 1
# monitordiskI/Ocircumstances
iostat -x 1
# monitornetworktraffic
tcpdump -i eth0 port 80
# monitorApacheerrorlog
tail -f /var/log/apache2/error.log
best practices
- using最 new version Apache: new version通常package含performanceimprovement and bug修复.
- 选择合适 MPM: 根据server硬件 and concurrentconnections选择合适 MPM.
- 启用cache: 合理configuration各种cachemechanism, reducing重复processing.
- 启用压缩: for 文本 in 容启用HTTP压缩, reducing传输data量.
- optimizationKeep-Alive设置: 合理configurationKeep-Aliveparameter, reducing连接开销.
- 禁用不必要 module: 只启用必要 module, reducingmemory占用.
- 合理configurationMPMparameter: 根据server硬件configuration调整MPMparameter.
- usingCDN: for 于静态resource, 考虑using in 容分发network(CDN).
- optimizationdatalibraryquery: such as果网站usingdatalibrary, optimizationdatalibraryquery可以显著improvingperformance.
- 定期monitor and 调优: 定期monitorserverperformance, 根据practicalcircumstancesfor调优.
Notes
in forperformanceoptimization时, 应注意以 under 几点:
- in modifyconfiguration之 before , 始终backup原始configurationfile.
- in produceenvironmentin, 先 in testenvironmentintestconfiguration变更.
- performanceoptimization is a 持续 过程, 需要根据practicalcircumstancescontinuously调整.
- 不同 网站 and application可能需要不同 optimization策略, 没 has 放之四海而皆准 solution.
- 过度optimization可能会导致 stable 性issues, 应 in performance and stable 性之间找 to 平衡.
实践case: Apacheperformanceoptimization
步骤1: 选择 and configurationMPM
# 查看当 before using MPM
apache2ctl -V | grep MPM
# 启用event MPM (推荐)
sudo a2dismod mpm_prefork
sudo a2enmod mpm_event
# 编辑event MPMconfiguration
sudo nano /etc/apache2/mods-available/mpm_event.conf
# 添加以 under in 容
<IfModule mpm_event_module>
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestWorkers 200
MaxConnectionsPerChild 10000
</IfModule>
# 重启Apache
sudo systemctl restart apache2
步骤2: 启用 and configurationcache
# 启用cachemodule
sudo a2enmod cache cache_disk
# creationcacheTable of Contents
sudo mkdir -p /var/cache/apache2
sudo chown www-data:www-data /var/cache/apache2
# 编辑虚拟主机configuration
sudo nano /etc/apache2/sites-available/example.com.conf
# 添加以 under in 容
<IfModule mod_cache.c>
<IfModule mod_cache_disk.c>
CacheEnable disk /
CacheDirLevels 2
CacheDirLength 1
CacheRoot /var/cache/apache2
CacheDefaultExpire 3600
CacheMaxExpire 86400
CacheIgnoreHeaders Set-Cookie
</IfModule>
</IfModule>
# 重启Apache
sudo systemctl restart apache2
步骤3: 启用 and configuration压缩
# 启用deflatemodule
sudo a2enmod deflate
# 编辑虚拟主机configuration
sudo nano /etc/apache2/sites-available/example.com.conf
# 添加以 under in 容
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/xml text/css text/plain
AddOutputFilterByType DEFLATE application/xml application/xhtml+xml application/rss+xml
AddOutputFilterByType DEFLATE application/javascript application/x-javascript
AddOutputFilterByType DEFLATE application/font-woff application/font-woff2
AddOutputFilterByType DEFLATE application/vnd.ms-fontobject application/x-font-ttf
AddOutputFilterByType DEFLATE application/json
DeflateCompressionLevel 6
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|rar|zip|7z|pdf|mov|avi|wmv|mp3|wav)$ no-gzip dont-vary
</IfModule>
# 重启Apache
sudo systemctl restart apache2
步骤4: optimization连接management
# 编辑apache2.conffile
sudo nano /etc/apache2/apache2.conf
# 添加 or modify以 under in 容
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
# 启用sendfile and MMAP
EnableSendfile On
EnableMMAP On
# 重启Apache
sudo systemctl restart apache2
步骤5: 禁用不必要 module
# 查看已启用 module
sudo a2query -m
# 禁用不必要 module
sudo a2dismod status autoindex negotiation include userdir cgi
# 重启Apache
sudo systemctl restart apache2
步骤6: testperformance
# usingabforperformancetest
ab -n 1000 -c 10 http://example.com/
# 查看Apachestatus
tail -f /var/log/apache2/access.log
# monitorsystemresource
top
# monitornetworktraffic
iftop
互动练习
练习1: MPMconfigurationoptimization
根据您 server硬件configuration, 选择合适 MPM并optimization其parameter. test不同configuration under performance表现, 找 to 最佳configuration.
练习2: cacheconfiguration
启用 and configurationApache diskcache, testcache for performance 影响. 调整cacheparameter, 找 to 最佳configuration.
练习3: 压缩configuration
启用 and configurationApache deflatemodule, test压缩 for performance 影响. 调整压缩级别, 找 to performance and 压缩率 平衡点.
练习4: 综合performanceoptimization
comprehensively applying learned performanceoptimizationtechniques, for Apacheserverfor全面optimization. usingabtooltestoptimization before after performancediff, assessmentoptimization效果.