I've got LEMP all setup. Now I would like to add mod_security.
I tried to research but all I could find are instructions on how to recompile nginx. Is there a way to add mod_security without needing to recompile?
I'm currently running nginx/1.9.15 installed with PPA because I was after HTTP/2. I don't have experience in compiling nginx and I don't know if ever I recompile I would break nginx since I got it from PPA.
Hope you can help.
Thank you.
You can do it by adding ModSecurity as dynamic module or using Dockerized and hardened Nginx container.
Use ModSecurity with Docker
Run HTTP server with default settings
Run HTTPS server with automated Let's Encrypt
Certificates are stored in the /etc/letsencrypt directory, you should save it on your local drive.
If you don't want your webserver to listen on HTTP add the environment variable
LISTEN_HTTP
with a "no" value. But Let's Encrypt needs the port 80 to be opened so redirecting the port is mandatory.Here you have three environment variables :
SERVER_NAME
: define the FQDN of your webserver, this is mandatory for Let's Encrypt (www.yourdomain.com should point to your IP address)AUTO_LETS_ENCRYPT
: enable automatic Let's Encrypt creation and renewal of certificatesREDIRECT_HTTP_TO_HTTPS
: enable HTTP to HTTPS redirectionDocker Hub : bunkerized-nginx
Requirement:NGINX 1.11.5 and later.
Step 1 : Installing needed packages
##Step 2 : Download and Compile the ModSecurity 3 Source Code##
Note: The compilation takes about 15 minutes, depending on the processing power of your system. #Step 3 : Download the NGINX Connector for ModSecurity and Compile It as a Dynamic Module#
##Step 4 : Load the NGINX ModSecurity Connector Dynamic Module ## Add this line to /etc/nginx/nginx.conf
##Step 5 : Configure and Enable## Set up the appropriate ModSecurity configuration file. Here we’re using the recommended ModSecurity configuration provided by TrustWave Spiderlabs, the corporate sponsors of ModSecurity.
Change the SecRuleEngine directive in the configuration to change from the default “detection only” mode to actively dropping malicious traffic.
Configure one or more rules. For the purposes of this blog we’re creating a single simple rule that drops a request in which the URL argument called testparam includes the string test in its value. Put the following text in /etc/nginx/modsec/main.conf
Add the modsecurity and modsecurity_rules_file directives to the NGINX configuration to enable ModSecurity:
source : nginx.com
There is an available package from the repository
ppa:phusion.nl/misc
. Here are the basic steps to install:Thereafter you could follow configuration instructions from the other answers.
To skip this manual lengthy installation progress, I have created scripit that takes care of downloading required files, respective nginx version source code downloading and compiling and all. Just follow below simple steps to get ModSecurity installed and configured:
Installation is done now you need to add few lines in nginx.conf and in your server config in sites-available folder.
In /etc/nginx/nginx.conf file add this line immediate after pid line:
and then in your server config file under /etc/nginx/sites-available/
Thank you.