how to install mod_security in centos 7

How To Install mod_security on CentOS 7

mod_security is a popular web application firewall that improves website security and offers website protection against malicious attacks. If your website runs on Apache, NGINX or IIS, it is advisable that you set up mod_security. In this article, we will look at how to install mod_security on CentOS 7.


What is mod_security

mod_security is a web application firewall that offers real-time monitoring, logging and filtering based on user-defined rules. It was initially built as Apache module but has grown to support NGINX and IIS as well.


How To Install mod_security on CentOS 7

Here are the steps to install mod_security on CentOS 7.


1. Update System

Open terminal and run the following commands to update your system

# yum clean all
# yum -y update

Also read : How to Set up Virtual Hosts in Apache


2. Install mod_security Dependencies

Run the following commands to install mod_security dependencies

# yum install gcc make httpd-devel libxml2 pcre-devel libxml2-devel curl-devel git

Also read : How to Create Custom 404 Page in Apache


3. Download & Install mod_security

We will download and install mod_security from source code. Run the following commands to do so.

# wget https://www.modsecurity.org/tarball/2.9.3/modsecurity-2.9.3.tar.gz
# tar xzf modsecurity-apache_2.9.3.tar.gz
# cd modsecurity-apache_2.9.3
# ./configure
# make install
# cp modsecurity.conf-recommended /etc/httpd/conf.d/modsecurity.conf
# cp unicode.mapping /etc/httpd/conf.d/

Also read : How to Create Self-Signed SSL certificate for Apache


4. Enable mod_security

Open Apache configuration file in a text editor.

# sudo vi /etc/httpd/conf/httpd.conf

Enable mod_security by uncommenting the following line by removing # at its beginning

#LoadModule security2_module modules/mod_security2.so

to

LoadModule security2_module modules/mod_security2.so

Also add the following lines at the end of the file

<IfModule security2_module>
   Include conf.d/modsecurity.conf
</IfModule>

Also read : How to use Apache bench for Load Testing


5. Configure mod_security

It is recommended to setup core mod_security rules by downloading and installing Open Web Application Security Project (OWASP) rule set.

# cd /etc/httpd
# git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
# mv owasp-modsecurity-crs modsecurity-crs
# cd modsecurity-crs
# cp modsecurity_crs_10_setup.conf.example modsecurity_crs_10_config.conf

Open Apach web configuration file

# sudo vi /etc/httpd/conf/httpd.conf

Add the following lines at its end.

Include modsecurity-crs/modsecurity_crs_10_config.conf
Include modsecurity-crs/base_rules/*.conf

Also read : How to Remove index.php from URL


6. Restart Apache Web Server

Restart Apache web server to apply changes

# systemctl restart httpd

That’s it. mod_security will be installed on your CentOS 7 system. Ubiq makes it easy to visualize data, and monitor them in real-time dashboards. Try Ubiq for free.