Ubuntu 上 Apache 权限管理实操指南
一 基础安全原则与运行身份
ps aux | grep apache2APACHE_RUN_USER 与 APACHE_RUN_GROUPexport APACHE_RUN_USER=你的用户export APACHE_RUN_GROUP=你的组sudo systemctl restart apache2二 文件与目录权限设置
sudo chown -R www-data:www-data /var/www/htmlsudo find /var/www/html -type d -exec chmod 755 {} \;sudo find /var/www/html -type f -exec chmod 644 {} \;chmod 770 或 750chmod 660 或 640find /var/www/html/uploads -type d -exec chmod 770 {} \; && find /var/www/html/uploads -type f -exec chmod 660 {} \;sudo chown -R deploy:www-data /var/www/htmlsudo find /var/www/html -type d -exec chmod 750 {} \;sudo find /var/www/html -type f -exec chmod 640 {} \;sudo find /var/www/html/{uploads,cache,var} -type d -exec chmod 770 {} \;sudo find /var/www/html/{uploads,cache,var} -type f -exec chmod 660 {} \;sudo chmod g+s /var/www/html。三 基于 Apache 的访问控制
<Directory> 中):
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
<Directory /var/www/secret>
Require all denied
</Directory>
<Location "/admin">
Require ip 203.0.113.10 198.51.100.0/24
</Location>
sudo a2enmod auth_basic authn_filesudo htpasswd -c /etc/apache2/.htpasswd alice<Directory "/var/www/html/admin">
AuthType Basic
AuthName "Restricted Area"
AuthUserFile /etc/apache2/.htpasswd
Require valid-user
</Directory>
sudo apache2ctl configtest && sudo systemctl reload apache2。四 多用户协作与最小权限实践
setfacl -m u:someuser:rwx /path)五 安全加固与排错
sudo aa-status 查看状态;必要时为 /usr/sbin/apache2 配置或调整策略后再 sudo systemctl reload apparmorsudo ufw allow 'Apache Full'sudo apache2ctl configtestsudo tail -f /var/log/apache2/error.logsudo tail -f /var/log/apache2/access.logsudo systemctl reload apache2(配置测试通过后)或 sudo systemctl restart apache2。