在CentOS上搭建Web服务器时,合理的权限和访问控制是系统安全的核心。以下是需要重点关注的策略和实施步骤:
最小权限原则
bash
chown -R apache:apache /var/www/html
chmod -R 750 /var/www/html
bash
chmod 640 /etc/httpd/conf/httpd.conf
目录结构隔离
/var/www/
├── html/ # 750 (主站点)
├── uploads/ # 770 (单独设置属组)
└── databases/ # 700 (敏感数据)
bash
chcon -R -t httpd_sys_content_t /var/www/html
bash
semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/uploads(/.*)?"
restorecon -Rv /var/www/uploads
用户隔离
User apache
Group apache
目录访问限制
<Directory "/var/www/private">
Require ip 192.168.1.0/24
Options -Indexes -ExecCGI
</Directory>
bash
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-port=443/tcp
firewall-cmd --reload
日志权限设置:
chmod 640 /var/log/httpd/*
chown root:apache /var/log/httpd/
实时监控示例:
tail -f /var/log/httpd/access_log | grep -E 'POST /login|admin.php'
ini
disable_functions = exec,passthru,shell_exec,system
open_basedir = /var/www/html:/tmp
expose_php = Off
bash
#!/bin/bash
find /var/www -type f -perm /o=w -exec ls -la {} \;
ausearch -k web_alert | grep -i denied
避免使用777权限,改用ACL精细控制:
setfacl -Rm g:devteam:rx /var/www/html
对于上传目录:
chown -R apache:apache /var/www/uploads
chmod -R 770 /var/www/uploads
find /var/www/uploads -type f -exec chmod 660 {} \;
通过以上分层防护策略,可构建纵深防御体系。建议部署后立即进行: 1. 漏洞扫描(使用OpenVAS或Nessus) 2. 压力测试(ab/siege) 3. 配置审计(lynis --tests WEB)