在 CentOS 上,Apache(httpd)日志的权限问题通常涉及日志文件本身权限、目录权限、SELinux、以及 logrotate 后权限变化。下面按常见场景说明。
/var/log/httpd/
├── access_log
├── error_log
├── ssl_access_log
├── ssl_error_log
默认属主:
root:root
或(某些版本)
root:apache
Apache 运行用户:
User apache
Group apache
chown -R root:apache /var/log/httpd
chmod 750 /var/log/httpd
chmod 640 /var/log/httpd/*.log
Apache 以
apache用户运行,只需要读 + 写权限即可,不需要执行权限。
CentOS 默认开启 SELinux,即使文件权限正确,也可能无法写日志。
getenforce
ls -Z /var/log/httpd
正常应为:
system_u:object_r:httpd_log_t:s0
restorecon -Rv /var/log/httpd
CentOS 使用 logrotate 轮转 Apache 日志:
/etc/logrotate.d/httpd
示例(默认):
/var/log/httpd/*log {
missingok
notifempty
sharedscripts
delaycompress
postrotate
/bin/systemctl reload httpd.service > /dev/null 2>/dev/null || true
endscript
}
可显式指定:
create 640 root apache
修改后测试:
logrotate -d /etc/logrotate.d/httpd
| 原因 | 解决方法 |
|---|---|
| 文件属主错误 | chown root:apache |
| 目录无写权限 | chmod 750 |
| SELinux 拦截 | restorecon -Rv |
| logrotate 重置权限 | create 640 root apache |
| 自定义日志路径 | 目录需 httpd_log_t |
如果你改了日志路径,例如:
/opt/www/logs/
必须:
chown -R root:apache /opt/www/logs
chmod 750 /opt/www/logs
semanage fcontext -a -t httpd_log_t "/opt/www/logs(/.*)?"
restorecon -Rv /opt/www/logs
ls -lZ /var/log/httpd
ps aux | grep httpd
getenforce
restorecon -Rv /var/log/httpd
如果你能贴出:
ls -lZ /var/log/httpd我可以帮你精确判断是哪种权限问题。