This script automates the installation of ModSecurity and the ModSecurity Nginx Connector. It also builds the dynamic Nginx module for ModSecurity and provides instructions for integrating ModSecurity with Nginx.
- Downloads and installs ModSecurity and its dependencies
- Clones the ModSecurity and ModSecurity Nginx Connector repositories
- Builds and installs the ModSecurity module for Nginx
- Provides a sample Nginx configuration to enable ModSecurity
- A server running a Debian-based Linux distribution
nginx
already installed on the system- Sudo/root privileges
- Clone this repository or download the script file:
git clone https://github.com/your-repo/modsecurity-nginx-installer.git
cd modsecurity-nginx-installer
- Make the script executable:
chmod +x install_modsecurity_nginx.sh
- Run the script as root or with sudo:
sudo ./install_modsecurity_nginx.sh
- Follow the on-screen instructions. The script will:
- Install the required dependencies
- Build and install ModSecurity
- Build and integrate the ModSecurity Nginx module
Add the following line to the top of your nginx.conf file:
load_module modules/ngx_http_modsecurity_module.so;
user www-data;
worker_processes auto;
worker_rlimit_nofile 65535;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /etc/nginx/conf.d/main/*.conf;
include /etc/nginx/modules-enabled/*.conf;
# Worker configuration
events {
worker_connections 1024;
use epoll;
multi_accept on;
}
http {
# Enable ModSecurity
modsecurity on;
modsecurity_rules_file /etc/nginx/conf/modsecurity.conf;
# Additional HTTP configuration here
}
Reload Nginx to apply the changes:
sudo systemctl reload nginx
- Installation logs are saved in .dhl_install_log.txt in the same directory as the script
- If errors occur during installation, review the log file for details
- Check if ModSecurity is enabled:
nginx -V 2>&1 | grep -o ngx_http_modsecurity_module
- wget
- tar
- autoconf
- automake
- build-essential
- git
- Additional libraries required to build ModSecurity and Nginx modules
- Ensure that the nginx.conf file references the correct ModSecurity configuration file (/etc/nginx/conf/modsecurity.conf)
- To uninstall the script's changes, clean up installed files and remove the dynamic module from the Nginx configuration
This script is distributed under the MIT License. Contributions and issues are welcome!