如何在 Debian 类系统(如 Debian、Ubuntu 等)中自动化部署 Apache HTTPD 网络服务器?以下是一个完整的指南,涵盖从安装软件包、创建自定义网页,到启动服务并开放防火墙端口的全过程。

在 Debian 类系统中部署 Apache HTTPD 网络服务器

任务步骤

  1. 安装 Apache HTTPD 软件包 ⇒ ansible.builtin.apt
  2. 创建自定义的 index.html 文件 ⇒ ansible.builtin.copy
  3. 启动 Apache HTTPD 服务并设置开机启动 ⇒ ansible.builtin.service
  4. 开放防火墙端口 ⇒ community.general.ufw

详细步骤

  1. 安装 apache2 软件包及其依赖项,使用 ansible.builtin.apt 模块。
  2. 使用 ansible.builtin.copy 模块创建自定义的 index.html 文件。
  3. 启动 apache2 服务并设置开机启动,使用 ansible.builtin.service 模块。
  4. 开放 HTTP 服务相关的防火墙端口,使用 community.general.ufw 模块。

示例代码

以下是一个 Ansible Playbook,用于在 Debian 类系统中部署 Apache HTTPD 网络服务器。

文件名:httpd_debian.yml

---
- name: 部署 Web 服务器
  hosts: all
  become: true
  tasks:
    - name: 安装 Apache HTTPD
      ansible.builtin.apt:
        name: apache2
        update_cache: true
        state: latest

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

    - name: 启用 Apache HTTPD 服务
      ansible.builtin.service:
        name: apache2
        enabled: true
        state: started

    - name: 开放防火墙端口
      community.general.ufw:
        rule: allow
        port: 80
        proto: tcp

执行命令

$ ansible-playbook -i virtualmachines/ubuntu/inventory services/httpd_debian.yml

执行结果

PLAY [部署 Web 服务器] *******************************************************

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

TASK [安装 Apache HTTPD] ****************************************************
changed: [ubuntu.example.com]

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

TASK [启用 Apache HTTPD 服务] ***********************************************
ok: [ubuntu.example.com]

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

PLAY RECAP *******************************************************************
ubuntu.example.com         : ok=5    changed=3    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

脚本的幂等性测试

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

$ ansible-playbook -i virtualmachines/ubuntu/inventory services/httpd_debian.yml

幂等性测试结果:

PLAY [部署 Web 服务器] *******************************************************

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

TASK [安装 Apache HTTPD] ****************************************************
ok: [ubuntu.example.com]

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

TASK [启用 Apache HTTPD 服务] ***********************************************
ok: [ubuntu.example.com]

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

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

执行前的系统状态

$ ssh devops@ubuntu.example.com
The authenticity of host 'ubuntu.example.com (192.168.0.191)' can't be established.
ECDSA key fingerprint is SHA256:SLtrfIjRKhuJtdTxjJ4V9yk+o/gO3MZi59KHehOQ3Ao.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'ubuntu.example.com,192.168.0.191' (ECDSA) to the list of known hosts.
$ sudo su
root@ubuntu:/home/devops# cat /etc/os-release 
NAME="Ubuntu"
VERSION="20.04.3 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.3 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal
root@ubuntu:/home/devops# apt list apache2
Listing... Done
apache2/focal-updates 2.4.41-4ubuntu3.8 amd64
apache2/focal-updates 2.4.41-4ubuntu3.8 i386
root@ubuntu:/home/devops# apt list apache2 --installed
Listing... Done
root@ubuntu:/home/devops# dpkg -l | grep apache2
root@ubuntu:/home/devops# cat /var/www/html/index.html
cat: /var/www/html/index.html: No such file or directory
root@ubuntu:/home/devops# ls -al /var/www
ls: cannot access '/var/www': No such file or directory
root@ubuntu:/home/devops#

执行后的系统状态

$ ssh devops@ubuntu.example.com
Last login: Mon Feb 14 14:26:02 2022 from 192.168.0.101
$ sudo su
root@ubuntu:/home/devops# apt list apache2
Listing... Done
apache2/focal-updates,focal-security,now 2.4.41-4ubuntu3.9 amd64 [installed]
apache2/focal-updates,focal-security 2.4.41-4ubuntu3.9 i386
root@ubuntu:/home/devops# dpkg -l | grep apache2
ii  apache2                              2.4.41-4ubuntu3.9                     amd64        Apache HTTP Server
ii  apache2-bin                          2.4.41-4ubuntu3.9                     amd64        Apache HTTP Server (modules and other binary files)
ii  apache2-data                         2.4.41-4ubuntu3.9                     all          Apache HTTP Server (common files)
ii  apache2-utils                        2.4.41-4ubuntu3.9                     amd64        Apache HTTP Server (utility programs for web servers)
root@ubuntu:/home/devops# systemctl status apache2
● apache2.service - The Apache HTTP Server
     Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2022-02-14 14:25:31 UTC; 1min 20s ago
       Docs: https://httpd.apache.org/docs/2.4/
    Main PID: 3182 (apache2)
       Tasks: 55 (limit: 1071)
      Memory: 5.1M
      CGroup: /system.slice/apache2.service
              ├─3182 /usr/sbin/apache2 -k start
              ├─3185 /usr/sbin/apache2 -k start
              └─3186 /usr/sbin/apache2 -k start
Feb 14 14:25:31 ubuntu systemd[1]: Starting The Apache HTTP Server...
Feb 14 14:25:31 ubuntu systemd[1]: Started The Apache HTTP Server.
root@ubuntu:/home/devops# cat /var/www/html/index.html
Custom Web Page
root@ubuntu:/home/devops# ls -al /var/www/
total 12
drwxr-xr-x  3 root root 4096 Feb 14 14:25 .
drwxr-xr-x 14 root root 4096 Feb 14 14:25 ..
drwxr-xr-x  2 root root 4096 Feb 14 14:25 html
root@ubuntu:/home/devops#

通过以上步骤,您可以在 Debian 类系统中使用 Ansible 自动化部署 Apache HTTPD 网络服务器。