Apache (httpd)
Apache HTTP Server(httpd)是最广泛使用的 Web 服务器之一。本文将从安装开始,逐步介绍配置结构、虚拟主机、模块管理、SSL 证书以及 SELinux 相关设置。
各发行版中的版本
Section titled “各发行版中的版本”安装 httpd
Section titled “安装 httpd”使用 DNF 安装 Apache 及常用工具:
sudo dnf install httpd httpd-tools -y安装完成后启动服务并设置为开机自启:
sudo systemctl start httpdsudo systemctl enable httpd验证服务运行状态:
sudo systemctl status httpd开放防火墙的 HTTP 和 HTTPS 端口:
sudo firewall-cmd --permanent --add-service=httpsudo firewall-cmd --permanent --add-service=httpssudo firewall-cmd --reload打开浏览器访问服务器 IP 地址,如果看到默认测试页面,说明安装成功。
配置文件结构
Section titled “配置文件结构”Apache 的配置文件分布在多个目录中,了解其结构有助于日后维护:
| 路径 | 说明 |
|---|---|
/etc/httpd/conf/httpd.conf | 主配置文件 |
/etc/httpd/conf.d/ | 额外配置片段目录(.conf 文件自动加载) |
/etc/httpd/conf.modules.d/ | 模块加载配置目录 |
/var/www/html/ | 默认网站根目录 |
/var/log/httpd/ | 日志目录(access_log、error_log) |
查看主配置文件中的关键参数:
grep -v '^\s*#' /etc/httpd/conf/httpd.conf | grep -v '^$'常见的全局配置项包括:
ServerRoot "/etc/httpd"Listen 80ServerAdmin root@localhostServerName your-domain.com:80DocumentRoot "/var/www/html"修改配置后务必检查语法再重载:
sudo apachectl configtestsudo systemctl reload httpd虚拟主机配置
Section titled “虚拟主机配置”虚拟主机允许在一台服务器上托管多个网站。推荐在 /etc/httpd/conf.d/ 下为每个站点创建独立配置文件。
创建站点目录与页面
Section titled “创建站点目录与页面”sudo mkdir -p /var/www/example.com/htmlsudo mkdir -p /var/www/example.com/logecho '<h1>Welcome to example.com</h1>' | sudo tee /var/www/example.com/html/index.html设置目录所有权:
sudo chown -R apache:apache /var/www/example.com编写虚拟主机配置
Section titled “编写虚拟主机配置”sudo tee /etc/httpd/conf.d/example.com.conf << 'EOF'<VirtualHost *:80> ServerName example.com ServerAlias www.example.com DocumentRoot /var/www/example.com/html
ErrorLog /var/www/example.com/log/error.log CustomLog /var/www/example.com/log/access.log combined
<Directory /var/www/example.com/html> AllowOverride All Require all granted </Directory></VirtualHost>EOFsudo apachectl configtestsudo systemctl reload httpd如需添加更多站点,重复上述步骤并创建对应的 .conf 文件即可。
启用与管理模块
Section titled “启用与管理模块”Apache 通过模块扩展功能。EL 系统中模块配置位于 /etc/httpd/conf.modules.d/ 目录。
查看已加载的模块
Section titled “查看已加载的模块”httpd -M安装额外模块
Section titled “安装额外模块”以 mod_ssl 和 mod_rewrite 为例:
sudo dnf install mod_ssl -ymod_rewrite 已包含在 httpd 默认安装中,只需确认已加载:
httpd -M | grep rewrite启用 .htaccess 重写
Section titled “启用 .htaccess 重写”在虚拟主机或目录配置中设置 AllowOverride All 即可启用 .htaccess 文件中的重写规则。创建一个示例 .htaccess:
sudo tee /var/www/example.com/html/.htaccess << 'EOF'RewriteEngine OnRewriteCond %{HTTPS} offRewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]EOFSSL/TLS 配置
Section titled “SSL/TLS 配置”使用自签名证书(测试环境)
Section titled “使用自签名证书(测试环境)”安装 mod_ssl 后会自动生成自签名证书,可直接通过 HTTPS 访问。证书位于:
- 证书:
/etc/pki/tls/certs/localhost.crt - 私钥:
/etc/pki/tls/private/localhost.key
使用 Let’s Encrypt 免费证书(生产环境)
Section titled “使用 Let’s Encrypt 免费证书(生产环境)”安装 Certbot:
sudo dnf install epel-release -ysudo dnf install certbot python3-certbot-apache -y申请证书:
sudo certbot --apache -d example.com -d www.example.comCertbot 会自动修改 Apache 配置并配置 HTTPS 虚拟主机。
设置自动续期
Section titled “设置自动续期”sudo certbot renew --dry-runCertbot 安装后会自动创建 systemd timer 来处理证书续期。确认 timer 已启用:
sudo systemctl status certbot-renew.timer手动配置 SSL 虚拟主机
Section titled “手动配置 SSL 虚拟主机”如果需要手动配置,参考以下模板:
sudo tee /etc/httpd/conf.d/example.com-ssl.conf << 'EOF'<VirtualHost *:443> ServerName example.com DocumentRoot /var/www/example.com/html
SSLEngine on SSLCertificateFile /etc/pki/tls/certs/example.com.crt SSLCertificateKeyFile /etc/pki/tls/private/example.com.key SSLCertificateChainFile /etc/pki/tls/certs/chain.crt
<Directory /var/www/example.com/html> AllowOverride All Require all granted </Directory></VirtualHost>EOFSELinux 与 httpd
Section titled “SELinux 与 httpd”在启用 SELinux 的系统上,Apache 受到额外的安全策略限制。以下是常见场景的处理方法。
查看当前 SELinux 状态
Section titled “查看当前 SELinux 状态”getenforcesestatus修改网站目录的 SELinux 上下文
Section titled “修改网站目录的 SELinux 上下文”如果将网站文件放在非默认目录(非 /var/www/),需要设置正确的 SELinux 上下文:
sudo semanage fcontext -a -t httpd_sys_content_t "/data/www(/.*)?"sudo restorecon -Rv /data/www允许 httpd 连接网络
Section titled “允许 httpd 连接网络”当 Apache 需要反向代理或连接后端数据库时:
sudo setsebool -P httpd_can_network_connect on允许 httpd 连接数据库
Section titled “允许 httpd 连接数据库”sudo setsebool -P httpd_can_network_connect_db on允许 httpd 发送邮件
Section titled “允许 httpd 发送邮件”sudo setsebool -P httpd_can_sendmail on允许 httpd 读取用户家目录
Section titled “允许 httpd 读取用户家目录”如果启用了 UserDir 模块:
sudo setsebool -P httpd_enable_homedirs on使用非标准端口
Section titled “使用非标准端口”如果 Apache 监听非标准端口(如 8080),需要将其添加到 SELinux 策略:
sudo semanage port -a -t http_port_t -p tcp 8080排查 SELinux 拒绝
Section titled “排查 SELinux 拒绝”如果遇到 SELinux 导致的问题,可以查看审计日志:
sudo ausearch -m avc -ts recentsudo sealert -a /var/log/audit/audit.log安装 setroubleshoot 工具可以获得更详细的建议:
sudo dnf install setroubleshoot-server -y常用运维命令速查
Section titled “常用运维命令速查”# 启动 / 停止 / 重启 / 重载sudo systemctl start httpdsudo systemctl stop httpdsudo systemctl restart httpdsudo systemctl reload httpd
# 查看实时访问日志sudo tail -f /var/log/httpd/access_log
# 查看实时错误日志sudo tail -f /var/log/httpd/error_log
# 查看 httpd 版本与编译参数httpd -V
# 列出所有虚拟主机配置httpd -S- Nginx 反向代理 — 替代 Web 服务器
- SELinux 入门 — 理解 SELinux 对 Web 服务的影响