如何在类 RedHat 系统中自动化部署 Apache HTTPD 虚拟主机?以下是一个完整的指南,涵盖从安装软件包、创建网站根目录、配置自定义网页,到设置虚拟主机、启动服务并开放防火墙端口的全过程。支持的系统包括 CentOS、Fedora、Rocky Linux、AlmaLinux、Amazon Linux 等。

在类 RedHat 系统中部署 Apache HTTPD 虚拟主机

使用的 Ansible 模块

  1. 安装软件包 ⇒ ansible.builtin.yum
  2. 创建网站根目录 ⇒ ansible.builtin.file
  3. 创建自定义 index.html ⇒ ansible.builtin.copy
  4. 配置 Apache 虚拟主机 ⇒ ansible.builtin.template
  5. 启动服务 ⇒ ansible.builtin.service
  6. 开放防火墙端口 ⇒ ansible.posix.firewalld

以下是详细步骤:

  1. 安装 httpd 软件包及其依赖项,使用 ansible.builtin.yum 模块。
  2. 创建网站根目录并设置正确权限,使用 ansible.builtin.file 模块。
  3. 创建自定义的 index.html 文件,使用 ansible.builtin.copy 模块。也可以通过 template 模块进一步优化。
  4. 为特定虚拟主机配置 Apache,使用 ansible.builtin.template 模块。
  5. 启动 httpd 服务并设置开机启动,使用 ansible.builtin.service 模块。
  6. 开放与 HTTP 服务相关的防火墙端口,使用 ansible.posix.firewalld 模块。

示例代码

以下是一个 Ansible Playbook,用于在类 RedHat 系统中部署 Apache HTTPD 虚拟主机。

文件名:httpd_redhat_vhost.yml

---
- name: 部署带虚拟主机的 Web 服务器
  hosts: all
  become: true
  vars:
    app_user: "apache"  # 应用程序用户
    http_host: "example.com"  # 虚拟主机域名
    http_conf: "example.com.conf"  # 虚拟主机配置文件名
    http_port: "80"  # HTTP 服务端口
  tasks:
    - name: 安装 httpd
      ansible.builtin.yum:
        name: httpd
        state: latest

    - name: 创建网站根目录
      ansible.builtin.file:
        path: "/var/www/{{ http_host }}"
        state: directory
        owner: "{{ app_user }}"
        mode: '0755'
        setype: "httpd_sys_content_t"

    - name: 创建自定义 index.html
      ansible.builtin.copy:
        dest: "/var/www/{{ http_host }}/index.html"
        content: |
          Custom Web Page

    - name: 配置 Apache 虚拟主机
      ansible.builtin.template:
        src: "templates/httpd.conf.j2"
        dest: "/etc/httpd/conf.d/{{ http_conf }}"

    - name: 启用 httpd 服务
      ansible.builtin.service:
        name: httpd
        enabled: true
        state: restarted

    - name: 开放防火墙端口
      ansible.posix.firewalld:
        service: http
        state: enabled
        immediate: true
        permanent: true

模板文件:templates/httpd.conf.j2

<VirtualHost *:{{ http_port }}>
  ServerAdmin webmaster@localhost
  ServerName {{ http_host }}
  ServerAlias www.{{ http_host }}
  ErrorLog /var/log/httpd/error.log
  CustomLog /var/log/httpd/access.log combined
  DocumentRoot "/var/www/{{ http_host }}"
</VirtualHost>

执行命令

$ ansible-playbook -i virtualmachines/demo/inventory services/httpd_redhat_vhost.yml

执行结果

PLAY [部署带虚拟主机的 Web 服务器] *******************************************************

TASK [Gathering Facts] ******************************************************
ok: [demo.example.com]

TASK [安装 httpd] ***********************************************************
changed: [demo.example.com]

TASK [创建网站根目录] *******************************************************
changed: [demo.example.com]

TASK [创建自定义 index.html] ***********************************************
changed: [demo.example.com]

TASK [配置 Apache 虚拟主机] *************************************************
changed: [demo.example.com]

TASK [启用 httpd 服务] *******************************************************
changed: [demo.example.com]

TASK [开放防火墙端口] *********************************************************
changed: [demo.example.com]

PLAY RECAP *******************************************************************
demo.example.com           : ok=7    changed=6    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

脚本的幂等性测试

再次运行 Playbook 以验证幂等性:

$ ansible-playbook -i virtualmachines/demo/inventory services/httpd_redhat_vhost.yml

幂等性测试结果:

PLAY [部署带虚拟主机的 Web 服务器] *******************************************************

TASK [Gathering Facts] ******************************************************
ok: [demo.example.com]

TASK [安装 httpd] ***********************************************************
ok: [demo.example.com]

TASK [创建网站根目录] *******************************************************
ok: [demo.example.com]

TASK [创建自定义 index.html] ***********************************************
ok: [demo.example.com]

TASK [配置 Apache 虚拟主机] *************************************************
ok: [demo.example.com]

TASK [启用 httpd 服务] *******************************************************
ok: [demo.example.com]

TASK [开放防火墙端口] *********************************************************
ok: [demo.example.com]

PLAY RECAP *******************************************************************
demo.example.com           : ok=7    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

执行前的系统状态

