Securing a web server is crucial to protect sensitive data, ensure privacy, and maintain the integrity of your applications and services. This guide will walk you through various steps and best practices to harden your Linux web server, making it more resilient against cyber threats.
1. Update and Patch Your System
Keeping your system up to date is the first line of defense.
- Regular Updates:
- Use
aptoryumto keep your system updated.
sudo apt update && sudo apt upgradesudo yum update
- Use
- Automate Updates:
- Set up automatic updates to ensure timely patching.
sudo apt install unattended-upgradessudo dpkg-reconfigure --priority=low unattended-upgrades
- Set up automatic updates to ensure timely patching.
2. User and Permission Management
Proper user management and permissions prevent unauthorized access.
- Limit Root Access:
- Avoid using the root account directly. Use
sudofor administrative tasks.
sudo usermod -aG sudo yourusernameSecure SSH Access:
- Avoid using the root account directly. Use
-
- Disable root login via SSH.
sudo nano /etc/ssh/sshd_config# Set PermitRootLogin to no
PermitRootLogin no
- Use key-based authentication instead of passwords.
- Change the default SSH port.
# Edit /etc/ssh/sshd_configPort 2222
- Disable root login via SSH.
- Manage User Permissions:
- Use
chownandchmodto set proper file permissions.
sudo chown -R www-data:www-data /var/www/htmlsudo chmod -R 755 /var/www/html
- Use
3. Firewall Configuration
A well-configured firewall controls access to your server. You can use either some firewall like ufw or rely on managing iptables “by hand”. Make sure you decide which method do you use and stick to it. Mixing both methods is not recommended. We recommend ufw as it is easier to manage.
- Use
ufw(Uncomplicated Firewall):- Allow necessary services and deny all others.
sudo ufw default deny incomingsudo ufw default allow outgoing
sudo ufw allow ssh
sudo ufw allow http
sudo ufw allow https
sudo ufw enable
- Allow necessary services and deny all others.
- Advanced Firewall (iptables):
- Create custom rules for more granular control.
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPTsudo iptables -A INPUT -p tcp –dport 80 -j ACCEPT
sudo iptables -A INPUT -p tcp –dport 443 -j ACCEPT
sudo iptables -A INPUT -j DROP
- Create custom rules for more granular control.
4. Secure Network Services
Limiting and securing network services reduces the attack surface.
- Disable Unnecessary Services:
- Identify and stop services that are not required.
sudo systemctl list-unit-files --type=servicesudo systemctl disable some-unneeded-service
sudo systemctl stop some-unneeded-service
- Identify and stop services that are not required.
- Restrict Listening Ports:
- Ensure services only listen on required interfaces.
sudo netstat -tulnpsudo lsof -i -P -n | grep LISTEN
- Ensure services only listen on required interfaces.
5. Web Server Configuration
Proper configuration of your web server is essential for security.
- Apache:
- Disable directory listing.
sudo nano /etc/apache2/apache2.conf# Add or modify:
<Directory /var/www/>
Options -Indexes
</Directory>
- Secure .htaccess files.
<Files ~ "^\.ht">Order allow,deny
Deny from all
</Files>
- Enable security modules.
sudo a2enmod headerssudo a2enmod ssl
sudo a2enmod rewrite
- Disable directory listing.
- Nginx:
- Disable server tokens.
sudo nano /etc/nginx/nginx.conf# Add or modify:
server_tokens off;
- Restrict access to sensitive files.
location ~ /\.ht {deny all;
}
- Implement strong SSL/TLS settings.
sudo nano /etc/nginx/conf.d/ssl.conf# Add or modify:
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
ssl_ciphers ‘HIGH:!aNULL:!MD5’;
- Disable server tokens.
6. Intrusion Detection and Prevention
Monitoring your system for unauthorized activities is crucial.
- Install and Configure Fail2Ban:
- Protect your server from brute-force attacks.
sudo apt install fail2bansudo systemctl enable fail2ban
sudo systemctl start fail2ban
sudo nano /etc/fail2ban/jail.local
# Add or modify:
[sshd]
enabled = true
port = 2222
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
- Protect your server from brute-force attacks.
- Use AIDE (Advanced Intrusion Detection Environment):
- Monitor file system changes.
sudo apt install aidesudo aideinit
sudo cp /var/lib/aide/aide.db.new /var/lib/aide/aide.db
sudo aide –check
- Monitor file system changes.
7. Logging and Monitoring
Regularly review logs to detect and respond to suspicious activities.
- Centralize Logging:
- Use tools like
rsyslogorsyslog-ngto centralize logs.
sudo apt install rsyslogsudo systemctl enable rsyslog
sudo systemctl start rsyslog
- Use tools like
- Analyze Logs:
- Use log analysis tools like
LogwatchorELK Stack.
sudo apt install logwatchsudo logwatch –detail High –mailto your-email@example.com –service All –range today
- Use log analysis tools like
8. Secure Your Applications
Ensure that the applications running on your server are secure.
- Regularly Update Applications:
- Use package managers or other tools to keep applications updated.
sudo apt update && sudo apt upgrade
- Use package managers or other tools to keep applications updated.
- Configure Secure Application Settings:
- Follow security best practices for each application (e.g., database, CMS).
9. Backup and Disaster Recovery
Prepare for the worst by having a solid backup and recovery plan.
- Automate Backups:
- Use tools like
rsnapshotorduplicity.
sudo apt install rsnapshotsudo nano /etc/rsnapshot.conf
sudo rsnapshot configtest
sudo rsnapshot hourly
- Use tools like
- Test Backups:
- Regularly test your backups to ensure they can be restored.
10. Security Tools and Best Practices
Leverage security tools and follow best practices to enhance security.
- Use SELinux or AppArmor:
- Implement mandatory access control.
sudo apt install selinux-basics selinux-policy-default auditdsudo selinux-activatesudo apt install apparmor
sudo systemctl enable apparmor
sudo systemctl start apparmor
- Implement mandatory access control.
Conclusion
By following these comprehensive steps, you can significantly harden your Linux web server against various threats. Regular updates, proper user management, firewall configuration, secure web server settings, intrusion detection, logging, and backups are essential components of a robust security strategy. Stay vigilant and proactive to ensure your server remains secure.