CentOS Apache 配置指南
一 环境准备与安装
sudo yum update -y 与 sudo yum install httpd -ysudo systemctl start httpd 与 sudo systemctl enable httpdsudo firewall-cmd --permanent --add-service=http、sudo firewall-cmd --permanent --add-service=https、sudo firewall-cmd --reloadhttp://服务器IP或域名,出现 Apache 测试页即成功。二 基础配置与目录结构
echo "<h1>Hello from Apache on CentOS</h1>" | sudo tee /var/www/html/index.htmlsudo apachectl configtestsudo systemctl reload httpdsudo systemctl restart httpdsudo systemctl status httpd。三 虚拟主机配置
sudo mkdir -p /var/www/example.comsudo chown -R apache:apache /var/www/example.comsudo chmod -R 755 /var/www/example.com<VirtualHost *:80>
ServerAdmin admin@example.com
DocumentRoot /var/www/example.com
ServerName example.com
ServerAlias www.example.com
ErrorLog /var/log/httpd/example.com-error.log
CustomLog /var/log/httpd/example.com-access.log combined
</VirtualHost>
sudo systemctl reload httpd服务器IP example.com www.example.comcurl http://example.com 验证。四 启用 HTTPS 与自动续期
sudo yum install epel-release -y 与 sudo yum install certbot python2-certbot-apache -ysudo certbot --apache -d example.com -d www.example.comsudo crontab -e0 0 * * * /usr/bin/certbot renew --quiet。五 安全与性能优化
sudo semanage fcontext -a -t httpd_sys_content_t "/var/www/example.com(/.*)?"sudo restorecon -Rv /var/www/example.comServerTokens Prod 与 ServerSignature OffKeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/css application/javascript
</IfModule>
sudo yum install mod_cache mod_cache_disk -y,并按需配置磁盘缓存。httpd -V