How to deploy modsecurity with nginx on ubuntu 20. 04 lts

How to Deploy Modsecurity with Nginx on Ubuntu 20.04 LTS

ModSecurity is a free, open-source Web Application Firewall supported by different web servers like Apache, IIS, and Nginx. It is deployed as an external security layer to protect web servers.

In this tutorial, you will learn how to install ModSecurity firewall with pre-installed Nginx. We have used Ubuntu 20.04 server to demonstrate the process.

Pre-requisites

Ubuntu 20.04 LTS server

Nginx installed on Ubuntu server

Installation Guide

If you don’t have Nginx installed on your server, follow this guide to fulfill the Modsecurity installation requirement:

https://linuxways.net/ubuntu/how-to-install-nginx-on-ubuntu-20-04-lts-using-source-code/

Now that you have installed Nginx, let’s get started with installing ModSecurity.

Step 1:
Sponsored
Install libmodsecurity3

First of all, install git on your machine so that you can clone the ModSecurity git repository. We can do it by running this command:

sudo apt install git -y

How to deploy modsecurity with nginx on ubuntu 20. 04 lts 1

Now that git is installed, clone the repository by running this command:

git clone --depth 1 -b v3/master --single-branch https://github.com/SpiderLabs/ModSecurity /usr/local/src/ModSecurity/

How to deploy modsecurity with nginx on ubuntu 20. 04 lts 2

Step 2: Go to the Modsecurity directory

Now that you have cloned the modsecurity git repository, go the modsecurity directory following the path below:

cd /usr/local/src/ModSecurity/

How to deploy modsecurity with nginx on ubuntu 20. 04 lts 3

Step 3: Install libmodsecurity3 dependencies

Now that we are inside the Modsecurity directory, we will install libmodsecurity3 dependencies in this step. Run this command:

sudo apt install gcc make build-essential autoconf automake libtool libcurl4-openssl-dev liblua5.3-dev libfuzzy-dev ssdeep gettext pkg-config libpcre3 libpcre3-dev libxml2 libxml2-dev libcurl4 libgeoip-dev libyajl-dev doxygen -y

How to deploy modsecurity with nginx on ubuntu 20. 04 lts 4

Step 4: Install Git modules

Now, install git submodules with the help of this command:

See also  Build your private 5G network with Charmed Aether SD-Core
git submodule init

How to deploy modsecurity with nginx on ubuntu 20. 04 lts 5

Next, we will update the submodules:

git submodule update

How to deploy modsecurity with nginx on ubuntu 20. 04 lts 6

Step 5: Build the modsecurity environment

It is time to build the modsecurity environment. To do that, run the following command:

./build.sh

How to deploy modsecurity with nginx on ubuntu 20. 04 lts 7

Now configure using this command:

./configure

How to deploy modsecurity with nginx on ubuntu 20. 04 lts 8

After this, you will get this error:

How to deploy modsecurity with nginx on ubuntu 20. 04 lts 9

It is okay to ignore this and move on.

Step 6: Compile the modsecurity source code

Now we will compile the environment for libmodsecurity3 with this command:

make

How to deploy modsecurity with nginx on ubuntu 20. 04 lts 10

If you want to increase the speed of compiling, you can specify -j . I have 4 CPUs and I am going to use all 4 to compile as shown below:

make -j 4

How to deploy modsecurity with nginx on ubuntu 20. 04 lts 11

Next, we will run the install command:

sudo make install

How to deploy modsecurity with nginx on ubuntu 20. 04 lts 12

The installation is done in the /usr/local/modsecurity/.

Step 7: Install modsecurity-nginx connector

In this step, we will install Modsecurity-nginx connector. It is the connection and communication point between Nginx and ModSecurity.

First of all, we need to clone the connector repository. Do that by running this command:

sudo git clone --depth 1 https://github.com/SpiderLabs/ModSecurity-nginx.git
/usr/local/src/ModSecurity-nginx/

How to deploy modsecurity with nginx on ubuntu 20. 04 lts 13

Step 8: Install modsecurity-nginx dependencies

First, go to Nginx source directory like this:

cd /usr/local/src/nginx/nginx-1.21.1

How to deploy modsecurity with nginx on ubuntu 20. 04 lts 14

Make sure to replace the Nginx version in the command with your current Nginx version otherwise you will get an error.

See also  How to Install Python 3.8 on Ubuntu, Debian and LinuxMint

To install the necessary dependencies, run this command:

sudo apt build-dep nginx && sudo apt install uuid-dev -y

How to deploy modsecurity with nginx on ubuntu 20. 04 lts 15

