Ansible Role apache_httpd
This role installs and configures a CIS-compliant Apache httpd.
What this Role does
This role configures Apache in the same way as is usual on Debian systems, so quite different to upstream’s suggested way to configure the web server. This is because this role attempts to make adding and removing mods, virtual hosts, and extra configuration directives as flexible as possible, in order to make automating the changes and administering the server as easy as possible.
The config is split into several files forming the configuration hierarchy outlined below, all located in the /etc/httpd/
directory:
/etc/httpd/
`-- httpd.conf
`-- conf-available/
`-- conf-enabled/
`-- mods-available/
`-- mods-enabled/
`-- sites-available/
`-- sites-enabled/
We try to avoid using <IfModule>
in the global Apache configuration as well as in the configuration of the vhosts as much as possible in order to facilitate debugging. Otherwise, when using <IfModule>
, configuration options are silently dropped, and their absence is very difficult to notice.
For flexibility, use the raw
variable to configure the following topics (have a look at the „Apache vHost Configs“ section for some examples):
SSL/TLS Certificates.
Quality of Service (
mod_qos
directives).Proxy passing rules.
Any other configuration instructions not covered in the „Role Variables“ chapters.
If you want to check Apache with our STIG audit script, run it like this:
Apache Application Server:
./audit.py --lengthy --profile-name='CIS Apache HTTP Server 2.4' --profile-version='v2.0.0' --hostname=web --control-name-exclude='2\.4|2\.6|2\.8|5\.7|6\.6|6\.7
Apache Reverse Proxy Server:
./audit.py --lengthy --profile-name='CIS Apache HTTP Server 2.4' --profile-version='v2.0.0' --hostname=proxy --control-name-exclude='2\.4|2\.6|2\.8|5\.7
What this Role doesn’t do
PHP: This role prefers the use of PHP-FPM over PHP, but it does not install either.
SELinux: Use specialized roles to set specific SELinux Booleans, Policies etc.
Config Examples for vHosts
Mandatory Requirements
On RHEL-compatible systems, enable the EPEL repository. This can be done using the linuxfabrik.lfops.repo_epel role.
Install the
python3-passlib
library. This can be done using the linuxfabrik.lfops.python role.
Optional Requirements
Install PHP and configure PHP-FPM. This can be done using the linuxfabrik.lfops.php role.
Mandatory Role Variables - Global Apache Config (core)
Variable |
Description |
---|---|
|
Mandatory, string. Apache Directive |
Example:
# mandatory
apache_httpd__conf_server_admin: 'webmaster@example.com'
Optional Role Variables - Global Apache Config (core)
Variable |
Description |
Default Value |
---|---|---|
|
String. Apache Directive |
|
|
String. Apache Directive |
|
|
String. Apache Directive |
|
|
String. Apache Directive |
|
|
String. Apache Directive |
|
|
String. Apache Directive |
|
|
Number. CIS: Do not set it above |
|
|
Number. CIS: Do not set it above |
|
|
Number. CIS: Do not set it above |
|
|
Number. CIS: Do not set it above |
|
|
Number. CIS: Do not set it above |
|
|
String. Apache Directive |
|
|
Number. Apache Directive |
|
|
String. Mandatory, string. Apache Directive |
|
|
Number. CIS: Do not set it above |
|
|
String. CIS: Do not set it to |
|
Example:
# optional - core
apache_httpd__conf_add_default_charset: 'UTF-8'
apache_httpd__conf_document_root: '/var/www/html'
apache_httpd__conf_enable_send_file: 'On'
apache_httpd__conf_error_log: 'syslog:local1'
apache_httpd__conf_hostname_lookups: 'Off'
apache_httpd__conf_keep_alive: 'On'
apache_httpd__conf_keep_alive_timeout: 5
apache_httpd__conf_limit_request_body: 102400
apache_httpd__conf_limit_request_field_size: 8190
apache_httpd__conf_limit_request_fields: 100
apache_httpd__conf_limit_request_line: 8190
apache_httpd__conf_log_level: 'warn'
apache_httpd__conf_max_keep_alive_requests: 500
apache_httpd__conf_server_name: 'localhost'
apache_httpd__conf_timeout: 10
apache_httpd__conf_trace_enable: 'Off'
Optional Role Variables - Specific to this role
Variable |
Description |
Default Value |
---|---|---|
|
List. List of dictionaries of |
|
|
List of dictionaries containing used to create and update the flat-files used to store usernames and password for basic authentication of HTTP users. Subkeys:
|
|
|
List. Checks if the |
unset |
|
List. List of dictionaries of |
|
|
List. List of dictionaries of packages to install, related to Apache, using the OS package manager. Possible options: |
|
|
Boolean. Set to true to skip the |
|
|
Boolean. Skip PHP configuration globally and in each vHost within Apache. |
|
|
Boolean. Whether the Apache webserver service should start on boot ( |
|
|
String. Make sure Apache webserver service is in a specific state. Possible options: |
|
Example:
# optional - role-specific
apache_httpd__conf__host_var:
- filename: 'deflate'
enabled: true
state: 'present'
template: 'deflate'
apache_httpd__htpasswd__host_var:
- username: 'test-user'
password: 'linuxfabrik'
state: 'present'
apache_httpd__limit_vhosts:
- 'test.example.com'
apache_httpd__mods__host_var:
- filename: 'alias'
enabled: true
state: 'present'
template: 'alias'
- filename: 'authn_core'
enabled: false # overwrite the default
state: 'absent' # overwrite the default
template: 'authn_core'
apache_httpd__packages__host_var:
- name: 'mod_qos'
state: 'present'
apache_httpd__skip_document_root_chown: true
apache_httpd__skip_php_fpm: false
apache_httpd__systemd_enabled: true
apache_httpd__systemd_state: 'started'
Mandatory Role Variables - vHosts
apache_httpd__vhosts__group_var
/ apache_httpd__vhosts__host_var
:
Variable |
Description |
---|---|
|
Mandatory, string. Set this variable for each vHost definition. Although this is just best practise, we would never use a vHost without a ServerName. |
Example:
# mandatory
apache_httpd__vhosts__host_var:
# Application vHosts
- template: 'app'
conf_server_name: 'myapp.example.com'
Optional Role Variables - vHosts
Using apache_httpd__vhosts__group_var
or apache_httpd__vhosts__host_var
(which are dictionaries), you define vHosts for Apache. The example below shows a complete example, use this as a starting point.
Types of vHosts:
app
A hardened vHost running an application like Nextcloud, Wordpress etc. with the most common options. Can be extended by using theraw
variable.localhost
A hardened, pre-defined VirtualHost just listening on https://localhost, and only accessible from localhost. Due to its naming, it is the first defined vHost. Useful for
Apache status info etc. Can be extended by using theraw
variable. The following URLs are pre-configured, accessible just from localhost:/fpm-ping
,/fpm-status
,/monitoring.php
,/server-info
,/server-status
.proxy
A typical hardened reverse proxy vHost. Can be extended by using theraw
variable. This proxy vHost definition prevents Apache from functioning as a forward proxy
server (inside > out).redirect
A vHost that redirects from one port (default „80“) to another (default „443“). Custom redirect rules can be provided using theraw
variable.raw
If none of the above vHost templates fit, use theraw
one and define everything except<VirtualHost>
and</VirtualHost>
completely from scratch.wordpress
A special vHost just for deploying WordPress instances.
„Hardened“ means among other things:
Old HTTP protocol (< HTTP/1.1) versions are disallowed.
IP address based requests are disallowed.
Number of bytes that are allowed in a request are limited.
etc.
This role creates a vHost named localhost
by default. Have a look at the defaults/main.yml |
Variable |
Description |
Default Value |
---|---|---|
|
Boolean. app-vHosts block access to files that begin with a period. With this setting you can disable this behavior. |
|
|
Boolean. app-vHosts forbid accessing them without a hostname / just by IP. With this setting you can disable this behavior. |
|
|
List. app- and localhost-vHosts block ALL file extensions by default (including |
* app: |
|
List. Should be used to disable unwanted HTTP methods. Only the explicity listed ones are allowed. Use |
* app: |
|
String. Authorization statement for the |
* app: |
|
String. If defined it results in a comment |
* app: unset |
|
String. Describes the vHost and results in a comment right above the |
* app: |
|
String. Will be set in the |
* app: |
|
String. Apache Directive. The log format has to be of |
* app: |
|
String. Apache Directive |
* app: |
|
String. Apache Directive |
* app: |
|
String. Apache Directive |
* app: |
|
Number. CIS: Do not set it above ‚15‘ seconds. |
* app: |
|
String. Apache Directive |
* app: |
|
String. Sets the |
* app: |
|
String. If you want to have a common look and feel on the error pages seen by the end user, set this to „On“ and define them on the reverse proxy server. |
* proxy: |
|
String. When enabled, this option will pass the |
|
|
Number. Apache Directive |
|
|
Number. CIS: |
* app: |
|
String. Apache Directive |
* app: |
|
List. Set this only if you need more than one |
* app: unset |
|
String. Apache Directive |
* app: unset |
|
Number. Apache Directive |
* app: |
|
Boolean. Enable this vHost. |
|
|
String. The filename of the vHost definition. If not set it defaults to the |
conf_server_name.virtualhost_port.conf |
|
String. Set the handler for PHP |
* app: |
|
String. It is sometimes desirable to pass variable content that Jinja would handle as variables or blocks. Jinja’s |
* app: unset |
|
Boolean. Skips checking file extensions in app- and localhost-vHosts, allowing essentially all file extensions. |
|
|
Boolean. Skips checking the HTTP methods in app-, localhost-, proxy-, wordpress-vHosts, allowing essentially all HTTP methods. |
|
|
String. Should the vhost definition file be created ( |
* app: unset |
|
String. Have a look at the intro of this paragraph. |
unset |
|
String. Used within the |
* app: |
|
Number. Used within the |
* app: |
Example: Have a look here.
Optional Role Variables - mod_dir
Variable |
Description |
Default Value |
---|---|---|
|
String. Apache Directive |
|
Example:
# optional - mod_dir
apache_httpd__mod_dir_directory_index: 'index.html'
Optional Role Variables - mod_log_config
This module is for flexible logging of client requests. Logs are written in a customizable format, and may be written directly to a file, or to an external program. Conditional logging is provided so that individual requests may be included or excluded from the logs based on characteristics of the request.
Variable |
Description |
Default Value |
---|---|---|
|
String. One of |
unset |
Example:
# optional - mod_log_config
apache_httpd__mod_log_config_custom_log: 'logs/access.log combined'
Optional Role Variables - mod_security (security2)
Variable |
Description |
Default Value |
---|---|---|
|
String. The OWASP ModSecurity Core Rule Set (CRS) Download URL. Change this if you are running your own mirror servers. |
|
|
String. The OWASP ModSecurity Core Rule Set (CRS) version number without „v“. |
|
|
Boolean. Skip the installation of the OWASP ModSecurity Core Rule Set (CRS). |
|
Example:
# optional - mod_security
apache_httpd__mod_security_coreruleset_url: 'https://github.com/coreruleset/coreruleset/archive'
apache_httpd__mod_security_coreruleset_version: '4.4.0'
apache_httpd__skip_mod_security_coreruleset: true
Optional Role Variables - mod_ssl
Variable |
Description |
Default Value |
---|---|---|
|
String. Apache Directive |
|
Example:
# optional - mod_ssl
apache_httpd__mod_ssl_ssl_use_stapling: 'on'
Optional Role Variables - mpm_common
Variable |
Description |
Default Value |
---|---|---|
|
List of numbers or strings. Apache Directive |
|
Example:
# optional - mpm_common
apache_httpd__mpm_common_listen:
- 80
- '192.0.2.10:80'
Optional Role Variables - mpm_event_module
TLDR: event MPM: A variant of the worker MPM with the goal of consuming threads only for connections with active processing. See: http://httpd.apache.org/docs/2.4/mod/event.html
Event: Based on worker, this MPM goes one step further by optimizing how the parent process schedules tasks to the child processes and the threads associated to those. A connection stays open for 5 seconds by default and closes if no new event happens; this is the keep-alive directive default value, which retains the thread associated to it. The Event MPM enables the process to manage threads so that some threads are free to handle new incoming connections while others are kept bound to the live connections. Allowing re-distribution of assigned tasks to threads will make for better resource utilization and performance.
Best for PHP-FPM. Default.
Variable |
Description |
Default Value |
---|---|---|
|
Number. Apache Directive |
|
|
Number. Apache Directive |
|
|
Number. Apache Directive |
|
|
Number. Apache Directive |
|
|
Number. Apache Directive |
|
|
Number. Apache Directive |
|
|
Number. Apache Directive |
|
Example:
# optional - mpm_event_module
apache_httpd__mpm_event_max_connections_per_child: 0
apache_httpd__mpm_event_max_request_workers: 400
apache_httpd__mpm_event_max_spare_threads: 250
apache_httpd__mpm_event_min_spare_threads: 75
apache_httpd__mpm_event_start_servers: 3
apache_httpd__mpm_event_thread_limit: 64
apache_httpd__mpm_event_threads_per_child: 25
Optional Role Variables - mpm_prefork_module
TLDR: prefork MPM: Implements a non-threaded, pre-forking web server. See: http://httpd.apache.org/docs/2.4/mod/prefork.html
Pre-fork: A new process is created for each incoming connection reaching the server. Each process is isolated from the others, so no memory is shared between them, even if they are performing identical calls at some point in their execution. This is a safe way to run applications linked to libraries that do not support threading—typically older applications or libraries.
NOTE: If enabling prefork, the httpd_graceful_shutdown SELinux boolean should be enabled, to allow graceful stop/shutdown.
This MPM is very self-regulating, so it is rarely necessary to adjust its configuration directives. Most important is that apache_httpd__mpm_prefork_max_request_workers
be big enough to handle as many simultaneous requests as you expect to receive, but small enough to assure that there is enough physical RAM for all processes.
Best for Standard PHP running any version of mod_php
. Does not work with http2.
Variable |
Description |
Default Value |
---|---|---|
|
Number. Apache Directive |
|
|
Number. Apache Directive |
|
|
Number. Apache Directive |
|
|
Number. Apache Directive |
|
|
Number. Apache Directive |
|
Example:
# optional - mpm_prefork_module
apache_httpd__mpm_prefork_max_connections_per_child: 0
apache_httpd__mpm_prefork_max_request_workers: 256
apache_httpd__mpm_prefork_max_spare_servers: 10
apache_httpd__mpm_prefork_min_spare_servers: 5
apache_httpd__mpm_prefork_start_servers: 5
Optional Role Variables - mpm_worker_module
TLDR: worker MPM: Multi-Processing Module implementing a hybrid multi-threaded multi-process web server. See: http://httpd.apache.org/docs/2.4/mod/worker.html
Worker: A parent process is responsible for launching a pool of child processes, some of which are listening for new incoming connections, and others are serving the requested content. Each process is threaded (a single thread can handle one connection) so one process can handle several requests concurrently. This method of treating connections encourages better resource utilization, while still maintaining stability. This is a result of the pool of available processes, which often has free available threads ready to immediately serve new connections.
The most important directives used to control this MPM are apache_httpd__mpm_worker_threads_per_child
, which controls the number of threads deployed by each child process and apache_httpd__mpm_worker_max_request_workers
, which controls the maximum total number of threads that may be launched.
Best for mod_qos if you intend to use any connection level control directive („QS_Srv*“), which is normally done on a Reverse Proxy. Works with PHP-FPM, too.
Variable |
Description |
Default Value |
---|---|---|
|
Number. Apache Directive |
|
|
Number. Apache Directive |
|
|
Number. Apache Directive |
|
|
Number. Apache Directive |
|
|
Number. Apache Directive |
|
|
Number. Apache Directive |
|
|
Number. Apache Directive |
|
Example:
# optional - mpm_worker_module
apache_httpd__mpm_worker_max_connections_per_child: 0
apache_httpd__mpm_worker_max_request_workers: 400
apache_httpd__mpm_worker_max_spare_threads: 250
apache_httpd__mpm_worker_min_spare_threads: 75
apache_httpd__mpm_worker_start_servers: 3
apache_httpd__mpm_worker_thread_limit: 64
apache_httpd__mpm_worker_threads_per_child: 25
Optional Role Variables - wsgi_python3_module
Variable |
Description |
Default Value |
---|---|---|
|
String. Apache Directive |
|
|
String. Apache Directive |
|
|
String. Apache Directive |
|