优化Ubuntu Apache2的安全设置可以通过以下步骤进行:
更新系统和软件包:
sudo apt update
sudo apt upgrade
配置防火墙:
ufw
(Uncomplicated Firewall)限制对服务器的访问,只允许必要的端口和服务,如SSH和HTTP。sudo ufw allow ssh
sudo ufw enable
使用SSH密钥认证:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
ssh-copy-id user@server_ip
sudo nano /etc/ssh/sshd_config
# 将以下行设置为 no
PasswordAuthentication no
PermitRootLogin no
# 重启SSH服务
sudo systemctl restart sshd
禁用未使用的服务和模块:
service --status-all
iptables -L
chkconfig --list
cat /etc/apache2/mods-enabled/* | grep -i loadmodule
a2dismod module_name
隐藏Apache版本和操作系统信息:
server signature
选项,将其设置为 off
,以隐藏版本和操作系统信息。vi /etc/apache2/conf-enabled/security.conf
# 将 ServerTokens Prod 和 ServerSignature Off
限制访问权限:
.htaccess
文件或主配置文件 httpd.conf
限制特定IP地址或IP段访问。<Directory /var/www/html>
Order deny,allow
Deny from all
Allow from 192.168.1.0/24
</Directory>
启用日志审计:
使用SSL/TLS加密:
sudo apt install certbot python3-certbot-apache
sudo certbot --apache
定期备份数据:
监控和日志分析:
fail2ban
,来监控和阻止恶意IP地址。sudo apt install fail2ban
sudo cp /etc/fail2ban/jail.local /etc/fail2ban/jail.local.baks
sudo systemctl start fail2ban
sudo systemctl enable fail2ban