Next, we will compile the Modsecurity-nginx connector module with the –with-compat flag by running this command:

sudo ./configure --with-compat --add-dynamic-module=/usr/local/src/ModSecurity-nginx

How to deploy modsecurity with nginx on ubuntu 20. 04 lts 16

Sponsored

Now run this command to create the dynamic modules:

sudo make modules

How to deploy modsecurity with nginx on ubuntu 20. 04 lts 17

Now, copy the dynamic module you just created in the objs/ngx_http_modsecurity_module.so to /usr/share/nginx/modules with the help of this command:

sudo cp objs/ngx_http_modsecurity_module.so /usr/share/nginx/modules/

How to deploy modsecurity with nginx on ubuntu 20. 04 lts 18

Step 9: Enable Modsecurity in Nginx configuration file

To enable Modsecurity in Nginx, you need to first specify the load-module and path to your modsecurity module in the configuration.

Open Nginx configuration file with the nano editor like this:

sudo nano /etc/nginx/nginx.conf

How to deploy modsecurity with nginx on ubuntu 20. 04 lts 19

In the file, add this line on the top:

load_module modules/ngx_http_modsecurity_module.so;

How to deploy modsecurity with nginx on ubuntu 20. 04 lts 20

Under the HTTP {} section, add the following code lines:

modsecurity on;
modsecurity_rules_file /etc/nginx/modsec/modsec-config.conf;

How to deploy modsecurity with nginx on ubuntu 20. 04 lts 21

Step 10: Configure directory and files for modsecurity

Create a directory with the name modsec. The path of the directory is mentioned in the command:

sudo mkdir /etc/nginx/modsec/

How to deploy modsecurity with nginx on ubuntu 20. 04 lts 22

You will need this directory in the future to store configuration files and rules.

Now, copy the sample Modsecurity configuration file from cloned git directory with this command:

sudo cp /usr/local/src/ModSecurity/modsecurity.conf-recommended /etc/nginx/modsec/modsecurity.conf

How to deploy modsecurity with nginx on ubuntu 20. 04 lts 23

Now open the modsecurity configuration file:

sudo nano /etc/nginx/modsec/modsecurity.conf

How to deploy modsecurity with nginx on ubuntu 20. 04 lts 24

Locate the SecRuleEngine directive in the file on line 7 and change it to DetectionOnly like this:

secruleEngine DetectionOnly

How to deploy modsecurity with nginx on ubuntu 20. 04 lts 25

To enable Modsecurity, find change the following directive to On like this:

secRuleEngine on

How to deploy modsecurity with nginx on ubuntu 20. 04 lts 26

Now locate the following directive on line 224:

secAuditLogParts ABIJDEFHZ

How to deploy modsecurity with nginx on ubuntu 20. 04 lts 27

Change it to:

secAuditLogParts ABCDEFHJKZ

How to deploy modsecurity with nginx on ubuntu 20. 04 lts 28

Now create modsec-config.conf file. Here you will add modsecurity.conf and other rules for modsecurity:

sudo nano /etc/nginx/modsec/modsec-config.conf

How to deploy modsecurity with nginx on ubuntu 20. 04 lts 29

Inside the file you just created, add this line:

Include /etc/nginx/modsec/modsecurity.conf

How to deploy modsecurity with nginx on ubuntu 20. 04 lts 30

Save the file and exit.

Step 11: Copy unicode.mapping file

Finally, copy the Modsecurity’s unicode.mapping file like this:

sudo cp /usr/local/src/ModSecurity/unicode.mapping /etc/nginx/modsec/

How to deploy modsecurity with nginx on ubuntu 20. 04 lts 31

Step 12: Check Nginx configuration

Before restarting Nginx, check if the configuration is fine by running this command:

sudo nginx -t

How to deploy modsecurity with nginx on ubuntu 20. 04 lts 32

If you get the following output, you are good to go:

How to deploy modsecurity with nginx on ubuntu 20. 04 lts 33

Step 13: Reload Nginx

Now restart Nginx with this command:

sudo systemctl restart nginx

How to deploy modsecurity with nginx on ubuntu 20. 04 lts 34

In this guide, we saw how we can install Modsecurity on an ubuntu server that already has pre-installed Nginx on it. We also saw how to configure ModSecurity and Nginx to connect them with the help of a few easy-to-follow commands.

Karim buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications including CCNA RS, SCP, and ACE. As an IT engineer and technical author, he writes for various websites.


Discover more from Ubuntu-Server.com

Subscribe to get the latest posts sent to your email.

Comments

No comments yet. Why don’t you start the discussion?

    Leave a Reply