在Ubuntu上优化Apache2的性能可以通过多种方法实现,以下是一些关键的步骤和建议:
sudo apt update && sudo apt upgrade
sudo apt install apache2
mod_deflate用于压缩,mod_expires用于缓存。sudo a2enmod deflate
sudo a2enmod expires
prefork、worker和event。对于高并发环境,worker或event是更好的选择。<IfModule mpm_worker_module>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
ServerLimit 256
MaxRequestWorkers 256
MaxConnectionsPerChild 1500
</IfModule>
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
mod_cache和mod_disk_cache来缓存静态资源,提高响应速度。LoadModule cache_module modules/mod_cache.so
LoadModule cache_disk_module modules/mod_disk_cache.so
CacheEnable disk /
CacheRoot "/var/cache/apache2"
CacheDirLevels 2
CacheDirLength 1
MaxClients或MaxRequestWorkers来控制并发连接数。MaxRequestWorkers 256
LoadModule ssl_module modules/mod_ssl.so
SSLEngine on
SSLCertificateFile /etc/ssl/certs/your_domain.crt
SSLCertificateKeyFile /etc/ssl/private/your_domain.key
mod_status模块以减少安全性风险。<IfModule mod_status.c>
SetHandler server-status
Order deny,allow
Deny from all
Allow from localhost
</IfModule>
/etc/sysctl.conf文件,调整内核参数以优化磁盘I/O和内存管理。vm.swappiness = 10
net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 65535
mod_proxy等模块实现负载均衡,分散请求压力。ProxyPass / balancer://mycluster
BalancerMember http://backend1.example.com
BalancerMember http://backend2.example.com
LoadModule status_module modules/mod_status.so
ExtendedStatus On
Location "/server-status"
SetHandler server-status
Order deny,allow
Deny from all
Allow from localhost
/dev/sda1 /data xfs defaults,noatime,nodiratime 0 0
sudo logrotate -f /etc/logrotate.conf
在进行任何配置更改后,都应该进行测试以确保服务器仍然按预期运行,并且在生产环境中应用更改之前,建议在测试环境中进行充分的测试。