Step 1: SSH into the Apache server
ssh ubuntu@your-appache-serverStep 2: Enable required Apache modules
sudo a2enmod ssl rewrite headers socache_shmcbStep 3: Install security packages
sudo apt update
sudo apt install -y libapache2-mod-security2 fail2ban certbot python3-certbot-apache modsecurity-crsStep 4: Hide Apache version info
Edit the security config:
sudo nano /etc/apache2/conf-enabled/security.confFind and change these two lines:
| Find | Change to |
|---|---|
| ServerTokens OS | ServerTokens Prod |
| ServerSignature On | ServerSignature Off |
Save and exit: Ctrl+O, Enter, Ctrl+X
Step 5: Enable ModSecurity WAF
sudo cp /etc/modsecurity/modsecurity.conf-recommended /etc/modsecurity/modsecurity.conf
sudo nano /etc/modsecurity/modsecurity.confFind this line: SecRuleEngine DetectionOnly
Change it to: SecRuleEngine On
Save and exit: Ctrl+O, Enter, Ctrl+X
Step 6: Configure fail2ban
Create a local config:
sudo nano /etc/fail2ban/jail.localPaste this configuration:
[apache-auth]
enabled = true
port = http,https
filter = apache-auth
logpath = /var/log/apache2/error.log
maxretry = 3
bantime = 3600
[apache-badbots]
enabled = true
port = http,https
filter = apache-badbots
logpath = /var/log/apache2/access.log
maxretry = 2
bantime = 86400Step 7: Restart services
sudo systemctl restart apache2
sudo systemctl enable fail2ban
sudo systemctl restart fail2banStep 8: Get SSL certificate
Replace yourdomain.com with your actual domain:
sudo certbot --apache -d yourdomain.comFollow the prompts:
- Enter email address
- Agree to terms (Y)
- Choose whether to share email (Y/N)
- Select option to redirect HTTP to HTTPS (option 2)
Step 9: Verify the fixes
- Check Apache status:
sudo systemctl status apache2 - Check server headers:
curl -sI http://localhost | grep Server - Check fail2ban:
sudo fail2ban-client status - Check ModSecurity:
sudo apache2ctl -M | grep security - Test SSL:
curl -I https://yourdomain.com
Step 10: Exit the server
exitQuick Reference Checklist
| Step | Command/Action | Done |
|---|---|---|
| 1 | SSH into server | ☐ |
| 2 | Enable Apache modules | ☐ |
| 3 | Install security packages | ☐ |
| 4 | Edit security.conf | ☐ |
| 5 | Enable ModSecurity | ☐ |
| 6 | Configure fail2ban | ☐ |
| 7 | Restart services | ☐ |
| 8 | Get SSL certificate | ☐ |
| 9 | Verify all fixes | ☐ |
| 10 | Exit server | ☐ |