$ ssh devops@demo.example.com
Last login: Tue Mar  1 15:44:18 2022 from 192.168.0.59
[devops@demo ~]$ sudo su
[root@demo devops]# dnf list httpd
Updating Subscription Management repositories.
Last metadata expiration check: 0:01:21 ago on Tue 01 Mar 2022 03:43:25 PM UTC.
Installed Packages
httpd.x86_64      2.4.37-43.module+el8.5.0+13806+b30d9eec.1       @rhel-8-for-x86_64-appstream-rpms
[root@demo devops]# rpm -qa | grep httpd
httpd-tools-2.4.37-43.module+el8.5.0+13806+b30d9eec.1.x86_64
redhat-logos-httpd-84.5-1.el8.noarch
httpd-filesystem-2.4.37-43.module+el8.5.0+13806+b30d9eec.1.noarch
httpd-2.4.37-43.module+el8.5.0+13806+b30d9eec.1.x86_64
[root@demo devops]# cat /etc/httpd/conf.d/example.com.conf
cat: /etc/httpd/conf.d/example.com.conf: No such file or directory
[root@demo devops]# cat /var/www/example.com/index.html 
cat: /var/www/example.com/index.html: No such file or directory
[root@demo devops]# systemctl status httpd
Unit httpd.service could not be found.
[root@demo devops]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: eth0 eth1
  sources: 
  services: cockpit dhcpv6-client ssh
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
[root@demo devops]#

执行后的系统状态

$ ssh devops@demo.example.com
Last login: Tue Mar  1 15:44:18 2022 from 192.168.0.59
[devops@demo ~]$ sudo su
[root@demo devops]# dnf list httpd
Updating Subscription Management repositories.
Last metadata expiration check: 0:01:21 ago on Tue 01 Mar 2022 03:43:25 PM UTC.
Installed Packages
httpd.x86_64      2.4.37-43.module+el8.5.0+13806+b30d9eec.1       @rhel-8-for-x86_64-appstream-rpms
[root@demo devops]# rpm -qa | grep httpd
httpd-tools-2.4.37-43.module+el8.5.0+13806+b30d9eec.1.x86_64
redhat-logos-httpd-84.5-1.el8.noarch
httpd-filesystem-2.4.37-43.module+el8.5.0+13806+b30d9eec.1.noarch
httpd-2.4.37-43.module+el8.5.0+13806+b30d9eec.1.x86_64
[root@demo devops]# cat /etc/httpd/conf.d/example.com.conf
<VirtualHost *:80>
  ServerAdmin webmaster@localhost
  ServerName example.com
  ServerAlias www.example.com
  ErrorLog /var/log/httpd/error.log
  CustomLog /var/log/httpd/access.log combined
  DocumentRoot "/var/www/example.com"
</VirtualHost>
[root@demo devops]# cat /var/www/example.com/index.html 
Custom Web Page
[root@demo devops]# ls -al /var/www/example.com/
total 4
drwxr-xr-x. 2 apache root 24 Mar  1 15:43 .
drwxr-xr-x. 5 root   root 52 Mar  1 15:43 ..
-rw-r--r--. 1 root   root 16 Mar  1 15:43 index.html
[root@demo devops]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2022-03-01 15:43:39 UTC; 2min 20s ago
     Docs: man:httpd.service(8)
 Main PID: 7516 (httpd)
   Status: "Running, listening on: port 80"
    Tasks: 213 (limit: 4952)
   Memory: 25.0M
   CGroup: /system.slice/httpd.service
           ├─7516 /usr/sbin/httpd -DFOREGROUND
           ├─7517 /usr/sbin/httpd -DFOREGROUND
           ├─7518 /usr/sbin/httpd -DFOREGROUND
           ├─7519 /usr/sbin/httpd -DFOREGROUND
           └─7520 /usr/sbin/httpd -DFOREGROUND
Mar 01 15:43:38 demo.example.com systemd[1]: Starting The Apache HTTP Server...
Mar 01 15:43:39 demo.example.com systemd[1]: Started The Apache HTTP Server.
Mar 01 15:43:39 demo.example.com httpd[7516]: Server configured, listening on: port 80
[root@demo devops]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: eth0 eth1
  sources: 
  services: cockpit dhcpv6-client http ssh
  ports: 
  protocols: 
  forward: no
  masquerade: no
  forward-ports: 
  source-ports: 
  icmp-blocks: 
  rich rules: 
[root@demo devops]# cat /var/log/httpd/access.log
192.168.0.59 - - [01/Mar/2022:16:03:58 +0000] "GET / HTTP/1.1" 200 17 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.1 Safari/605.1.15"
192.168.0.59 - - [01/Mar/2022:16:03:58 +0000] "GET /favicon.ico HTTP/1.1" 404 196 "http://demo.example.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.1 Safari/605.1.15"
192.168.0.59 - - [01/Mar/2022:16:04:02 +0000] "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.1 Safari/605.1.15"
192.168.0.59 - - [01/Mar/2022:16:04:10 +0000] "GET /favicon.ico HTTP/1.1" 404 196 "http://demo.example.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.1 Safari/605.1.15"
192.168.0.59 - - [01/Mar/2022:16:04:55 +0000] "GET / HTTP/1.1" 304 - "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.1 Safari/605.1.15"
192.168.0.59 - - [01/Mar/2022:16:04:57 +0000] "-" 408 - "-" "-"