Chapter 1. Setting up the Apache HTTP web server
1.1. Introduction to the Apache HTTP web server Copy linkLink copied to clipboard!
A web server is a network service that serves content to a client over the web. This typically means web pages, but any other documents can be served as well. Web servers are also known as HTTP servers, as they use the hypertext transport protocol (HTTP).
The Apache HTTP Server, httpd, is an open source web server developed by the Apache Software Foundation.
If you are upgrading from a previous release of Red Hat Enterprise Linux, you have to update the httpd service configuration accordingly. This section reviews some of the newly added features, and guides you through the update of prior configuration files.
1.2. Notable changes in the Apache HTTP Server Copy linkLink copied to clipboard!
The Apache HTTP Server has been updated from version 2.4.6 in RHEL 7 to version 2.4.37 in RHEL 8. This updated version includes several new features, but maintains backwards compatibility with the RHEL 7 version at the level of configuration and Application Binary Interface (ABI) of external modules.
New features include:
-
HTTP/2support is now provided by themod_http2package, which is a part of thehttpdmodule. -
systemd socket activation is supported. See
httpd.socket(8)man page for more details.
Multiple new modules have been added:
-
mod_proxy_hcheck- a proxy health-check module -
mod_proxy_uwsgi- a Web Server Gateway Interface (WSGI) proxy -
mod_proxy_fdpass- provides support for the passing the socket of the client to another process -
mod_cache_socache- an HTTP cache using, for example, memcache backend -
mod_md- an ACME protocol SSL/TLS certificate service
-
The following modules now load by default:
-
mod_request -
mod_macro -
mod_watchdog
-
-
A new subpackage,
httpd-filesystem, has been added, which contains the basic directory layout for the Apache HTTP Server including the correct permissions for the directories. -
Instantiated service support,
httpd@.servicehas been introduced. See thehttpd.serviceman page for more information.
-
A new
httpd-init.servicereplaces the%post scriptto create a self-signedmod_sslkey pair.
-
Automated TLS certificate provisioning and renewal using the Automatic Certificate Management Environment (ACME) protocol is now supported with the
mod_mdpackage (for use with certificate providers such asLet’s Encrypt). -
The Apache HTTP Server now supports loading TLS certificates and private keys from hardware security tokens directly from
PKCS#11modules. As a result, amod_sslconfiguration can now usePKCS#11URLs to identify the TLS private key, and, optionally, the TLS certificate in theSSLCertificateKeyFileandSSLCertificateFiledirectives. A new
ListenFreedirective in the/etc/httpd/conf/httpd.conffile is now supported.Similarly to the
Listendirective,ListenFreeprovides information about IP addresses, ports, or IP address-and-port combinations that the server listens to. However, withListenFree, theIP_FREEBINDsocket option is enabled by default. Hence,httpdis allowed to bind to a nonlocal IP address or to an IP address that does not exist yet. This allowshttpdto listen on a socket without requiring the underlying network interface or the specified dynamic IP address to be up at the time whenhttpdis trying to bind to it.Note that the
ListenFreedirective is currently available only in RHEL 8.For more details on
ListenFree, see the following table:Expand Table 1.1. ListenFree directive’s syntax, status, and modules Syntax Status Modules ListenFree [IP-address:]portnumber [protocol]
MPM
event, worker, prefork, mpm_winnt, mpm_netware, mpmt_os2
Other notable changes include:
The following modules have been removed:
-
mod_file_cache mod_nssUse
mod_sslas a replacement. For details about migrating frommod_nss, see Section 1.14, “Exporting a private key and certificates from an NSS database to use them in an Apache web server configuration”.-
mod_perl
-
-
The default type of the DBM authentication database used by the Apache HTTP Server in RHEL 8 has been changed from
SDBMtodb5. -
The
mod_wsgimodule for the Apache HTTP Server has been updated to Python 3. WSGI applications are now supported only with Python 3, and must be migrated from Python 2. The multi-processing module (MPM) configured by default with the Apache HTTP Server has changed from a multi-process, forked model (known as
prefork) to a high-performance multi-threaded model,event.Any third-party modules that are not thread-safe need to be replaced or removed. To change the configured MPM, edit the
/etc/httpd/conf.modules.d/00-mpm.conffile. See thehttpd.service(8)man page for more information.- The minimum UID and GID allowed for users by suEXEC are now 1000 and 500, respectively (previously 100 and 100).
-
The
/etc/sysconfig/httpdfile is no longer a supported interface for setting environment variables for thehttpdservice. Thehttpd.service(8)man page has been added for the systemd service. -
Stopping the
httpdservice now uses a “graceful stop” by default. -
The
mod_auth_kerbmodule has been replaced by themod_auth_gssapimodule.
1.3. Updating the configuration Copy linkLink copied to clipboard!
To update the configuration files from the Apache HTTP Server version used in Red Hat Enterprise Linux 7, choose one of the following options:
-
If
/etc/sysconfig/httpdis used to set environment variables, create a systemd drop-in file instead. - If any third-party modules are used, ensure they are compatible with a threaded MPM.
- If suexec is used, ensure user and group IDs meet the new minimums.
You can check the configuration for possible errors by using the following command:
apachectl configtest
# apachectl configtest
Syntax OK
1.4. The Apache configuration files Copy linkLink copied to clipboard!
The httpd, by default, reads the configuration files after start. You can see the list of the locations of configuration files in the table below.
| Path | Description |
|---|---|
|
| The main configuration file. |
|
| An auxiliary directory for configuration files that are included in the main configuration file. |
|
| An auxiliary directory for configuration files which load installed dynamic modules packaged in Red Hat Enterprise Linux. In the default configuration, these configuration files are processed first. |
Although the default configuration is suitable for most situations, you can use also other configuration options. For any changes to take effect, restart the web server first.
To check the configuration for possible errors, type the following at a shell prompt:
apachectl configtest
# apachectl configtest
Syntax OK
To make the recovery from mistakes easier, make a copy of the original file before editing it.
1.5. Managing the httpd service Copy linkLink copied to clipboard!
This section describes how to start, stop, and restart the httpd service.
Prerequisites
- The Apache HTTP Server is installed.
Procedure
To start the
httpdservice, enter:systemctl start httpd
# systemctl start httpdCopy to Clipboard Copied! Toggle word wrap Toggle overflow To stop the
httpdservice, enter:systemctl stop httpd
# systemctl stop httpdCopy to Clipboard Copied! Toggle word wrap Toggle overflow To restart the
httpdservice, enter:systemctl restart httpd
# systemctl restart httpdCopy to Clipboard Copied! Toggle word wrap Toggle overflow
1.6. Setting up a single-instance Apache HTTP Server Copy linkLink copied to clipboard!
You can set up a single-instance Apache HTTP Server to serve static HTML content.
Follow the procedure if the web server should provide the same content for all domains associated with the server. If you want to provide different content for different domains, set up name-based virtual hosts. For details, see Configuring Apache name-based virtual hosts.
Procedure
Install the
httpdpackage:yum install httpd
# yum install httpdCopy to Clipboard Copied! Toggle word wrap Toggle overflow If you use
firewalld, open the TCP port80in the local firewall:firewall-cmd --permanent --add-port=80/tcp firewall-cmd --reload
# firewall-cmd --permanent --add-port=80/tcp # firewall-cmd --reloadCopy to Clipboard Copied! Toggle word wrap Toggle overflow Enable and start the
httpdservice:systemctl enable --now httpd
# systemctl enable --now httpdCopy to Clipboard Copied! Toggle word wrap Toggle overflow Optional: Add HTML files to the
/var/www/html/directory.NoteWhen adding content to
/var/www/html/, files and directories must be readable by the user under whichhttpdruns by default. The content owner can be either therootuser androotuser group, or another user or group of the administrator’s choice. If the content owner is therootuser androotuser group, the files must be readable by other users. The SELinux context for all the files and directories must behttpd_sys_content_t, which is applied by default to all content within the/var/wwwdirectory.
Verification
Connect with a web browser to
http://server_IP_or_host_name/.If the
/var/www/html/directory is empty or does not contain anindex.htmlorindex.htmfile, Apache displays theRed Hat Enterprise Linux Test Page. If/var/www/html/contains HTML files with a different name, you can load them by entering the URL to that file, such ashttp://server_IP_or_host_name/example.html.
1.7. Configuring Apache name-based virtual hosts Copy linkLink copied to clipboard!
Name-based virtual hosts enable Apache to serve different content for different domains that resolve to the IP address of the server.
You can set up a virtual host for both the example.com and example.net domain with separate document root directories. Both virtual hosts serve static HTML content.
Prerequisites
Clients and the web server resolve the
example.comandexample.netdomain to the IP address of the web server.Note that you must manually add these entries to your DNS server.
Procedure
Install the
httpdpackage:yum install httpd
# yum install httpdCopy to Clipboard Copied! Toggle word wrap Toggle overflow Edit the
/etc/httpd/conf/httpd.conffile:Append the following virtual host configuration for the
example.comdomain:Copy to Clipboard Copied! Toggle word wrap Toggle overflow These settings configure the following:
-
All settings in the
<VirtualHost *:80>directive are specific for this virtual host. -
DocumentRootsets the path to the web content of the virtual host. ServerNamesets the domains for which this virtual host serves content.To set multiple domains, add the
ServerAliasparameter to the configuration and specify the additional domains separated with a space in this parameter.-
CustomLogsets the path to the access log of the virtual host. ErrorLogsets the path to the error log of the virtual host.NoteApache uses the first virtual host found in the configuration also for requests that do not match any domain set in the
ServerNameandServerAliasparameters. This also includes requests sent to the IP address of the server.
-
All settings in the
Append a similar virtual host configuration for the
example.netdomain:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Create the document roots for both virtual hosts:
mkdir /var/www/example.com/ mkdir /var/www/example.net/
# mkdir /var/www/example.com/ # mkdir /var/www/example.net/Copy to Clipboard Copied! Toggle word wrap Toggle overflow If you set paths in the
DocumentRootparameters that are not within/var/www/, set thehttpd_sys_content_tcontext on both document roots:semanage fcontext -a -t httpd_sys_content_t "/srv/example.com(/.*)?" restorecon -Rv /srv/example.com/ semanage fcontext -a -t httpd_sys_content_t "/srv/example.net(/.\*)?" restorecon -Rv /srv/example.net/
# semanage fcontext -a -t httpd_sys_content_t "/srv/example.com(/.*)?" # restorecon -Rv /srv/example.com/ # semanage fcontext -a -t httpd_sys_content_t "/srv/example.net(/.\*)?" # restorecon -Rv /srv/example.net/Copy to Clipboard Copied! Toggle word wrap Toggle overflow These commands set the
httpd_sys_content_tcontext on the/srv/example.com/and/srv/example.net/directory.Note that you must install the
policycoreutils-python-utilspackage to run therestoreconcommand.If you use
firewalld, open port80in the local firewall:firewall-cmd --permanent --add-port=80/tcp firewall-cmd --reload
# firewall-cmd --permanent --add-port=80/tcp # firewall-cmd --reloadCopy to Clipboard Copied! Toggle word wrap Toggle overflow Enable and start the
httpdservice:systemctl enable --now httpd
# systemctl enable --now httpdCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
Create a different example file in each virtual host’s document root:
echo "vHost example.com" > /var/www/example.com/index.html echo "vHost example.net" > /var/www/example.net/index.html
# echo "vHost example.com" > /var/www/example.com/index.html # echo "vHost example.net" > /var/www/example.net/index.htmlCopy to Clipboard Copied! Toggle word wrap Toggle overflow -
Use a browser and connect to
http://example.com. The web server shows the example file from theexample.comvirtual host. -
Use a browser and connect to
http://example.net. The web server shows the example file from theexample.netvirtual host.
1.8. Configuring Kerberos authentication for the Apache HTTP web server Copy linkLink copied to clipboard!
To perform Kerberos authentication in the Apache HTTP web server, RHEL 8 uses the mod_auth_gssapi Apache module. The Generic Security Services API (GSSAPI) is an interface for applications that make requests to use security libraries, such as Kerberos. The gssproxy service allows to implement privilege separation for the httpd server, which optimizes this process from the security point of view.
The mod_auth_gssapi module replaces the removed mod_auth_kerb module.
Prerequisites
-
The
httpdandgssproxypackages are installed. -
The Apache web server is set up and the
httpdservice is running.
1.8.1. Setting up GSS-Proxy in an IdM environment Copy linkLink copied to clipboard!
This procedure describes how to set up GSS-Proxy to perform Kerberos authentication in the Apache HTTP web server.
Procedure
Enable access to the
keytabfile of HTTP/<SERVER_NAME>@realm principal by creating the service principal:ipa service-add HTTP/<SERVER_NAME>
# ipa service-add HTTP/<SERVER_NAME>Copy to Clipboard Copied! Toggle word wrap Toggle overflow Retrieve the
keytabfor the principal stored in the/etc/gssproxy/http.keytabfile:ipa-getkeytab -s $(awk '/^server =/ {print $3}' /etc/ipa/default.conf) -k /etc/gssproxy/http.keytab -p HTTP/$(hostname -f)# ipa-getkeytab -s $(awk '/^server =/ {print $3}' /etc/ipa/default.conf) -k /etc/gssproxy/http.keytab -p HTTP/$(hostname -f)Copy to Clipboard Copied! Toggle word wrap Toggle overflow This step sets permissions to 400, thus only the
rootuser has access to thekeytabfile. Theapacheuser does not.Create the
/etc/gssproxy/80-httpd.conffile with the following content:[service/HTTP] mechs = krb5 cred_store = keytab:/etc/gssproxy/http.keytab cred_store = ccache:/var/lib/gssproxy/clients/krb5cc_%U euid = apache
[service/HTTP] mechs = krb5 cred_store = keytab:/etc/gssproxy/http.keytab cred_store = ccache:/var/lib/gssproxy/clients/krb5cc_%U euid = apacheCopy to Clipboard Copied! Toggle word wrap Toggle overflow Restart and enable the
gssproxyservice:systemctl restart gssproxy.service systemctl enable gssproxy.service
# systemctl restart gssproxy.service # systemctl enable gssproxy.serviceCopy to Clipboard Copied! Toggle word wrap Toggle overflow
1.9. Configuring TLS encryption on an Apache HTTP Server Copy linkLink copied to clipboard!
By default, Apache provides content to clients using an unencrypted HTTP connection. This section describes how to enable TLS encryption and configure frequently used encryption-related settings on an Apache HTTP Server.
Prerequisites
- The Apache HTTP Server is installed and running.
1.9.1. Adding TLS encryption to an Apache HTTP Server Copy linkLink copied to clipboard!
You can enable TLS encryption on an Apache HTTP Server for the example.com domain.
Prerequisites
- The Apache HTTP Server is installed and running.
The private key is stored in the
/etc/pki/tls/private/example.com.keyfile.For details about creating a private key and certificate signing request (CSR), as well as how to request a certificate from a certificate authority (CA), see your CA’s documentation. Alternatively, if your CA supports the ACME protocol, you can use the
mod_mdmodule to automate retrieving and provisioning TLS certificates.-
The TLS certificate is stored in the
/etc/pki/tls/certs/example.com.crtfile. If you use a different path, adapt the corresponding steps of the procedure. -
The CA certificate is stored in the
/etc/pki/tls/certs/ca.crtfile. If you use a different path, adapt the corresponding steps of the procedure. - Clients and the web server resolve the host name of the server to the IP address of the web server.
Procedure
Install the
mod_sslpackage:yum install mod_ssl
# yum install mod_sslCopy to Clipboard Copied! Toggle word wrap Toggle overflow Edit the
/etc/httpd/conf.d/ssl.conffile and add the following settings to the<VirtualHost _default_:443>directive:Set the server name:
ServerName example.com
ServerName example.comCopy to Clipboard Copied! Toggle word wrap Toggle overflow
The server name must match the entry set in the Common Name field of the certificate.
Optional: If the certificate contains additional host names in the
Subject Alt Names(SAN) field, you can configuremod_sslto provide TLS encryption also for these host names. To configure this, add theServerAliasesparameter with corresponding names:ServerAlias www.example.com server.example.com
ServerAlias www.example.com server.example.comCopy to Clipboard Copied! Toggle word wrap Toggle overflow Set the paths to the private key, the server certificate, and the CA certificate:
SSLCertificateKeyFile "/etc/pki/tls/private/example.com.key" SSLCertificateFile "/etc/pki/tls/certs/example.com.crt" SSLCACertificateFile "/etc/pki/tls/certs/ca.crt"
SSLCertificateKeyFile "/etc/pki/tls/private/example.com.key" SSLCertificateFile "/etc/pki/tls/certs/example.com.crt" SSLCACertificateFile "/etc/pki/tls/certs/ca.crt"Copy to Clipboard Copied! Toggle word wrap Toggle overflow For security reasons, configure that only the
rootuser can access the private key file:chown root:root /etc/pki/tls/private/example.com.key chmod 600 /etc/pki/tls/private/example.com.key
# chown root:root /etc/pki/tls/private/example.com.key # chmod 600 /etc/pki/tls/private/example.com.keyCopy to Clipboard Copied! Toggle word wrap Toggle overflow WarningIf the private key was accessed by unauthorized users, revoke the certificate, create a new private key, and request a new certificate. Otherwise, the TLS connection is no longer secure.
If you use
firewalld, open port443in the local firewall:firewall-cmd --permanent --add-port=443/tcp firewall-cmd --reload
# firewall-cmd --permanent --add-port=443/tcp # firewall-cmd --reloadCopy to Clipboard Copied! Toggle word wrap Toggle overflow Restart the
httpdservice:systemctl restart httpd
# systemctl restart httpdCopy to Clipboard Copied! Toggle word wrap Toggle overflow
NoteIf you protected the private key file with a password, you must enter this password each time when the
httpdservice starts.
Verification
-
Use a browser and connect to
https://example.com.
1.9.2. Setting the supported TLS protocol versions on an Apache HTTP Server Copy linkLink copied to clipboard!
By default, the Apache HTTP Server on RHEL uses the system-wide crypto policy that defines safe default values, which are also compatible with recent browsers. For example, the DEFAULT policy defines that only the TLSv1.2 and TLSv1.3 protocol versions are enabled in apache.
You can manually configure which TLS protocol versions your Apache HTTP Server supports. Follow the procedure if your environment requires to enable only specific TLS protocol versions, for example:
-
If your environment requires that clients can also use the weak
TLS1(TLSv1.0) orTLS1.1protocol. -
If you want to configure that Apache only supports the
TLSv1.2orTLSv1.3protocol.
Prerequisites
- TLS encryption is enabled on the server as described in Adding TLS encryption to an Apache HTTP server.
Procedure
Edit the
/etc/httpd/conf/httpd.conffile, and add the following setting to the<VirtualHost>directive for which you want to set the TLS protocol version. For example, to enable only theTLSv1.3protocol:SSLProtocol -All TLSv1.3
SSLProtocol -All TLSv1.3Copy to Clipboard Copied! Toggle word wrap Toggle overflow Restart the
httpdservice:systemctl restart httpd
# systemctl restart httpdCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
Use the following command to verify that the server supports
TLSv1.3:openssl s_client -connect example.com:443 -tls1_3
# openssl s_client -connect example.com:443 -tls1_3Copy to Clipboard Copied! Toggle word wrap Toggle overflow Use the following command to verify that the server does not support
TLSv1.2:openssl s_client -connect example.com:443 -tls1_2
# openssl s_client -connect example.com:443 -tls1_2Copy to Clipboard Copied! Toggle word wrap Toggle overflow If the server does not support the protocol, the command returns an error:
140111600609088:error:1409442E:SSL routines:ssl3_read_bytes:tlsv1 alert protocol version:ssl/record/rec_layer_s3.c:1543:SSL alert number 70
140111600609088:error:1409442E:SSL routines:ssl3_read_bytes:tlsv1 alert protocol version:ssl/record/rec_layer_s3.c:1543:SSL alert number 70Copy to Clipboard Copied! Toggle word wrap Toggle overflow - Optional: Repeat the command for other TLS protocol versions.
1.9.3. Setting the supported ciphers on an Apache HTTP Server Copy linkLink copied to clipboard!
By default, the Apache HTTP Server uses the system-wide crypto policy that defines safe default values, which are also compatible with recent browsers. For the list of ciphers the system-wide crypto allows, see the /etc/crypto-policies/back-ends/openssl.config file.
You can manually configure which ciphers your Apache HTTP Server supports. Follow the procedure if your environment requires specific ciphers.
Prerequisites
- TLS encryption is enabled on the server as described in Adding TLS encryption to an Apache HTTP server.
Procedure
Edit the
/etc/httpd/conf/httpd.conffile, and add theSSLCipherSuiteparameter to the<VirtualHost>directive for which you want to set the TLS ciphers:SSLCipherSuite "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH:!SHA1:!SHA256"
SSLCipherSuite "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH:!SHA1:!SHA256"Copy to Clipboard Copied! Toggle word wrap Toggle overflow This example enables only the
EECDH+AESGCM,EDH+AESGCM,AES256+EECDH, andAES256+EDHciphers and disables all ciphers which use theSHA1andSHA256message authentication code (MAC).Restart the
httpdservice:systemctl restart httpd
# systemctl restart httpdCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
To display the list of ciphers the Apache HTTP Server supports:
Install the
nmappackage:yum install nmap
# yum install nmapCopy to Clipboard Copied! Toggle word wrap Toggle overflow Use the
nmaputility to display the supported ciphers:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
1.10. Configuring TLS client certificate authentication Copy linkLink copied to clipboard!
Client certificate authentication enables administrators to allow only users who authenticate using a certificate to access resources on the web server. You can configure client certificate authentication for the /var/www/html/Example/ directory.
If the Apache HTTP Server uses the TLS 1.3 protocol, certain clients require additional configuration. For example, in Firefox, set the security.tls.enable_post_handshake_auth parameter in the about:config menu to true.
Prerequisites
- TLS encryption is enabled on the server as described in Adding TLS encryption to an Apache HTTP server.
Procedure
Edit the
/etc/httpd/conf/httpd.conffile and add the following settings to the<VirtualHost>directive for which you want to configure client authentication:<Directory "/var/www/html/Example/"> SSLVerifyClient require </Directory>
<Directory "/var/www/html/Example/"> SSLVerifyClient require </Directory>Copy to Clipboard Copied! Toggle word wrap Toggle overflow The
SSLVerifyClient requiresetting defines that the server must successfully validate the client certificate before the client can access the content in the/var/www/html/Example/directory.Restart the
httpdservice:systemctl restart httpd
# systemctl restart httpdCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
Use the
curlutility to access thehttps://example.com/Example/URL without client authentication:curl https://example.com/Example/
$ curl https://example.com/Example/ curl: (56) OpenSSL SSL_read: error:1409445C:SSL routines:ssl3_read_bytes:tlsv13 alert certificate required, errno 0Copy to Clipboard Copied! Toggle word wrap Toggle overflow The error indicates that the web server requires a client certificate authentication.
Pass the client private key and certificate, as well as the CA certificate to
curlto access the same URL with client authentication:curl --cacert ca.crt --key client.key --cert client.crt https://example.com/Example/
$ curl --cacert ca.crt --key client.key --cert client.crt https://example.com/Example/Copy to Clipboard Copied! Toggle word wrap Toggle overflow If the request succeeds,
curldisplays theindex.htmlfile stored in the/var/www/html/Example/directory.
1.11. Securing web applications on a web server using ModSecurity Copy linkLink copied to clipboard!
ModSecurity is an open source web application firewall (WAF) supported by various web servers such as Apache, Nginx, and IIS, which reduces security risks in web applications. ModSecurity provides customizable rule sets for configuring your server.
The mod_security-crs package contains the core rule set (CRS) with rules against cross-website scripting, bad user agents, SQL injection, Trojans, session hijacking, and other exploits.
1.11.1. Deploying the ModSecurity web-based application firewall for Apache Copy linkLink copied to clipboard!
To reduce risks related to running web-based applications on your web server by deploying ModSecurity, install the mod_security and mod_security_crs packages for the Apache HTTP server. The mod_security_crs package provides the core rule set (CRS) for the ModSecurity web-based application firewall (WAF) module.
Procedure
Install the
mod_security,mod_security_crs, andhttpdpackages:yum install -y mod_security mod_security_crs httpd
# yum install -y mod_security mod_security_crs httpdCopy to Clipboard Copied! Toggle word wrap Toggle overflow Start the
httpdserver:systemctl restart httpd
# systemctl restart httpdCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
Verify that the ModSecurity web-based application firewall is enabled on your Apache HTTP server:
httpd -M | grep security
# httpd -M | grep security security2_module (shared)Copy to Clipboard Copied! Toggle word wrap Toggle overflow Check that the
/etc/httpd/modsecurity.d/activated_rules/directory contains rules provided bymod_security_crs:ls /etc/httpd/modsecurity.d/activated_rules/
# ls /etc/httpd/modsecurity.d/activated_rules/ ... REQUEST-921-PROTOCOL-ATTACK.conf REQUEST-930-APPLICATION-ATTACK-LFI.conf ...Copy to Clipboard Copied! Toggle word wrap Toggle overflow
1.11.2. Adding a custom rule to ModSecurity Copy linkLink copied to clipboard!
If the rules contained in the ModSecurity core rule set (CRS) do not fit your scenario and if you want to prevent additional possible attacks, you can add your custom rules to the rule set used by the ModSecurity web-based application firewall. The following example demonstrates the addition of a simple rule. For creating more complex rules, see the reference manual on the ModSecurity Wiki website.
Prerequisites
- ModSecurity for Apache is installed and enabled.
Procedure
Open the
/etc/httpd/conf.d/mod_security.conffile in a text editor of your choice, for example:vi /etc/httpd/conf.d/mod_security.conf
# vi /etc/httpd/conf.d/mod_security.confCopy to Clipboard Copied! Toggle word wrap Toggle overflow Add the following example rule after the line starting with
SecRuleEngine On:SecRule ARGS:data "@contains evil" "deny,status:403,msg:'param data contains evil data',id:1"
SecRule ARGS:data "@contains evil" "deny,status:403,msg:'param data contains evil data',id:1"Copy to Clipboard Copied! Toggle word wrap Toggle overflow The previous rule forbids the use of resources to the user if the
dataparameter contains theevilstring.- Save the changes, and quit the editor.
Restart the
httpdserver:systemctl restart httpd
# systemctl restart httpdCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
Create a
test.htmlpage:echo "mod_security test" > /var/www/html/test.html
# echo "mod_security test" > /var/www/html/test.htmlCopy to Clipboard Copied! Toggle word wrap Toggle overflow Restart the
httpdserver:systemctl restart httpd
# systemctl restart httpdCopy to Clipboard Copied! Toggle word wrap Toggle overflow Request
test.htmlwithout malicious data in theGETvariable of the HTTP request:curl http://localhost/test.html?data=good
$ curl http://localhost/test.html?data=good mod_security testCopy to Clipboard Copied! Toggle word wrap Toggle overflow Request
test.htmlwith malicious data in theGETvariable of the HTTP request:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Check the
/var/log/httpd/error_logfile, and locate the log entry about denying access with theparam data containing an evil datamessage:[Wed May 25 08:01:31.036297 2022] [:error] [pid 5839:tid 139874434791168] [client ::1:45658] [client ::1] ModSecurity: Access denied with code 403 (phase 2). String match "evil" at ARGS:data. [file "/etc/httpd/conf.d/mod_security.conf"] [line "4"] [id "1"] [msg "param data contains evil data"] [hostname "localhost"] [uri "/test.html"] [unique_id "Yo4amwIdsBG3yZqSzh2GuwAAAIY"]
[Wed May 25 08:01:31.036297 2022] [:error] [pid 5839:tid 139874434791168] [client ::1:45658] [client ::1] ModSecurity: Access denied with code 403 (phase 2). String match "evil" at ARGS:data. [file "/etc/httpd/conf.d/mod_security.conf"] [line "4"] [id "1"] [msg "param data contains evil data"] [hostname "localhost"] [uri "/test.html"] [unique_id "Yo4amwIdsBG3yZqSzh2GuwAAAIY"]Copy to Clipboard Copied! Toggle word wrap Toggle overflow
1.12. Installing the Apache HTTP Server manual Copy linkLink copied to clipboard!
You can install the Apache HTTP Server manual. This manual provides a detailed documentation of, for example:
- Configuration parameters and directives
- Performance tuning
- Authentication settings
- Modules
- Content caching
- Security tips
- Configuring TLS encryption
After installing the manual, you can display it using a web browser.
Prerequisites
- The Apache HTTP Server is installed and running.
Procedure
Install the
httpd-manualpackage:yum install httpd-manual
# yum install httpd-manualCopy to Clipboard Copied! Toggle word wrap Toggle overflow Optional: By default, all clients connecting to the Apache HTTP Server can display the manual. To restrict access to a specific IP range, such as the
192.0.2.0/24subnet, edit the/etc/httpd/conf.d/manual.conffile and add theRequire ip 192.0.2.0/24setting to the<Directory "/usr/share/httpd/manual">directive:<Directory "/usr/share/httpd/manual"> ... Require ip 192.0.2.0/24 ... </Directory><Directory "/usr/share/httpd/manual"> ... Require ip 192.0.2.0/24 ... </Directory>Copy to Clipboard Copied! Toggle word wrap Toggle overflow Restart the
httpdservice:systemctl restart httpd
# systemctl restart httpdCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
-
To display the Apache HTTP Server manual, connect with a web browser to
http://host_name_or_IP_address/manual/
1.13. Working with Apache modules Copy linkLink copied to clipboard!
The httpd service is a modular application, and you can extend it with a number of Dynamic Shared Objects (DSOs). Dynamic Shared Objects are modules that you can dynamically load or unload at runtime as necessary. You can find these modules in the /usr/lib64/httpd/modules/ directory.
1.13.1. Loading a DSO module Copy linkLink copied to clipboard!
As an administrator, you can choose the functionality to include in the server by configuring which modules the server should load. To load a particular DSO module, use the LoadModule directive. Note that modules provided by a separate package often have their own configuration file in the /etc/httpd/conf.modules.d/ directory.
Prerequisites
-
You have installed the
httpdpackage.
Procedure
Search for the module name in the configuration files in the
/etc/httpd/conf.modules.d/directory:grep mod_ssl.so /etc/httpd/conf.modules.d/*
# grep mod_ssl.so /etc/httpd/conf.modules.d/*Copy to Clipboard Copied! Toggle word wrap Toggle overflow Edit the configuration file in which the module name was found, and uncomment the
LoadModuledirective of the module:LoadModule ssl_module modules/mod_ssl.so
LoadModule ssl_module modules/mod_ssl.soCopy to Clipboard Copied! Toggle word wrap Toggle overflow If the module was not found, for example, because a RHEL package does not provide the module, create a configuration file, such as
/etc/httpd/conf.modules.d/30-example.confwith the following directive:LoadModule ssl_module modules/<custom_module>.so
LoadModule ssl_module modules/<custom_module>.soCopy to Clipboard Copied! Toggle word wrap Toggle overflow Restart the
httpdservice:systemctl restart httpd
# systemctl restart httpdCopy to Clipboard Copied! Toggle word wrap Toggle overflow
1.13.2. Compiling a custom Apache module Copy linkLink copied to clipboard!
You can create your own module and build it with the help of the httpd-devel package, which contains the include files, the header files, and the APache eXtenSion (apxs) utility required to compile a module.
Prerequisites
-
You have the
httpd-develpackage installed.
Procedure
Build a custom module with the following command:
apxs -i -a -c module_name.c
# apxs -i -a -c module_name.cCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
- Load the module the same way as described in Loading a DSO module.
1.14. Exporting a private key and certificates from an NSS database to use them in an Apache web server configuration Copy linkLink copied to clipboard!
RHEL 8 no longer provides the mod_nss module for the Apache web server, and Red Hat recommends using the mod_ssl module. If you store your private key and certificates in a Network Security Services (NSS) database, for example, because you migrated the web server from RHEL 7 to RHEL 8, follow this procedure to extract the key and certificates in Privacy Enhanced Mail (PEM) format. You can then use the files in the mod_ssl configuration as described in Configuring TLS encryption on an Apache HTTP server.
This procedure assumes that the NSS database is stored in /etc/httpd/alias/ and that you store the exported private key and certificates in the /etc/pki/tls/ directory.
Prerequisites
- The private key, the certificate, and the certificate authority (CA) certificate are stored in an NSS database.
Procedure
List the certificates in the NSS database:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow You need the nicknames of the certificates in the next steps.
To extract the private key, you must temporarily export the key to a PKCS #12 file:
Use the nickname of the certificate associated with the private key, to export the key to a PKCS #12 file:
pk12util -o /etc/pki/tls/private/export.p12 -d /etc/httpd/alias/ -n "Example Server Certificate"
# pk12util -o /etc/pki/tls/private/export.p12 -d /etc/httpd/alias/ -n "Example Server Certificate" Enter password for PKCS12 file: password Re-enter password: password pk12util: PKCS12 EXPORT SUCCESSFULCopy to Clipboard Copied! Toggle word wrap Toggle overflow Note that you must set a password on the PKCS #12 file. You need this password in the next step.
Export the private key from the PKCS #12 file:
openssl pkcs12 -in /etc/pki/tls/private/export.p12 -out /etc/pki/tls/private/server.key -nocerts -nodes
# openssl pkcs12 -in /etc/pki/tls/private/export.p12 -out /etc/pki/tls/private/server.key -nocerts -nodes Enter Import Password: password MAC verified OKCopy to Clipboard Copied! Toggle word wrap Toggle overflow Delete the temporary PKCS #12 file:
rm /etc/pki/tls/private/export.p12
# rm /etc/pki/tls/private/export.p12Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Set the permissions on
/etc/pki/tls/private/server.keyto ensure that only therootuser can access this file:chown root:root /etc/pki/tls/private/server.key chmod 0600 /etc/pki/tls/private/server.key
# chown root:root /etc/pki/tls/private/server.key # chmod 0600 /etc/pki/tls/private/server.keyCopy to Clipboard Copied! Toggle word wrap Toggle overflow Use the nickname of the server certificate in the NSS database to export the CA certificate:
certutil -d /etc/httpd/alias/ -L -n "Example Server Certificate" -a -o /etc/pki/tls/certs/server.crt
# certutil -d /etc/httpd/alias/ -L -n "Example Server Certificate" -a -o /etc/pki/tls/certs/server.crtCopy to Clipboard Copied! Toggle word wrap Toggle overflow Set the permissions on
/etc/pki/tls/certs/server.crtto ensure that only therootuser can access this file:chown root:root /etc/pki/tls/certs/server.crt chmod 0600 /etc/pki/tls/certs/server.crt
# chown root:root /etc/pki/tls/certs/server.crt # chmod 0600 /etc/pki/tls/certs/server.crtCopy to Clipboard Copied! Toggle word wrap Toggle overflow Use the nickname of the CA certificate in the NSS database to export the CA certificate:
certutil -d /etc/httpd/alias/ -L -n "Example CA" -a -o /etc/pki/tls/certs/ca.crt
# certutil -d /etc/httpd/alias/ -L -n "Example CA" -a -o /etc/pki/tls/certs/ca.crtCopy to Clipboard Copied! Toggle word wrap Toggle overflow Follow Configuring TLS encryption on an Apache HTTP server to configure the Apache web server, and:
-
Set the
SSLCertificateKeyFileparameter to/etc/pki/tls/private/server.key. -
Set the
SSLCertificateFileparameter to/etc/pki/tls/certs/server.crt. -
Set the
SSLCACertificateFileparameter to/etc/pki/tls/certs/ca.crt.
-
Set the