gcoop-libre.apache
Apache 2.x
一个 Ansible 角色,用于在 RHEL/CentOS、Debian/Ubuntu、SLES 和 Solaris 上安装 Apache 2.x。
需求
该角色不管理使用 SSL/TLS 的站点证书和密钥文件。
如果您需要带 PHP 的 Apache,可以将 PHP 包添加到 apache_packages
变量中。或者,如果您更喜欢将 PHP 作为 FPM 而不是 Apache 模块使用,可以使用其他角色,例如 geerlingguy.php
角色或 geerlingguy.apache-php-fpm
。
角色变量
可用的变量如下所列,并附有默认值(参见 defaults/main.yml
):
apache_enablerepo: ''
安装 Apache 时使用的仓库(仅在 RHEL/CentOS 系统上使用)。如果您希望获得比操作系统核心仓库中可用的更新版本的 Apache,请使用 EPEL 之类的仓库。
apache_listen_ip: '*'
apache_listen_port: 80
apache_listen_port_ssl: 443
Apache 应该监听的 IP 地址和端口。如果您有其他服务(例如反向代理)在 80 或 443 端口上监听,需要更改默认值时很有用。
apache_keepalive: true
apache_keepalive_requests: 500
apache_keepalive_timeout: 5
此属性启用 HTTP 持久连接,其超时和最大请求可以自定义。
apache_prefork_server_limit: 256
apache_prefork_start_servers: 5
apache_prefork_min_spare_servers: 5
apache_prefork_max_spare_servers: 10
apache_prefork_max_clients: 256
apache_prefork_max_requests_per_child: 0
服务器 MPM 模块的配置。此属性将适用于 MPM Prefork 模块和 MPM Worker 模块。仅在 Debian 的 Apache 2.4 上,您可以使用不同的值配置 MPM 模式,因为它有独立的文件。
apache_worker_server_limit: 16
apache_worker_start_servers: 3
apache_worker_min_spare_threads: 75
apache_worker_max_spare_threads: 250
apache_worker_max_clients: 400
apache_worker_max_requests_per_child: 0
apache_worker_thread_limit: 64
apache_worker_threads_per_child: 25
MPM Worker 模块的配置。这仅在 Debian GNU/Linux 上的 Apache 2.4 可用。
apache_event_server_limit: 16
apache_event_start_servers: 3
apache_event_min_spare_threads: 75
apache_event_max_spare_threads: 250
apache_event_max_clients: 400
apache_event_max_requests_per_child: 0
apache_event_thread_limit: 64
apache_event_threads_per_child: 25
MPM Event 模块的配置。这仅在 Debian GNU/Linux 上的 Apache 2.4 可用。
apache_remove_default_aliases: True
删除默认 Apache 配置带来的默认别名。
apache_base_dir: '/var/www'
将分配网站的基本目录。此值与接下来的三个一起使用,以生成没有显式 Document Root 的虚拟主机文档根。
apache_web_subdir: 'web'
apache_ssl_subdir: 'ssl'
apache_private_subdir: 'private'
用于 HTTP 网络文件的子目录、用于 HTTPS 网络文件的子目录以及用于 htpasswd 文件的子目录。如果虚拟主机没有 Document Root,角色会生成三个目录:
apache_base_dir/SITENAME/apache_web_subdir
apache_base_dir/SITENAME/apache_ssl_subdir
apache_base_dir/SITENAME/apache_private_subdir
apache_create_vhosts: true apache_vhosts_filename: 'vhosts.conf'
如果设置为 true,将创建一个全局虚拟主机配置文件以及每个虚拟主机一个由该角色的变量管理的文件(见下文),并放置在 Apache 配置文件夹中。如果设置为 false,您可以将自己的虚拟主机文件放入 Apache 的配置文件夹中,跳过该角色添加的便利(但更基本的)文件。
apache_remove_default_vhost: false
在 Debian/Ubuntu 上,Apache 配置中包含一个默认虚拟主机。将其设置为 true
可删除该默认虚拟主机关联的配置文件。
apache_global_vhost_settings: |
DirectoryIndex index.php index.html
# 在后续行添加其他全局设置。
您可以使用此变量在角色提供的虚拟主机文件中添加或覆盖全局 Apache 配置设置(假设 apache_create_vhosts
为 true)。默认情况下,只有 DirectoryIndex 配置。
apache_global_vhost_access_log_format_combined: '%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"'
apache_global_vhost_access_log_format_proxy: '%{X-Forwarded-For}i %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"'
通过这些属性,您可以自定义组合日志格式和用于记录通过代理请求的日志格式。
apache_global_vhost_aliases:
- name: /images
dest: /var/www/images
要添加到 vhost.conf 的别名列表。将对所有虚拟主机可用。apache_create_vhosts
应该启用。
apache_global_vhost_custom_errors:
- code: error-code
action: error-action
Apache 错误页面的自定义。您可以在 Apache 文档 上阅读有关 action
值的信息。
apache_vhosts:
- servername: 'local.dev'
serveralias:
- 'alias1.local'
- 'alias2.local'
serveradmin: webmaster@localhost
documentroot: '/var/www/html'
enabled: true
separate_logs: true
separate_logs_proxy_format: true
custom_errors:
- code: error-code
action: error-action
frame_options: SAMEORIGIN
deflate: true
deflate_dont_vary: User-Agent
fileetag: true
aliases:
- name: alias-url
dest: alias-path
setenvif:
- attribute: 'X-Forwarded-For'
pattern: '(.*)'
var: 'ENV_VAR'
value: 'true'
redirect_to_https: false
allowoverride: 'All'
rewritebase: '/'
custom_rewrites:
- pattern: regex
substitution: text
flags: '[R=301,L]'
conditions:
- test_string: '%{HTTP_HOST}'
pattern: '^old\.site\.com$'
flags: '[NC]'
redirect_to_servername: True
exclude_from_redirect:
- 'valid.alias.com'
redirect_to_file: '/index.php'
include_htaccess: False
documentroot_extra_parameters: '自定义文档根配置'
restricted_access:
- path: '/secret'
regex: false
all: false
ips:
- '127.0.0.1'
- '192.168.0.1'
hosts:
- 'www.site.com'
env_variables:
- 'ENV_VAR'
htpasswd: '/.htpasswd'
extra_parameters: '自定义虚拟主机配置'
为每个虚拟主机添加一组属性。唯一必需的是 servername
。如果没有 documentroot,它将按照上述描述生成。
对于服务器别名的所有请求将重定向到服务器名称,并使用 301 代码,除了在 exclude_from_redirect
属性中指定的别名。
restricted_access
中的路径应相对 Document Root。如果虚拟主机没有显式的 Document Root,htpasswd 文件将位于 apache_private_subdir 目录中。
|
在 YAML 中表示多行缩进块,因此换行符在生成的配置文件中得以保留。
apache_vhosts_ssl: []
默认情况下没有配置 SSL 虚拟主机,但您可以使用与 apache_vhosts
相同的模式添加它们,并添加一些附加指令:
apache_vhosts_ssl:
- servername: 'local.dev',
certificate_file: '/path/to/certificate.crt'
certificate_key_file: '/path/to/certificate.key'
certificate_chain_file: '/path/to/certificate_chain.crt'
sts: true
sts_max_age: '63072000'
sts_subdomains: true
redirect_to_http: false
前三个属性设置证书路径。接下来的三个允许覆盖全局 STS 配置。最后一个将所有请求重定向到 HTTP 主机。
apache_vhosts_ordered: False
此属性强制 Apache 按照 apache_vhosts
和 apache_vhosts_ssl
列表中出现的顺序加载虚拟主机。否则,Apache 按字母顺序处理配置文件。
可以使用其他与 SSL 相关的角色变量管理其他 SSL 指令。
apache_ssl_protocol: 'All -SSLv2 -SSLv3'
apache_ssl_cipher_suite: 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH'
客户端与您的服务器建立安全连接时使用/允许的 SSL 协议和加密套件。这些是安全/合理的默认值,但为了最大安全性、性能和/或兼容性,您可能需要调整这些设置。您可以在 Cipherli.st:Apache、nginx 和 Lighttpd 的强加密套件 中找到一些信息。
apache_frame_options: SAMEORIGIN
此属性设置站点在 Frame 或 IFrame 中加载时的安全策略。您可以查看 这个维基百科文章 中的有效选项。您还可以在这里使用 False
或 no
来完全跳过设置此头。此全局配置可以逐个虚拟主机覆盖。
apache_sts: true
apache_sts_max_age: '63072000'
apache_sts_subdomains: true
默认情况下启用 HTTP 严格传输安全,最大有效期为 1 年,并包含子域。此全局配置可以逐个虚拟主机覆盖。
apache_mods_enabled:
- rewrite
- ssl
apache_mods_disabled: []
这些属性仅适用于 Debian 和 Ubuntu。要启用或禁用的 Apache 模块。请查看 Apache 配置目录(默认:/etc/apache2/mods-available
)中的 mods-available
目录,了解所有可用模块。
apache_packages:
- [特定平台]
要安装的软件包列表。默认情况下,这对 RedHat 或 Debian 系统是特定平台的一组软件包(有关默认值,请参见 vars/RedHat.yml
和 vars/Debian.yml
)。
apache_state: started
设置此角色运行时 Apache 守护进程的初始状态。通常应保持为 started
,但如果您需要在 playbook 执行期间修复 Apache 配置或不想在运行此角色时启动 Apache,可以将其设置为 stopped
。
apache_ignore_missing_ssl_certificate: true
如果您只希望在 vhost 证书存在时创建 SSL 虚拟主机(例如,使用 Let's Encrypt 时),请将 apache_ignore_missing_ssl_certificate
设置为 false
。这样做时,您可能需要多次运行 playbook,以便配置所有虚拟主机(如果 playbook 的其他部分生成 SSL 证书)。
依赖关系
无。
示例 Playbook
- hosts: webservers
vars_files:
- vars/main.yml
roles:
- gcoop-libre.apache
在 vars/main.yml
中:
apache_listen_port: 8080
apache_vhosts:
- servername: example.com
许可证
GPLv2
作者信息
此角色由 gcoop 自由软件合作社 于 2016 年创建。
ansible-galaxy install gcoop-libre.apache