How to block IPs with Apache
To protect your website from malicious attacks such as DOS & DDOS, you may need to block IP addresses.
This tutorial describes How To Block IPs with Apache Webserver.
1. Blocking an IP from hitting your site.
Open your Apache Vhost file:
vim /etc/httpd/conf.d/example.com.conf
Add the following:
<Directory / >
Order allow,deny
deny from 1.2.3.4
</Directory>
Replace the 1.2.3.4 with the IP address you want to block access from.
Reload / Restart Apache webserver.
systemctl restart httpd
2. Blocking an IP from hitting a subdirectory.
Add the following to your Apache Vhost file & Restart Apache web server.
<Directory /var/www/html/somedirectory >
Order allow,deny
deny from 1.2.3.4
</Directory>
This will block access to a particular sub-directory.
3. Blocking IPs from hitting your site using .htaccess file.
Open your .htaccess file & Add the following:
<Limit GET POST>
order allow,deny
deny from 1.2.3.4
deny from 5.7.8.9
allow from all
</Limit>
Restart/Reload your Apache web server:
systemctl restart httpd
Leave a Reply