第 13 章 Apache HTTP 服务器


Apache HTTP 服务器提供具有当前 HTTP 标准的开源 HTTP 服务器。[14]
在红帽企业 Linux 中,httpd 软件包 提供了 Apache HTTP 服务器。输入以下命令查看是否安装了 httpd 软件包:
~]$ rpm -q httpd
package httpd is not installed
如果没有安装它,且您想要使用 Apache HTTP 服务器,使用 yum 工具作为 root 用户安装它:
~]# yum install httpd

13.1. Apache HTTP 服务器和 SELinux

启用 SELinux 后,Apache HTTP Server (httpd)默认运行限制。受限制的进程在其自己的域中运行,并且与其他受限制的进程隔开。如果一个受攻击者限制的进程受到 SELinux 策略配置的影响,攻击者对资源的访问权限和可能受到的破坏会受到限制。以下示例演示了在其自己的域中运行的 httpd 进程。本例假设安装了 httpdsetroubleshootsetroubleshoot-serverpolicycoreutils-python 软件包:
  1. 运行 getenforce 命令,确认 SELinux 是否在 enforcing 模式下运行:
    ~]$ getenforce
    Enforcing
    
    当 SELinux 处于 enforcing 模式时,命令会返回 Enforcing
  2. 以 root 用户身份输入以下命令以启动 httpd
    ~]# systemctl start httpd.service
    确认 服务正在运行。输出中应包括以下信息(只有时间戳有所不同):
    ~]# systemctl status httpd.service       
    httpd.service - The Apache HTTP Server
    	  Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled)
    	  Active: active (running) since Mon 2013-08-05 14:00:55 CEST; 8s ago
    
  3. 要查看 httpd 进程,请执行以下命令:
    ~]$ ps -eZ | grep httpd
    system_u:system_r:httpd_t:s0    19780 ?        00:00:00 httpd
    system_u:system_r:httpd_t:s0    19781 ?        00:00:00 httpd
    system_u:system_r:httpd_t:s0    19782 ?        00:00:00 httpd
    system_u:system_r:httpd_t:s0    19783 ?        00:00:00 httpd
    system_u:system_r:httpd_t:s0    19784 ?        00:00:00 httpd
    system_u:system_r:httpd_t:s0    19785 ?        00:00:00 httpd
    
    httpd 进程关联的 SELinux 上下文为 system_u:system_r:httpd_t:s0。上下文的最后一部分 httpd_t 是类型。类型定义进程的域以及文件的类型。在本例中,httpd 进程在 httpd_t 域中运行。
SELinux 策略定义了在受限制域(如 httpd_t)中运行的进程如何与文件、其他进程和系统交互。文件必须正确标记,以允许 httpd 访问这些文件。例如,httpd 可以读取标有 httpd_sys_content_t 类型的文件,但无法写入它们,即使 Linux (DAC)权限允许写访问。必须启用布尔值以允许某些行为,如允许脚本网络访问,允许 httpd 访问 NFS 和 CIFS 卷,以及允许 httpd 执行通用网关接口(CGI)脚本。
当配置 /etc/httpd/conf/httpd.conf 文件时,httpd 侦听端口 80、443、488、8008、8009 或 8443,必须使用 semanage port 命令将新端口号添加到 SELinux 策略配置中。以下示例演示了将 httpd 配置为侦听 httpd 策略配置中没有定义的端口,因此 httpd 无法启动。这个示例还演示了如何将 SELinux 系统配置为允许 httpd 成功侦听策略中没有定义的非标准端口。本例假定已安装了 httpd 软件包。以 root 用户身份运行示例中的每个命令:
  1. 输入以下命令确认 httpd 没有运行:
    ~]# systemctl status httpd.service
    httpd.service - The Apache HTTP Server
    	  Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled)
              Active: inactive (dead)
    
    如果输出不同,请停止该进程:
    ~]# systemctl stop httpd.service
  2. 使用 semanage 工具查看 SELinux 允许 httpd 侦听的端口:
    ~]# semanage port -l | grep -w http_port_t
    http_port_t                    tcp      80, 443, 488, 8008, 8009, 8443
    
  3. 以 root 用户身份编辑 /etc/httpd/conf/httpd.conf 文件。配置 Listen 选项,以便它列出在 httpd 的 SELinux 策略配置中未配置的端口。在本例中,httpd 配置为侦听端口 12345 :
    # Change this to Listen on specific IP addresses as shown below to 
    # prevent Apache from glomming onto all bound IP addresses (0.0.0.0)
    #
    #Listen 12.34.56.78:80
    Listen 127.0.0.1:12345
    
  4. 输入以下命令启动 httpd
    ~]# systemctl start httpd.service
    Job for httpd.service failed. See 'systemctl status httpd.service' and 'journalctl -xn' for details.
    
    记录类似如下的 SELinux 拒绝信息:
    setroubleshoot: SELinux is preventing the httpd (httpd_t) from binding to port 12345. For complete SELinux messages. run sealert -l f18bca99-db64-4c16-9719-1db89f0d8c77
    
  5. 对于 SELinux,允许 httpd 侦听端口 12345,如本例中使用,需要以下命令:
    ~]# semanage port -a -t http_port_t -p tcp 12345
  6. 再次启动 httpd,并侦听新端口:
    ~]# systemctl start httpd.service
  7. 现在,SELinux 已被配置为允许 httpd 侦听一个非标准端口(本例中为 TCP 12345),httpd 在此端口上成功启动。
  8. 要证明 httpd 正在侦听并在 TCP 端口 12345 上进行通信,请打开到指定端口的 telnet 连接并发出 HTTP GET 命令,如下所示:
    ~]# telnet localhost 12345
    Trying 127.0.0.1...
    Connected to localhost.
    Escape character is '^]'.
    GET / HTTP/1.0
    
    HTTP/1.1 200 OK
    Date: Wed, 02 Dec 2009 14:36:34 GMT
    Server: Apache/2.2.13 (Red Hat)
    Accept-Ranges: bytes
    Content-Length: 3985
    Content-Type: text/html; charset=UTF-8
    [...continues...]
    


[14] 如需更多信息,请参阅《 系统管理员指南》中的名为 Apache HTTP Sever 的章节。
Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.