Turgon37.apache2
Ansible 角色 Apache2
描述
:grey_exclamation: 在使用此角色之前,请注意我的所有 Ansible 角色都是根据我的 IT 基础设施编写的。因此,即使它们尽可能通用,也不一定能满足您的需求,我建议您仔细分析它们的功能并评估它们安全安装在您服务器上的能力。
此角色配置 Apache2 守护进程实例。
要求
需要 Ansible >= 2.4
依赖
如果您使用 Zabbix 监控配置文件,则需要角色 ansible-zabbix-agent
操作系统家族
此角色适用于 Debian
特性
截至目前,此角色可以用于:
- 安装 Apache2
- 配置主服务器文件
- 创建虚拟主机配置
- 管理启用的模块
- 重写某些模块配置
- Zabbix 监控项
- 本地事实
配置
服务器
所有可以重写的变量都存储在 defaults/main.yml 文件以及下表中。要查看默认值,请参考该文件。
名称 | 描述 |
---|---|
apache2__version |
选择要安装的 apache2 版本(根据操作系统软件库中的可用版本)例如:2.4.25-3+deb9u5 |
apache2__service_enabled |
一个布尔值,表示在启动和运行时是否启用 Apache 服务 |
apache2__service_restartable |
如果为真,则在配置更改时会自动重启 Apache 服务(在生产环境中设置为 false) |
apache2__server_tokens |
配置错误页面中的服务器令牌的详细信息 |
apache2__server_signature |
是否在错误页面上打印服务器签名 |
apache2__trace_enable |
配置 HTTP TRACE 方法 |
apache2__ssl_ciphers |
列出可用的 SSL 加密算法,默认配置仅推荐子集 |
apache2__ssl_protocols |
列出启用的 SSL 协议,默认配置为所有协议,除外 SSL 2/3 |
apache2__ssl_honorciphers |
告诉服务器优先使用其加密顺序,而不是客户端的顺序 |
apache2__log_formats |
一个字典,包含 Apache 可用的所有日志格式。 |
apache2__listen_http |
Apache 监听 HTTP 请求的端口/主机列表 |
apache2__listen_https |
Apache 监听 HTTPS 请求的端口/主机列表 |
关于 apache2__listen_http(s) 的说明,目前这些指令是手动填写的,我计划自动生成它,但这似乎很复杂,因为虚拟主机可以通过 include_role 定义。
以下变量适用于服务器,并可以在每个虚拟主机中重写:
名称 | 描述 |
---|---|
apache2__serveradmin |
管理员的可选电子邮件地址 |
apache2__allow_override_list |
AllowOverrideList 指令 |
apache2__allow_override |
AllowOverride 指令 |
apache2__options |
Option 指令 |
要配置启用或不启用的模块,您必须在以下三个列表中的任何一个中声明所有模块名称:
- apache2__modules_enabled_global
- apache2__modules_enabled_group
- apache2__modules_enabled_host
默认情况下,没有任何模块被启用,因此请注意,如果没有启用 mpm 模块,Apache 将无法启动。 这些列表中的每个条目必须是模块名称。如果某个模块有 .conf 和 .load 文件,它们将自动被包含。另外,如果角色包含 templates/modules.conf/(module name) 目录中的模板文件,它将替换任何现有的发行版配置文件。
虚拟主机
每个虚拟主机必须与 vhost 块声明。您可以将 vhost 块放入以下三种可用列表中的任何一个:
- apache2__virtual_hosts_global
- apache2__virtual_hosts_group
- apache2__virtual_hosts_host
默认情况下,每个虚拟主机在 '*' 上监听,并在根据 HTTP(s) 状态确定的默认端口上监听。 如果 SSL 引擎为 'ON',则使用 443,否则使用 80。
只有部分 Apache2 指令和部分在 ansible 中实现,您可以在文件 directives sections 中查看可用的。如果您需要未实施的指令,可以使用 extra_parameters 项目。如果缺少部分类型,则需要 fork 并在角色中实现它。
每个 vhost 块必须放在一个字典中,其中的键将是 vhost 配置的文件名。然后每个 vhost 必须是一个字典,可以包含以下变量:
名称 | 类型 | 描述 |
---|---|---|
hosts | 字符串或字符串/字典数组 | 要监听的接口列表 |
hosts[] | 字符串 | 如果 hosts 列表中的某个项为字符串,则将其解释为 "IP:PORT" 或 "X.X.X.X:X" |
hosts[].ip | 字符串 | 如果 hosts 列表中的某个项为具有 'ip' 键的字典,则将其解释为 "IP" |
hosts[].port | 整数 | 如果 hosts 列表中的某个项为具有 'port' 键的字典,则将用于监听端口。如果未设置此键,则将根据 HTTP 协议进行推断(见上文) |
server_name | 字符串 | vhost 的主机 |
server_alias | 字符串 | 主机名别名 |
server_admin | 字符串 | 可选的管理员电子邮件地址 |
document_root | 字符串 | 文档根文件夹的路径。此目录将自动创建,因为如果缺少此目录,Apache 将不会启动。 |
document_root_user | 字符串 | 文档根文件夹的 Unix 所有者。仅在定义该变量时应用 |
document_root_group | 字符串 | 文档根文件夹的 Unix 组。仅在定义该变量时应用 |
document_root_mode | 字符串 | 文档根文件夹的 Unix 模式。仅在定义该变量时应用。请注意,Apache 至少仍需以只读模式访问此文件夹 |
error_log | 字符串 | 错误日志文件的路径。错误日志文件将在此目录中创建,因此请确保 Apache 仍具有足够的访问权限 |
error_log_user | 字符串 | 错误日志目录的 Unix 所有者 |
error_log_group | 字符串 | 错误日志目录的 Unix 组 |
allow_override | 字符串 | AllowOverride 指令 |
allow_override_list | 字符串 | AllowOverrideList 指令 |
options | 字符串 | Option 指令 |
headers | 数组 | Header 指令的数组 |
files_match | 字典数组(见下文) | 包含 FileMatch 定义,每个都必须是具有以下键的字典 |
files_match[].regexp | 字符串 | 触发文件匹配的正则表达式 |
files_match[].actions | 字符串数组 | 当此文件匹配被触发时要执行的 Apache 指令列表 |
extra_parameters | 字符串数组 | 任何额外的 Apache 指令 |
https | 字典 | 见下文所有 https 子键 |
https.enabled | 布尔值 | 默认为真,可用于禁用 https 并保留配置字符串 |
https.certificate_chain_file | 字符串 | 证书链的路径 |
https.verify_client | 字符串 | 要执行的客户端证书验证类型 |
https.verify_client_depth | 整数 | 客户端证书验证的最大深度 |
https.ca_certificate_path | 字符串 | CA 证书目录的路径 |
https.ca_certificate_file | 字符串 | CA 证书文件的路径 |
https.crl_path | 字符串 | CRL 文件夹的路径 |
https.crl_file | 字符串 | CRL 文件的路径 |
事实
默认情况下,本地事实被安装并暴露以下变量:
ansible_local.apache2.version_full
ansible_local.apache2.version_major
示例
Playbook
在 Playbook 中按如下方式使用:
- hosts: all
roles:
- turgon37.apache2
库存
- 手动加载的 Apache 模块示例
apache2__modules_enabled_group:
- access_compat # 为已弃用的 Allow,Order 指令提供支持
- alias # 提供 Alias
# - auth_basic # 提供基本 HTTP 身份验证
- authn_core
# - authn_file # 基于 htpasswd 的身份验证
- authz_core
# - authz_host # 基于 ip/host 的身份验证
# - authz_user # 基于用户名的身份验证
# - autoindex # 被禁用,索引被禁用
- deflate # 提供 Gzip 压缩
- dir # 提供 DirectoryIndex
- env # 提供 SetEnv
# - filter # 提供 FilterChain
- headers # 提供 RequestHeader
- mime
- mpm_prefork
- negotiation # 处理内容类型
- php7.0
# - proxy
# - proxy_http
# - setenvif
- ssl # 处理 SSL
- socache_shmcb # mod_ssl 所需
- 默认 Debian 虚拟主机
apache2__host_virtual_hosts:
000-default:
server_name: www.example.com
server_admin: webmaster@localhost
document_root: /var/www/html
sections:
- type: directory
path: /var/www/html
directives:
- require: all granted
error_log: '{{ apache2__log_directory }}/error.log'
custom_log: '{{ apache2__log_directory }}/access.log combined'
- 从 HTTP 到 HTTPS 的简单永久重定向
apache2__host_virtual_hosts:
web-redirect:
hosts:
- ip: "10.0.0.1"
server_name: www.example.net
server_alias: www2.example.net
extra_parameters:
- RedirectPermanent / https://www.example.net/
- 从 HTTPS 到 HTTP 的代理传递
apache2__host_virtual_hosts:
proxy-https:
hosts:
- ip: 10.0.0.1
- ip: 192.168.56.12
server_name: www.example.net
server_alias: www2.example.net
extra_parameters:
- 'ProxyRequests Off'
- 'ProxyPreserveHost On'
- 'ProxyPass / http://localhost:3001/'
- 'ProxyPassReverse / https://localhost:3001/'
https:
certificate_file: /etc/ssl/apache2/www.example.net.pem
certificate_key_file: /etc/ssl/apache2/www.example.net.key
- 带文档根和 PHP 应用程序(Jeedom)的 HTTP 虚拟主机
apache2__host_virtual_hosts:
hosts:
- 10.0.0.1
- "127.0.0.1:443"
server_name: jeedom.example.net
document_root: /var/www/html
document_root: '{{ jeedom__install_directory }}'
document_root_user: '{{ apache2__service_user }}'
document_root_group: '{{ apache2__service_user }}'
error_log: '{{ jeedom__install_directory }}/log/http.error'
error_log_user: '{{ apache2__service_user }}'
error_log_group: '{{ apache2__service_user }}'
sections:
- type: directory
path: '{{ jeedom__install_directory }}'
directives:
- allow_override: All
- options: -Indexes -ExecCGI -FollowSymLinks
- require: all granted
- type: files_match
regex: '\.(appcache|atom|bbaw|bmp|crx|css|cur|eot|f4[abpv]|flv|geojson|gif|htc|ico|jpe?g|js|json(ld)?|m4[av]|manifest|map|mp4|oex|og[agv]|opus|otf|pdf|png|rdf|rss|safariextz|svgz?|swf|topojson|tt[cf]|txt|vcard|vcf|vtt|webapp|web[mp]|webmanifest|woff2?|xloc|xml|xpi)$'
directives:
- header:
- unset Content-Security-Policy
- unset X-Frame-Options
- unset X-XSS-Protection
directives:
- header:
# 内容安全策略 (CSP)
#- set Content-Security-Policy "script-src 'self'; object-src 'self'"
# 降低 MIME 类型安全风险
- set X-Content-Type-Options "nosniff"
# 点击劫持
- set X-Frame-Options "DENY"
# 反射跨站脚本 (XSS) 攻击
- set X-XSS-Protection "1; mode=block"
- unset X-Powered-By
- 带文档根和 PHP 应用程序(Jeedom)的 HTTPS 虚拟主机
apache2__host_virtual_hosts:
jeedom-https:
hosts:
- ip: 10.0.0.1
- ip: 127.0.0.1
port: 4343
server_name: jeedom.example.net
document_root: /var/www/html
allow_override: All
options: '-Indexes -ExecCGI -FollowSymLinks'
headers:
- set X-Content-Type-Options "nosniff"
- always set Strict-Transport-Security "max-age=16070400; includeSubDomains"
- set X-XSS-Protection "1; mode=block"
- unset X-Powered-By
files_match:
- regexp: '\.(appcache|atom|bbaw|bmp|crx|css|cur|eot|f4[abpv]|flv|geojson|gif|htc|ico|jpe?g|js|json(ld)?|m4[av]|manifest|map|mp4|oex|og[agv]|opus|otf|pdf|png|rdf|rss|safariextz|svgz?|swf|topojson|tt[cf]|txt|vcard|vcf|vtt|webapp|web[mp]|webmanifest|woff2?|xloc|xml|xpi)$'
actions:
- Header unset Content-Security-Policy
- Header unset X-Frame-Options
- Header unset X-XSS-Protection
https:
certificate_file: /etc/ssl/apache2/jeedom.www.example.net.pem
certificate_key_file: /etc/ssl/apache2/jeedom.www.example.net.key