系统更新
yum update
或 apt-get update && apt-get upgrade
SSH安全
# 修改默认端口
Port 2222
# 禁用root登录
PermitRootLogin no
# 使用密钥认证
PasswordAuthentication no
# 限制登录IP
AllowUsers user@your_ip
修改后重启SSH服务:service sshd restart
防火墙配置
# 安装iptables/firewalld
yum install firewalld
systemctl enable firewalld
systemctl start firewalld
# 开放必要端口
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --permanent --add-port=443/tcp
firewall-cmd --permanent --add-port=2222/tcp
firewall-cmd --reload
隐藏版本信息
server_tokens off;
限制HTTP方法
if ($request_method !~ ^(GET|HEAD|POST)$ ) {
return 444;
}
防止点击劫持
add_header X-Frame-Options "SAMEORIGIN";
XSS防护
add_header X-XSS-Protection "1; mode=block";
禁用目录浏览
autoindex off;
SSL安全配置
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384...';
运行安全脚本
mysql_secure_installation
修改默认端口
# /etc/my.cnf
[mysqld]
port = 3307
限制访问IP
GRANT ALL PRIVILEGES ON db.* TO 'user'@'192.168.1.%' IDENTIFIED BY 'password';
禁用远程root登录
DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1');
FLUSH PRIVILEGES;
禁用危险函数
disable_functions = exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source
限制文件访问
open_basedir = /var/www/:/tmp/
关闭错误显示
display_errors = Off
log_errors = On
限制上传文件大小
upload_max_filesize = 2M
post_max_size = 8M
网站目录权限
chown -R nginx:nginx /var/www/
find /var/www/ -type d -exec chmod 750 {} \;
find /var/www/ -type f -exec chmod 640 {} \;
敏感文件保护
chmod 600 /etc/nginx/nginx.conf
chmod 600 /etc/my.cnf
chmod 600 /etc/php.ini
启用日志
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
日志轮转
安装监控工具
# 如fail2ban
yum install fail2ban
systemctl enable fail2ban
systemctl start fail2ban
备份策略
安全扫描
# 使用lynis进行安全审计
yum install lynis
lynis audit system
漏洞监控
通过以上配置,您的LNMP服务器将具备较高的安全防护能力。请根据实际业务需求调整配置,并定期检查和更新安全设置。