在CentOS上配置Apache服务器时,可以采用以下一些技巧来提高其性能和安全性:
安装与基本配置:
sudo yum update -ysudo yum install httpd -y
sudo systemctl start httpdsudo systemctl enable httpd
sudo systemctl status httpd
配置防火墙:
sudo firewall-cmd --permanent --add-service=httpsudo firewall-cmd --permanent --add-service=httpsudo firewall-cmd --reload
虚拟主机配置:
基于名字的虚拟主机:
sudo mkdir -p /var/www/nn /var/www/hh
echo "nn.yunrange.com, NAME based virtual host test file." | sudo tee /var/www/nn/index.html
echo "hh.yunrange.com, NAME based virtual host test file." | sudo tee /var/www/hh/index.html
编辑 /etc/httpd/conf/httpd.conf
文件,添加以下内容:
NameVirtualHost *:80
VirtualHost *:80 DocumentRoot /var/www/nn
ServerName nn.yunrange.com
VirtualHost *:80 DocumentRoot /var/www/hh
ServerName hh.yunrange.com
重启Apache服务:
sudo systemctl restart httpd
基于IP的虚拟主机:
sudo mkdir -p /var/www/ip1 /var/www/ip2
echo "IP based virtual host file for IP1." | sudo tee /var/www/ip1/index.html
echo "IP based virtual host file for IP2." | sudo tee /var/www/ip2/index.html
编辑 /etc/httpd/conf/httpd.conf
文件,添加以下内容:
VirtualHost 192.168.5.150:80 DocumentRoot /var/www/ip1
ServerName 192.168.5.150
VirtualHost 192.168.5.151:80 DocumentRoot /var/www/ip2
ServerName 192.168.5.151
重启Apache服务:
sudo systemctl restart httpd
SSL配置:
sudo yum install epel-release -y
sudo yum install certbot python2-certbot-apache -y
sudo certbot --apache -d yourdomain.com -d www.yourdomain.com
性能优化:
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
StartServers 5
MinSpareServers 5
MaxSpareServers 10
MaxRequestWorkers 150
MaxConnectionsPerChild 0
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript
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"
安全性:
ServerTokens Prod
ServerSignature Off
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
通过这些配置技巧,可以显著提高Apache服务器的性能、安全性和稳定性。