ModSecurity and the OWASP Core Rule Set for Apache on AlmaLinux 9 and Rocky Linux 9
Install mod_security for Apache on the RHEL 9 family, load the OWASP Core Rule Set 4, run it in detection mode, then block and tune per site.
This guide installs ModSecurity as a web application firewall for Apache httpd on the RHEL 9 family (AlmaLinux 9, Rocky Linux 9 and CentOS Stream 9), adds the OWASP Core Rule Set (CRS), and shows how to move from logging to blocking and how to deal with false positives per site.
Older tutorials were written for CentOS 6, which reached end of life in 2020; CentOS 7 and 8 have followed. AlmaLinux and Rocky Linux are the drop-in successors, and CentOS Stream 9 is the rolling preview of RHEL 9. You no longer compile anything: mod_security is in the standard AppStream repository.
What has changed since older tutorials
- Project ownership: ModSecurity was developed by Trustwave SpiderLabs. Trustwave ended its support on 1 July 2024 and the project was transferred to OWASP in 2024, where it continues as a community project with releases for both engine lines.
- Engine for Apache: Apache uses the ModSecurity 2.9 line (
mod_security2). ModSecurity 3 is a separate library aimed at Nginx; the CRS project does not support running it under Apache. The RHEL 9 family ships 2.9.6 with security fixes backported by the distribution. - Rules: the Core Rule Set is its own OWASP project, no longer tied to SpiderLabs. The current major version is CRS 4. The 3.3 line reached end of life upstream in the third quarter of 2026.
Requirements
- AlmaLinux 9, Rocky Linux 9 or CentOS Stream 9 and a user with
sudorights. - A website served by Apache httpd. The examples use
example.com.
Step 1: Install Apache and mod_security
sudo dnf install httpd mod_security
sudo systemctl enable --now httpdThe package sets up everything the engine needs:
| Path | Purpose |
|---|---|
/etc/httpd/conf.modules.d/10-mod_security.conf | Loads the module |
/etc/httpd/conf.d/mod_security.conf | Engine settings: rule engine, body limits, logging |
/etc/httpd/modsecurity.d/ | Your own *.conf files, loaded first |
/etc/httpd/modsecurity.d/activated_rules/ | Rule files from packages, loaded second |
/etc/httpd/modsecurity.d/local_rules/ | Local rules and exclusions, loaded last |
/var/log/httpd/modsec_audit.log | Audit log with full details of flagged requests |
Restart Apache and confirm the module is active:
sudo systemctl restart httpd
sudo httpd -M | grep securityThe output should list security2_module.
Step 2: Start in detection mode
The packaged configuration sets SecRuleEngine On, which blocks immediately once rules are present. On a server with live sites, switch to DetectionOnly first. In that mode rules are processed and matches are logged, but no request is denied.
sudo sed -i 's/SecRuleEngine On/SecRuleEngine DetectionOnly/' /etc/httpd/conf.d/mod_security.conf
grep SecRuleEngine /etc/httpd/conf.d/mod_security.confStep 3: Install the Core Rule Set
You have two ways to get rules. Use one of them, never both, because the rule IDs would collide.
Option A: CRS 4 from the project (recommended)
Look up the current release on the CRS releases page and set it in the first line. The 4.25 line is the long-term support release, supported until the third quarter of 2027.
CRS_VERSION=4.25.2
cd /tmp
curl -LO https://github.com/coreruleset/coreruleset/archive/refs/tags/v${CRS_VERSION}.tar.gz
curl -LO https://github.com/coreruleset/coreruleset/releases/download/v${CRS_VERSION}/coreruleset-${CRS_VERSION}.tar.gz.ascCheck the signature. The fingerprint of the signing key is printed in the CRS installation documentation; compare it with what gpg shows.
gpg --fetch-key https://coreruleset.org/security.asc
gpg --verify coreruleset-${CRS_VERSION}.tar.gz.asc v${CRS_VERSION}.tar.gzUnpack to /etc/crs4 and create the setup file:
sudo mkdir /etc/crs4
sudo tar -xzf v${CRS_VERSION}.tar.gz --strip-components 1 -C /etc/crs4
sudo cp /etc/crs4/crs-setup.conf.example /etc/crs4/crs-setup.confTell Apache to load it. A file in /etc/httpd/modsecurity.d/ is picked up by the packaged mod_security.conf:
sudo tee /etc/httpd/modsecurity.d/crs4.conf > /dev/null <<'EOF'
IncludeOptional /etc/crs4/crs-setup.conf
IncludeOptional /etc/crs4/plugins/*-config.conf
IncludeOptional /etc/crs4/plugins/*-before.conf
IncludeOptional /etc/crs4/rules/*.conf
IncludeOptional /etc/crs4/plugins/*-after.conf
EOFOption B: the distribution package
AppStream also contains mod_security_crs. It installs the rules under /usr/share/mod_modsecurity_crs/rules/, links them into activated_rules/ and puts the setup file at /etc/httpd/modsecurity.d/crs-setup.conf, so it works without further configuration:
sudo dnf install mod_security_crsThe trade-off: this package is CRS 3.3.5. It is tested against the packaged engine and updated through dnf, but the 3.3 line no longer gets new rules or fixes from the CRS project itself. Choose it if you want everything maintained by the distribution, or if Apache reports rule syntax errors with CRS 4 on the packaged engine.
Check and restart
sudo apachectl configtest
sudo systemctl restart httpdStep 4: Test the rules
Send a request that resembles an attack (this is the test from the CRS documentation):
curl -i 'http://example.com/?foo=/etc/passwd&bar=/bin/sh'In detection mode the page loads normally. The match is written to Apache's error log as a short message and to the audit log in full:
sudo grep ModSecurity /var/log/httpd/error_log | tail -n 5
sudo tail -n 60 /var/log/httpd/modsec_audit.logEach audit entry is split into lettered sections: the request headers, the response headers and a section listing every rule that matched with its ID, message and the data that triggered it. The rule IDs are what you need for tuning.
Step 5: Enable blocking
Let detection mode run under real traffic for a few days. When legitimate requests no longer show up in the log, turn blocking on:
sudo sed -i 's/SecRuleEngine DetectionOnly/SecRuleEngine On/' /etc/httpd/conf.d/mod_security.conf
sudo apachectl configtest && sudo systemctl restart httpdThe test request now returns 403 Forbidden.
How CRS decides to block
CRS does not block on the first matching rule. Every match adds points to an anomaly score for the request, and a final rule (949110 for inbound requests) denies it when the score reaches the threshold. Two settings in crs-setup.conf control how strict this is:
- Paranoia level (1 to 4): higher levels enable more aggressive rules and produce more false positives. Stay on the default level 1 until the site runs cleanly.
- Anomaly score thresholds: the default inbound threshold means one critical match is enough to block. Raising it temporarily is a way to introduce blocking gradually on a busy site.
Tuning for individual sites
Because ModSecurity 2 is an Apache module, its directives work in the main configuration, in a <VirtualHost> and in <Location> blocks. That lets you fix a false positive exactly where it happens. Exclusions must be loaded after the rule they refer to, so put global ones in /etc/httpd/modsecurity.d/local_rules/, which is included last:
sudo nano /etc/httpd/modsecurity.d/local_rules/exclusions.conf# Remove one rule everywhere
SecRuleRemoveById 942100
# Keep the rule but stop it from inspecting one parameter
SecRuleUpdateTargetById 942100 "!ARGS:content"To limit an exclusion to one path, or to switch a single site back to logging only, edit that site's virtual host. Apache reads the files in /etc/httpd/conf.d/ in alphabetical order, so the virtual host file must sort after mod_security.conf for a rule removal to take effect; a name such as vhost-example.com.conf does that:
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/example.com
# Log only for this site while it is being tuned
SecRuleEngine DetectionOnly
<Location "/admin/editor">
SecRuleRemoveById 941100 942100
</Location>
</VirtualHost>Run sudo apachectl configtest and restart httpd after each change. Prefer the narrowest fix: one parameter before one rule, one location before the whole site. Avoid SecRuleEngine Off unless a site must not be inspected at all.
Maintenance
- Engine:
sudo dnf upgradedelivers fixes formod_security. Restart httpd afterwards. - Rules (option A): unpack each new CRS release into a fresh directory, copy
crs-setup.confand your plugins, update the paths incrs4.conf, then runconfigtest. Your exclusions inlocal_rules/are untouched. - Logs: the audit log grows quickly on busy or frequently attacked sites. Check that it is covered by log rotation (
/etc/logrotate.d/httpd) and watch disk usage in/var/log/httpd.
Frequently asked questions
Should I use ModSecurity 3 with Apache instead?
No. Version 3 is a library with connectors, and the combination that the engine and CRS projects support for Apache is the 2.9 line. Version 2 is not deprecated; it still receives releases from the OWASP project and backported fixes in RHEL-family packages.
Does ModSecurity replace firewalld or Fail2ban?
No, they work at different layers. firewalld decides which ports and source addresses reach the server at all. Fail2ban bans addresses based on log patterns. ModSecurity inspects the content of HTTP requests that were already allowed in. Keep all of them.
A plugin or CMS update suddenly gets blocked. What now?
Look up the request in /var/log/httpd/modsec_audit.log, note the rule IDs and the parameter named in the match, and add a targeted exclusion as shown above. For common applications such as WordPress, Nextcloud or phpMyAdmin, CRS 4 offers exclusion plugins that go into /etc/crs4/plugins/ and cover the known false positives.
Will the firewall protect an outdated application?
Only partly. Generic rules stop many common injection and traversal attempts, but they do not know every application-specific flaw. Treat ModSecurity as an extra layer that buys time, not as a substitute for updates.