centos

Apache2在CentOS上如何进行安全设置

小樊
38
2025-08-14 16:58:00
栏目: 智能运维

以下是在CentOS上进行Apache2安全设置的关键步骤:

  1. 更新系统与软件

    sudo yum update -y  
    sudo yum install httpd mod_ssl mod_security mod_evasive -y  # 安装Apache及安全模块  
    
  2. 配置防火墙
    开放HTTP(80)和HTTPS(443)端口:

    sudo firewall-cmd --permanent --add-service=http  
    sudo firewall-cmd --permanent --add-service=https  
    sudo firewall-cmd --reload  
    
  3. 隐藏Apache版本信息
    编辑/etc/httpd/conf/httpd.conf,设置:

    ServerTokens Prod  
    ServerSignature Off  
    
  4. 启用SSL/TLS加密

    • 生成自签名证书(或使用Let’s Encrypt):
      sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/pki/tls/private/apache-selfsigned.key -out /etc/pki/tls/certs/apache-selfsigned.crt  
      
    • 编辑/etc/httpd/conf.d/ssl.conf,启用SSL并指定证书路径:
      <VirtualHost *:443>  
          SSLEngine on  
          SSLCertificateFile /etc/pki/tls/certs/apache-selfsigned.crt  
          SSLCertificateKeyFile /etc/pki/tls/private/apache-selfsigned.key  
      </VirtualHost>  
      
  5. 配置安全模块

    • mod_security:编辑/etc/httpd/conf.d/security2.conf,启用规则引擎:
      SecRuleEngine On  
      SecAuditLog /var/log/httpd/modsec_audit.log  
      
    • mod_evasive:编辑/etc/httpd/conf.d/evasive.conf,设置防DoS参数:
      <IfModule mod_evasive20.c>  
          DOSHashTableSize 3097  
          DOSPageCount 2  
          DOSSiteCount 50  
          DOSBlockingPeriod 10  
      </IfModule>  
      
  6. 限制访问权限

    • 禁止目录列表:在配置文件中添加Options -Indexes
    • 通过.htaccess或主配置文件限制IP访问:
      <Directory "/var/www/html">  
          Require ip 192.168.1.0/24  
      </Directory>  
      
  7. 优化用户权限
    确保Apache以apache用户/组运行,网站目录权限设置为750

    sudo chown -R apache:apache /var/www/html  
    sudo chmod -R 750 /var/www/html  
    
  8. 定期更新与监控

    • 定期执行sudo yum update修复漏洞。
    • 监控日志文件/var/log/httpd/access_log/var/log/httpd/error_log

:若使用SELinux,需确保其策略允许Apache访问必要资源,可通过sudo setsebool -P httpd_can_network_connect 1调整。

以上步骤综合自,可根据实际需求调整。

0
看了该问题的人还看了