插件窝 干货文章 CentOS搭建web服务器的安全性调优指南

CentOS搭建web服务器的安全性调优指南

bash systemctl 安全 配置 247    来源:    2025-04-13

CentOS Web服务器安全性调优指南

一、系统基础安全设置

  1. 最小化安装

    • 安装时选择"Minimal Install"模式
    • 只安装必要的软件包
  2. 系统更新

    yum update -y
    yum install yum-plugin-security -y
    yum update --security -y
    
  3. 禁用不必要的服务

    systemctl list-unit-files | grep enabled
    systemctl disable <不必要的服务>
    

二、账户与认证安全

  1. 密码策略

    # 编辑/etc/login.defs
    PASS_MAX_DAYS 90
    PASS_MIN_DAYS 7
    PASS_MIN_LEN 8
    PASS_WARN_AGE 14
    
    # 安装cracklib加强密码复杂度
    yum install cracklib -y
    
  2. SSH安全配置

    # 编辑/etc/ssh/sshd_config
    Port 2222  # 修改默认端口
    PermitRootLogin no
    MaxAuthTries 3
    LoginGraceTime 1m
    AllowUsers your_username
    Protocol 2
    

    重启SSH服务:systemctl restart sshd

  3. sudo权限控制

    • 使用visudo命令编辑/etc/sudoers
    • 限制特定用户使用特定命令

三、防火墙配置

  1. 启用firewalld

    systemctl enable firewalld
    systemctl start firewalld
    
  2. 配置防火墙规则

    firewall-cmd --permanent --add-port=80/tcp
    firewall-cmd --permanent --add-port=443/tcp
    firewall-cmd --permanent --add-port=2222/tcp  # SSH自定义端口
    firewall-cmd --reload
    

四、Web服务器安全配置

对于Apache

  1. 隐藏版本信息

    # 在/etc/httpd/conf/httpd.conf中添加
    ServerTokens Prod
    ServerSignature Off
    
  2. 禁用目录浏览

    Options -Indexes
    
  3. 限制HTTP方法

    <Directory "/var/www/html">
       <LimitExcept GET POST HEAD>
           Deny from all
       </LimitExcept>
    </Directory>
    

对于Nginx

  1. 隐藏版本信息

    # 在/etc/nginx/nginx.conf的http块中添加
    server_tokens off;
    
  2. 禁用不必要的HTTP方法

    if ($request_method !~ ^(GET|HEAD|POST)$ ) {
       return 444;
    }
    

五、文件系统安全

  1. 关键目录权限

    chmod 750 /etc/httpd/conf/
    chmod 640 /etc/httpd/conf/httpd.conf
    chown root:root /etc/httpd/conf/httpd.conf
    
  2. 设置不可变属性

    chattr +i /etc/passwd
    chattr +i /etc/shadow
    chattr +i /etc/group
    chattr +i /etc/gshadow
    

六、日志与监控

  1. 启用日志记录

    # 配置rsyslog
    yum install rsyslog
    systemctl enable rsyslog
    systemctl start rsyslog
    
  2. 安装fail2ban防暴力破解

    yum install epel-release
    yum install fail2ban
    systemctl enable fail2ban
    systemctl start fail2ban
    

七、定期安全维护

  1. 自动安全更新

    yum install yum-cron -y
    systemctl enable yum-cron
    systemctl start yum-cron
    
  2. 定期安全检查

    • 使用lynis进行安全审计:yum install lynis
    • 运行:lynis audit system
  3. 备份策略

    • 定期备份配置文件、数据库和网站数据
    • 使用cron设置自动备份任务

八、高级安全措施

  1. SELinux配置

    # 查看状态
    sestatus
    
    # 设置为enforcing模式
    setenforce 1
    
    # 永久生效
    sed -i 's/SELINUX=permissive/SELINUX=enforcing/g' /etc/selinux/config
    
  2. 安装ModSecurity WAF

    # 对于Apache
    yum install mod_security mod_security_crs
    
    # 对于Nginx
    # 需要从源码编译安装
    
  3. 配置HTTP安全头

    # 在Web服务器配置中添加
    Header set X-XSS-Protection "1; mode=block"
    Header set X-Frame-Options "SAMEORIGIN"
    Header set X-Content-Type-Options "nosniff"
    Header set Content-Security-Policy "default-src 'self'"
    

通过以上步骤,您的CentOS Web服务器将获得显著的安全提升。请根据实际业务需求调整配置,并定期复查安全设置。