October 12, 2026 · by Alex M.
Web attacks are growing in sophistication every year: SQL injection, XSS, CSRF, path traversal — the threat landscape is vast. A Web Application Firewall (WAF) is an essential defense layer for any server running web applications. ModSecurity is one of the most powerful and mature open-source WAFs available, compatible with both Nginx and Apache.
In this complete guide, you'll learn how to install and configure ModSecurity on a CLIQHOST NVMe VPS running Ubuntu 22.04, together with the OWASP Core Rule Set (CRS) — the industry-standard ruleset for web application protection.
ModSecurity is a WAF module that integrates directly into your web server and inspects every HTTP request before it reaches your application. It can:
Whether you run an e-commerce store, a corporate site, or a SaaS application on a CLIQHOST SSD VPS, ModSecurity provides a robust, battle-tested shield against the most common attack vectors.
Before you begin, make sure you have:
sudo privilegesIf you don't have a server yet, you can quickly spin up a Linux VPS at CLIQHOST with Ubuntu pre-installed.
sudo apt update && sudo apt upgrade -y
sudo apt install libapache2-mod-security2 -y
Enable the module:
sudo a2enmod security2
sudo systemctl restart apache2
Verify the module is active:
apache2ctl -M | grep security
# Expected output: security2_module (shared)
Copy the recommended configuration file:
sudo cp /etc/modsecurity/modsecurity.conf-recommended /etc/modsecurity/modsecurity.conf
Open the file and change the engine mode:
sudo nano /etc/modsecurity/modsecurity.conf
Find:
SecRuleEngine DetectionOnly
Replace with:
SecRuleEngine On
Best Practice: Start in
DetectionOnlymode first, monitor the logs for false positives, then switch toOnonce you're confident in your setup.
sudo apt install git -y
sudo git clone https://github.com/coreruleset/coreruleset.git /etc/modsecurity/crs
sudo cp /etc/modsecurity/crs/crs-setup.conf.example /etc/modsecurity/crs/crs-setup.conf
Edit the Apache ModSecurity configuration:
sudo nano /etc/apache2/mods-enabled/security2.conf
Add or update the Include directives:
<IfModule security2_module>
SecDataDir /tmp/
Include /etc/modsecurity/modsecurity.conf
Include /etc/modsecurity/crs/crs-setup.conf
Include /etc/modsecurity/crs/rules/*.conf
</IfModule>
Restart Apache:
sudo systemctl restart apache2
For Nginx, ModSecurity works as a dynamic module (libmodsecurity3 + the ngx_http_modsecurity_module connector).
sudo apt install -y build-essential libpcre3 libpcre3-dev \
libssl-dev libtool autoconf libxml2-dev libgeoip-dev \
libcurl4-openssl-dev libpcre++-dev libyajl-dev git
cd /opt
sudo git clone --depth 1 -b v3/master https://github.com/SpiderLabs/ModSecurity
cd ModSecurity
sudo git submodule init
sudo git submodule update
sudo ./build.sh
sudo ./configure
sudo make
sudo make install
Compilation typically takes 5–15 minutes depending on server resources. On a CLIQHOST NVMe VPS, the fast disk I/O speeds up the build noticeably.
Check your Nginx version:
nginx -v
# Example: nginx version: nginx/1.24.0
Download the matching Nginx source:
cd /opt
NGINX_VERSION=$(nginx -v 2>&1 | sed 's/.*nginx\///' | tr -d '\n')
wget http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz
tar -xvzf nginx-${NGINX_VERSION}.tar.gz
Clone the connector:
sudo git clone --depth 1 https://github.com/SpiderLabs/ModSecurity-nginx.git
Compile only the dynamic module:
cd nginx-${NGINX_VERSION}
sudo ./configure --with-compat --add-dynamic-module=../ModSecurity-nginx
sudo make modules
sudo cp objs/ngx_http_modsecurity_module.so /etc/nginx/modules/
Add at the very top of /etc/nginx/nginx.conf:
load_module modules/ngx_http_modsecurity_module.so;
Set up the ModSecurity configuration directory:
sudo mkdir /etc/nginx/modsecurity
sudo cp /opt/ModSecurity/modsecurity.conf-recommended /etc/nginx/modsecurity/modsecurity.conf
sudo cp /opt/ModSecurity/unicode.mapping /etc/nginx/modsecurity/
Enable the rule engine:
sudo sed -i 's/SecRuleEngine DetectionOnly/SecRuleEngine On/' /etc/nginx/modsecurity/modsecurity.conf
sudo git clone https://github.com/coreruleset/coreruleset.git /etc/nginx/modsecurity/crs
sudo cp /etc/nginx/modsecurity/crs/crs-setup.conf.example /etc/nginx/modsecurity/crs/crs-setup.conf
Create the main include file:
sudo nano /etc/nginx/modsecurity/main.conf
Content:
Include /etc/nginx/modsecurity/modsecurity.conf
Include /etc/nginx/modsecurity/crs/crs-setup.conf
Include /etc/nginx/modsecurity/crs/rules/*.conf
sudo nano /etc/nginx/sites-available/example.com
Add inside the server block:
server {
listen 80;
server_name example.com www.example.com;
modsecurity on;
modsecurity_rules_file /etc/nginx/modsecurity/main.conf;
location / {
proxy_pass http://127.0.0.1:8080;
}
}
Test and reload Nginx:
sudo nginx -t
sudo systemctl reload nginx
The easiest way to verify ModSecurity is working is to simulate a SQL injection attack:
curl -I "http://example.com/?id=1%20UNION%20SELECT%201,2,3--"
If ModSecurity is active, the response will be 403 Forbidden.
Check the logs for confirmation:
# Apache
sudo tail -f /var/log/apache2/error.log | grep ModSecurity
# Nginx
sudo tail -f /var/log/nginx/error.log | grep ModSecurity
A typical blocked-attack log entry looks like this:
ModSecurity: Access denied with code 403 (phase 2).
Matched "Operator `DetectSQLi' with parameter `' at REQUEST_URI.
[id "942100"] [msg "SQL Injection Attack Detected via libinjection"]
OWASP CRS can occasionally block legitimate requests. The recommended approach:
<LocationMatch "/wp-admin">
SecRuleRemoveById 942100
</LocationMatch>
Create an exceptions file:
sudo nano /etc/nginx/modsecurity/exceptions.conf
Content:
SecRuleRemoveById 942100
Include it in main.conf before the CRS rules.
In crs-setup.conf, adjust paranoia_level:
SecAction \
"id:900000,\
phase:1,\
nolog,\
pass,\
t:none,\
setvar:tx.paranoia_level=1"
Level 1 is the most permissive; level 4 is the strictest. Start with level 1–2 and increase gradually.
Keep your ruleset current with a weekly cron job:
sudo crontab -e
Add:
0 3 * * 1 cd /etc/nginx/modsecurity/crs && git pull && systemctl reload nginx
Configure detailed audit logging in modsecurity.conf:
SecAuditEngine RelevantOnly
SecAuditLog /var/log/modsecurity/audit.log
SecAuditLogParts ABIJDEFHZ
SecAuditLogType Serial
Create the log directory:
sudo mkdir /var/log/modsecurity
sudo chown www-data:www-data /var/log/modsecurity
For a complete security stack, you can integrate ModSecurity logs with Fail2Ban to automatically ban repeat offenders. Find more server security guides on the CLIQHOST blog.
If you'd rather delegate the setup and ongoing maintenance, CLIQHOST server management services include WAF installation, configuration, and tuning tailored to your specific application.
For larger projects requiring dedicated resources and top-tier performance, a CLIQHOST managed dedicated server gives you both the horsepower and the hands-on technical support.
Don't forget to pair ModSecurity with a valid SSL certificate from CLIQHOST — encrypted traffic + WAF inspection is the gold standard for web security.
| Step | Apache | Nginx |
|---|---|---|
| 1 | apt install libapache2-mod-security2 |
Compile libmodsecurity3 |
| 2 | a2enmod security2 |
Compile Nginx dynamic module |
| 3 | Copy modsecurity.conf |
Copy modsecurity.conf |
| 4 | Clone OWASP CRS | Clone OWASP CRS |
| 5 | SecRuleEngine On |
SecRuleEngine On |
| 6 | Restart Apache | modsecurity on in vhost + reload |
ModSecurity with the OWASP Core Rule Set is a powerful, free, and production-proven WAF solution for any Linux web server. Whether you run Apache or Nginx, setup takes under an hour and adds a meaningful layer of protection against the most prevalent web attacks.
Pairing ModSecurity with a fast CLIQHOST NVMe VPS and a properly configured SSL certificate gives your web infrastructure a solid, security-first foundation.
Need help getting your server secured and optimized? Contact the CLIQHOST team — we're here to help.
Real reviews from customers who trust CLIQHOST for performance, reliability and expert technical support.
"We moved our online shop from a foreign host and the difference is night and day — pages load instantly and support replies in minutes, in Romanian."
"Migrated 12 client sites to CLIQHOST. Free migration, zero downtime, and the cPanel setup is exactly what my team needed. Highly recommend."
"Our NVMe VPS handles traffic spikes without a sweat. Full root, local datacenter, and billing in MDL — everything we wanted from a provider."