How to Install ModSecurity with Apache on Ubuntu 22.04

Learn how to install and configure Modsecurity with Apache on Ubuntu 22.04 to bolster your web security. Follow our comprehensive tutorial to ensure a seamless setup process and protect your website from potential threats.

Welcome to this comprehensive guide on installing ModSecurity with Apache on Ubuntu 22.04. In this article, we will walk you through the step-by-step process of setting up Modsecurity, a powerful web application firewall, to enhance the security of your Apache web server. Modsecurity acts as a shield against various web-based attacks and provides an additional layer of protection for your website. So, let’s dive in and learn how to install Modsecurity with Apache on Ubuntu 22.04.


Before we begin, make sure you have a fresh installation of Ubuntu 22.04 with root access. Additionally, ensure that you have Apache installed and running on your system.

Install Apache

To install Apache, open your terminal and execute the following commands:

sudo apt update
sudo apt install apache2

Once the installation is complete, you can verify the Apache installation by entering your server’s IP address in a web browser. If Apache is properly installed, you should see the default Apache landing page.

Install ModSecurity

Next, we need to install Modsecurity on our Ubuntu system. Follow these steps to install Modsecurity:

Step 1: Install Dependencies

Before installing Modsecurity, we need to install some dependencies. Open your terminal and run the following commands:

sudo apt install git build-essential libcurl4-openssl-dev libxml2 libxml2-dev libpcre++-dev liblua5.3-dev

Step 2: Download Modsecurity

Now, let’s download the Modsecurity source code from the official GitHub repository. Run the following commands in your terminal:

cd ~
git clone
cd ModSecurity
git submodule init
git submodule update

Step 3: Configure and Install Modsecurity

Once the source code is downloaded, we can proceed with the configuration and installation of Modsecurity. Run the following commands in your terminal:

sudo make install

Configure Modsecurity

After installing Modsecurity, we need to configure it to work with Apache. Here’s how you can do it:

Step 1: Enable Modsecurity Module

Open the Apache configuration file using a text editor:

sudo nano /etc/apache2/apache2.conf

Add the following line at the end of the file:

Include modsecurity.conf

Save the changes and exit the text editor.

Step 2: Create ModSecurity Configuration File

Create a new configuration file for Modsecurity:

sudo cp /usr/local/modsecurity/lib/modsecurity.conf-recommended /etc/modsecurity/modsecurity.conf

Step 3: Enable OWASP Core Rule Set (CRS)

The OWASP Core Rule Set provides a set of predefined rules to protect your web applications. To enable the CRS, run the following command:

sudo cp /usr/local/modsecurity/lib/modsecurity-crs/rules/*.conf /etc/modsecurity/

Enable ModSecurity

To enable Modsecurity, we need to enable the necessary Apache modules and restart Apache. Follow these steps:

Step 1: Enable ModSecurity Module

sudo a2enmod security2

Step 2: Restart Apache

sudo service apache2 restart

Test Modsecurity

To ensure that Modsecurity is working correctly, let’s perform a quick test. Open your web browser and access your website. If everything is configured correctly, Modsecurity will start protecting your web applications by blocking potential attacks.


If you encounter any issues during the installation or configuration process, refer to the official Modsecurity documentation or consult the Modsecurity community for assistance.


Congratulations! You have successfully installed Modsecurity with Apache on Ubuntu 22.04. With Modsecurity up and running, your web applications are now better protected against various web-based attacks. Remember to keep your Modsecurity rules updated to ensure optimal security for your website.

I hope this guide has been helpful in understanding the process of installing Modsecurity with Apache on Ubuntu 22.04. If you have any further questions or need additional assistance, feel free to reach out.

Must Read

Related Articles