damianlewis.apache
Ansible 角色:Apache
安装和配置 Apache。
要求
无。
角色变量
可用的变量如下所列,默认值见 defaults/main.yml
。
apache_use_ppa: false
要使用 PPA 仓库安装 Apache,请将 apache_use_ppa
变量设置为 true
。
apache_user: www
apache_group: www
您可以更改 Apache 运行的用户和组。默认情况下,Apache 将使用 www-data
作为用户和组。
apache_timeout: '100'
apache_enable_keepalive: false
apache_max_keepalive_request: '200'
apache_keepalive_timeout: '300'
apache_enable_hostname_lookups: true
可以通过设置以上变量之一来覆盖一些 Apache 的主要配置指令。
apache_log_path: /path/to/logs
apache_error_log_level: warn
您可以使用 apache_error_log_level
变量配置错误日志。错误日志级别可以是以下之一:trace8, ..., trace1, debug, info, notice, warn, error, crit, alert,
或 emerg
。默认的错误日志级别为 warn
。
您可以通过设置 apache_log_path
变量来覆盖日志文件的存储位置。默认位置为 /var/log/apache2
。
apache_disable_default_site: true
禁用默认的 Apache 安装站点。
apache_pid_file: logs/apache.pid
默认的 pid
指令设置为 /var/run/apache2/apache2.pid
。使用 apache_pid_file
变量来覆盖此设置。
apache_run_path: /var/run/apache2
apache_lock_path: /var/lock/apache2
使用 apache_run_path
和 apache_lock_path
变量来覆盖临时状态文件位置的默认值。apache_run_path
的默认值为 /var/run/apache2
,apache_lock_path
的默认值为 /var/lock/apache2
。
apache_http_port: '8080'
apache_https_port: '44300'
要覆盖 HTTP 和 HTTPS 的默认端口,请使用 apache_http_port
和 apache_https_port
变量。
apache_allow_additional_confs: false
默认情况下,Apache 将包含在 conf.d
或 conf-enabled
文件夹中找到的任何配置文件。要排除这些附加配置,请将 apache_allow_additional_confs
变量设置为 false
。
apache_modules:
- name: headers
- name: rewrite
- name: ssl
state: absent
使用 apache_modules
变量来启用/禁用 Apache 模块。name
属性是必需的,应为要启用/禁用模块的名称。默认情况下,任何列出的模块将被启用。要禁用模块,请添加 state
属性并设置值为 absent
。
apache_sites:
- hostname: www.example.com
root: /var/www/html
alias: *.example.com
使用 apache_sites
变量来添加和配置站点。站点所需的唯一属性是 hostname
和 root
文件夹的路径。可以添加可选的服务器 alias
。
apache_sites:
- hostname: example.com
root: /var/www/html
state: absent
默认情况下,当添加到 apache_sites
变量时会创建一个站点。使用 state
属性并设置值为 absent
来禁用/移除站点。
apache_sites:
- hostname: example.com
root: /var/www/html
enable_http_to_https_redirect: true
将 enable_http_to_https_redirect
变量设置为将 HTTP 请求重定向到 HTTPS 协议。确保已配置 SSL 证书,并且 Apache 已配置为使用 SSL,如下所述。
apache_sites:
- hostname: www.sub.example.com
root: /var/www/subdomain/sub
virtualhost_directives: |
ServerPath "/sub/"
RewriteEngine On
RewriteRule "^(/sub/.*)" "/var/www/subdomain$1"
可以使用 virtualhost_directives
属性添加其他虚拟主机指令。
apache_sites:
- hostname: example.com
root: /var/www/html
headers:
- 'X-Frame-Options "DENY"'
- 'X-Content-Type-Options "nosniff"'
- 'X-XSS-Protection "1; mode=block"'
使用 headers
属性添加服务器响应头。
apache_sites:
- hostname: example.com
root: /var/www/html
directories:
- rule: '/var/www/html'
block: |
Options -Indexes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
通过将其添加到 directories
属性中,可以创建目录指令。指定目录的 rule
和要包含的 block
指令。
apache_sites:
- hostname: example.com
root: /var/www/html
filesmatches:
- rule: '.+\.ph(p[3457]?|t|tml)$'
block: |
SetHandler proxy:unix:/run/php/php7.2-fpm.sock|fcgi://localhost
通过将其添加到 filesmatches
属性中,可以创建 FilesMatch 指令。指定正则表达式的 rule
和要包含的 block
指令。
apache_sites:
- hostname: example.com
root: /var/www/html
enable_error_log: true
error_log_level: warn
enable_access_log: true
access_log_format: main
要为各个站点创建单独的日志文件,请将 enable_error_log
和 enable_access_log
属性设置为 true
。使用 error_log_level
和 access_log_format
属性配置日志。错误日志级别可以是以下之一:trace8, ..., trace1, debug, info, notice, warn, error, crit, alert,
或 emerg
。默认的错误日志级别为 warn
。要使用自定义访问日志格式,请确保该格式存在。默认的访问日志格式为 combined
。
apache_sites:
- hostname: example.com
root: /var/www/html
ssl_certificate: /etc/ssl/example.com/certificate.crt
ssl_certificate_key: /etc/ssl/private/example.com.key
ssl_certificate_chain: /etc/ssl/example.com/fullchain.pem
要使用 HTTPS,请确保已在服务器上安装 SSL 证书。通过 ssl_certificate
属性指定证书位置,通过 ssl_certificate_key
属性指定私钥位置。可以通过设置 ssl_certificate_chain
变量使用可选的完整链证书。
apache_sites:
- hostname: example.com
root: /var/www/html
https_virtualhost_directives: |
SSLProtocol All -SSLv2 -SSLv3 -TLSv1 -TLSv1.1
SSLCipherSuite EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH
SSLHonorCipherOrder on
SSLUseStapling on
SSLCompression off
SSLSessionTickets off
https_headers:
- 'Strict-Transport-Security "max-age=63072000; includeSubDomains; preload"'
可以使用 https_server_directives
和 https_headers
属性添加其他 SSL 指令和头。
apache_sites:
- hostname: example.com
root: /var/www/html
server_directives: |
SSLStaplingCache shmcb:logs/stapling-cache(150000)
可以使用 server_directives
属性将其他服务器指令添加到虚拟主机文件中。
apache_sites:
- hostname: example.com
root: /var/www/html
ip_address: '*'
http_port: '80'
https_port: '443'
上面显示了用于配置站点的默认值。将这些属性添加到站点可覆盖其值。
依赖关系
无。
示例 Playbook
- hosts: server
become: yes
tasks:
- import_role:
name: damianlewis.apache