Install ModSecurity on Apache for Ubuntu: A Comprehensive Guide
ModSecurity on Apache for Ubuntu blocks web attacks by checking incoming traffic before it hits your server. This free security tool stops dangerous requests like SQL injection and cross-site scripting from breaking your website.
You can set up ModSecurity version 3.0.10 alongside Apache 2.4 on an Ubuntu 22.04 LTS server to keep your web apps safe from common online threats.
Install ModSecurity and the Apache module using `sudo apt update` and `sudo apt install apache2 libapache2-mod-security2`. Enable the module with `sudo a2enmod security2` and reload Apache.
How to install ModSec with Apache on Ubuntu Linux
ModSecurity (ModSec) acts as a web application firewall (a specialized screen that filters incoming web traffic) to make your Ubuntu server more secure. Apache served as its initial development platform, and ModSec now fully supports Nginx and IIS as a web application security (WAS) tool.
Here is how to install it with the Apache HTTP server.
Install ModSecurity and Apache
Installing ModSecurity and Apache on Ubuntu requires running a couple of terminal commands. This process adds an important security layer to the web server, helping protect it from common online threats and attacks.
sudo apt update sudo apt install apache2 libapache2-mod-security2
Checking that ModSec is installed involves running the commands below.
apachectl -M | grep security
The command above should produce an output similar to this:
ModSecurity's Apache module remains disabled by default. Turning on the ModSecurity module requires running the commands presented below. Enabling this module is crucial for activating ModSecurity's web application firewall features, which protect the website.
ModSecurity configuration on the Apache server in Ubuntu actively blocks threats by adjusting main settings. Begin by copying the recommended configuration file, modsecurity.conf-recommended, and then edit this file to best protect the server.
The default configuration file sits at /etc/modsecurity/modsecurity.conf-recommended. Customization starts by copying this file and renaming it modsecurity.conf.
sudo cp /etc/modsecurity/modsecurity.conf-recommended /etc/modsecurity/modsecurity.conf
Next, run the commands below to open the newly created config file.
sudo nano /etc/modsecurity/modsecurity.conf
# -- Rule engine initialization ---------------------------------------------- # Enable ModSecurity, attaching it to every transaction. Use detection # only to start with, because that minimises the chances of post-installation # disruption. # SecRuleEngine On # -- Request body handling --------------------------------------------------- # Allow ModSecurity to access request bodies. If you don't, ModSecurity # won't be able to see any POST parameters, which opens a large security # hole for attackers to exploit. SecRequestBodyAccess On # Enable XML request body parser. # Initiate XML Processor in case of xml content-type
Save and exit, then restart Apache.
sudo systemctl restart apache2
Add OWASP ModSecurity rules
Adding the OWASP Core Rule Set (CRS) to ModSecurity in Ubuntu gives web apps strong security rules to defend against attacks. This process relies on cloning the latest rules from GitHub directly to the server.
Clone the latest OWASP CRS from GitHub to the /usr/share/ directory by running the commands below.
sudo rm -rf /usr/share/modsecurity-crs sudo git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git /usr/share/modsecurity-crs
sudo mv /usr/share/modsecurity-crs/crs-setup.conf.example /usr/share/modsecurity-crs/crs-setup.conf
Next, run the commands below to open the ModSec security configuration file.
sudo nano /etc/apache2/mods-enabled/security2.conf
Then, edit the highlighted lines to enable the OWASP rules downloaded above.
<IfModule security2_module>
# Default Debian dir for modsecurity's persistent data
SecDataDir /var/cache/modsecurity
# Include all the *.conf files in /etc/modsecurity.
# Keeping your local configuration in that directory
# will allow for an easy upgrade of THIS file and
# make your life easier
IncludeOptional /etc/modsecurity/*.conf
# Include OWASP ModSecurity CRS rules if installed
IncludeOptional /usr/share/modsecurity-crs/*.conf
IncludeOptional /usr/share/modsecurity-crs/rules/*.conf
</IfModule>Save and exit.
Restart Apache to finish the process.
sudo systemctl restart apache2
To test that ModSec runs correctly, execute the commands below.
curl 'http://127.0.0.1/test.html?a=<script>alert(1);</script>'
An output message similar to the one below should appear:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>403 Forbidden</title> </head><body> <h1>Forbidden</h1> <p>You don't have permission to access this resource.</p> <hr> <address>Apache/2.4.52 (Ubuntu) Server at 127.0.0.1 Port 80</address> </body></html>
That completes the setup.
Conclusion:
- ModSecurity is a powerful open-source web application firewall (WAF) that offers an extra layer of security to detect and prevent attacks before they reach the web application.
- Following the installation steps outlined above, you can successfully set up ModSecurity with Apache on Ubuntu Linux.
- OWASP ModSecurity rules, when applied, provide recommended security settings to protect your applications further.
- After completing the installation and configuration, you can verify the functionality of ModSecurity by testing it with a sample HTTP request.
- Your feedback is welcomed, and please feel free to use the comment form below to report any errors or add additional insights.
What is mod_security in Apache?
ModSecurity remains the standard open-source web application firewall engine for handling HTTP request and response filtering across platforms like the Apache HTTP Server, Microsoft IIS, and Nginx.
Is ModSecurity a good WAF?
Why use ModSecurity? Web Application Firewalls function as the primary line of defense against HTTP attacks on web applications and servers. The ModSecurity WAF serves this purpose widely alongside the Coraza WAF, which OWASP also provides.
Was this guide helpful?
About the Author
Richard
Tech Writer, IT Professional
Richard, a writer for Geek Rewind, is a tech enthusiast who loves breaking down complex IT topics into simple, easy-to-understand ideas. With years of hands-on experience in system administration and enterprise IT operations, he’s developed a knack for offering practical tips and solutions. Richard aims to make technology more accessible and actionable. He's deeply committed to the Geek Rewind community, always ready to answer questions and engage in discussions.
No comments yet — be the first to share your thoughts!