在CentOS上配置和優化Apache服務器是一個涉及多個方面的過程,旨在提高服務器的性能、安全性和穩定性。以下是一些關鍵的優化技巧和步驟:
sudo yum update -y
sudo yum install httpd -y
sudo systemctl start httpd
sudo systemctl enable httpd
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
httpd.conf
中添加以下行來啟用KeepAlive:KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
prefork
模塊:<IfModule mpm_prefork_module>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxRequestWorkers 150
MaxConnectionsPerChild 0
</IfModule>
mod_expires
模塊啟用靜態文件緩存:<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/css "access plus 30 days"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType application/javascript "access plus 30 days"
</IfModule>
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript
</IfModule>
/etc/sysctl.conf
文件,調整TCP連接數、文件句柄數等參數:net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.ip_local_port_range = 1024 65000
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_max_tw_buckets = 5000
sudo yum install epel-release -y
sudo yum install certbot python2-certbot-apache -y
sudo certbot --apache -d yourdomain.com -d www.yourdomain.com
httpd.conf
中修改ServerTokens
和ServerSignature
指令以隱藏版本信息。通過上述優化技巧,可以顯著提高CentOS上Apache服務器的性能和安全性。在進行任何配置更改后,請確保重新啟動Apache服務以使更改生效。