# Ubuntu / Debian
apt update && apt upgrade -y
# CentOS / Rocky Linux
yum update -y
# Ubuntu / Debian
ufw default deny incoming
ufw default allow outgoing
ufw allow 22/tcp
ufw allow 80/tcp
ufw allow 443/tcp
ufw enable
# CentOS / Rocky Linux
firewall-cmd --permanent --set-default-zone=drop
firewall-cmd --permanent --zone=public --add-port=22/tcp
firewall-cmd --permanent --zone=public --add-port=80/tcp
firewall-cmd --permanent --zone=public --add-port=443/tcp
firewall-cmd --reload
apt install fail2ban -y
# 或
yum install fail2ban -y
systemctl enable fail2ban
systemctl start fail2ban
编辑 /etc/ssh/sshd_config
:
PermitRootLogin no
PasswordAuthentication no
server_tokens off;
apt install certbot python3-certbot-nginx -y
certbot --nginx
server {
listen 80;
server_name yourdomain.com;
return 301 https://$host$request_uri;
}
add_header X-Frame-Options SAMEORIGIN;
add_header X-Content-Type-Options nosniff;
add_header Referrer-Policy no-referrer-when-downgrade;
add_header Content-Security-Policy "default-src 'self';";
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload";
client_max_body_size 2M;
limit_conn_zone $binary_remote_addr zone=conn_limit_per_ip:10m;
limit_conn conn_limit_per_ip 10;
limit_req_zone $binary_remote_addr zone=req_limit_per_ip:10m rate=5r/s;
limit_req zone=req_limit_per_ip burst=10;
mysql_secure_installation
编辑 /etc/mysql/my.cnf
或 /etc/my.cnf
:
bind-address = 127.0.0.1
CREATE USER 'appuser'@'localhost' IDENTIFIED BY 'yourpassword';
GRANT SELECT, INSERT, UPDATE, DELETE ON yourdb.* TO 'appuser'@'localhost';
FLUSH PRIVILEGES;
启用察筛日志监控异常操作。
php.ini
expose_php = Off
display_errors = Off
log_errors = On
error_log = /var/log/php_errors.log
disable_functions = exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source
upload_max_filesize = 2M
post_max_size = 8M
fastcgi_param PHP_ADMIN_VALUE "open_basedir=/var/www/html:/tmp/";
定期备份数据和网站文件
安装监控工具(Netdata、Prometheus)
使用 WAF (如 Cloudflare 或自建 WAF)
部署 IDS/IPS (如 Snort)
本指南适合各种环境使用,可根据自身情况选择性加固。