How to Install Apache with ModSecurity on Ubuntu 22.04 | apache

ModSecurity is a free and open-source firewall tool supported by various web servers, such as Apache, Nginx, and IIS. It is a signature-based firewall that is capable to block several types of attacks including, cross-site scripting (XSS), brute force attacks, and known code injection attacks. It provides different rule sets that allow you to customize and configure your server security. It can also monitor web traffic in real time and help you detect and respond to intrusions.

Install ModSecurity

Run the following command to install the ModSecurity Apache module:

Once the package is installed, enable the ModSecurity module with the following command:

Next, restart the Apache service to apply the changes:

You can also check the Apache status using the following command:

Configure ModSecurity

Next, edit the ModSecurity configuration file:

Uncomment the following line:

Save and close the file then rename the ModSecurity configuration file:

Next, edit the ModeSecurity configuration file:

Change the following lines:

Save and close the file then restart the Apache service to apply the changes:

Install the OWASP Core Rule Set

In order to protect your web application from malicious attacks, you will need to download and install the OWASP core rule set to your server.

First, download the latest rule set with the following command:

Once the download is completed, extract the downloaded file using the following command:

Next, create a directory for rules and move the extracted ruls inside that directory:

Next, navigate to your OWASP rules directory and rename the configuration file:

Next, edit the ModSecurity configuration file and define the path of your OWASP rule sets:

Save and close the file then verify the Apache for any syntax error with the following command:

If everything is fine, you will get the following output:

Finally, restart the Apache service to apply the configuration:

Verify ModSecurity

At this point, the Apache web server is configured with ModSecurity. Now, it’s time to test whether it works or not.

Open your command-line interface and run the following command:

You should get the 403 Forbidden error:

You can also test the ModSecurity using the following command:

You will get the following output:

You can also check the ModSecurity log for more information:

You will get the following output:

 

Leave a Comment