在 CentOS 上配置 Apache(httpd)日志,主要包括 错误日志 和 访问日志 的配置。下面按常见场景说明。
CentOS 中 Apache 通常为 httpd,日志默认在:
/var/log/httpd/
/var/log/httpd/access_log/var/log/httpd/error_log/etc/httpd/conf/httpd.conf
/etc/httpd/conf.d/*.conf
在 httpd.conf 中:
ErrorLog /var/log/httpd/error_log
LogLevel warn
<VirtualHost *:80>
ServerName www.example.com
DocumentRoot /var/www/html
ErrorLog /var/log/httpd/www_error.log
LogLevel warn
</VirtualHost>
✅ LogLevel 级别
debug | info | notice | warn | error | crit | alert | emerg
CustomLog /var/log/httpd/access_log combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
一般推荐使用 combined。
<VirtualHost *:80>
ServerName www.example.com
DocumentRoot /var/www/html
CustomLog /var/log/httpd/www_access.log combined
ErrorLog /var/log/httpd/www_error.log
</VirtualHost>
SetEnvIf Request_URI "\.gif$" gif-image
CustomLog /var/log/httpd/404.log combined env=!gif-image
CentOS 默认使用 logrotate:
/etc/logrotate.d/httpd
示例:
/var/log/httpd/*log {
daily
missingok
rotate 14
compress
notifempty
sharedscripts
postrotate
/bin/systemctl reload httpd > /dev/null 2>/dev/null || true
endscript
}
修改配置后一定要执行:
systemctl reload httpd
或
systemctl restart httpd
tail -f /var/log/httpd/access_log
tail -f /var/log/httpd/error_log
| 问题 | 原因 |
|---|---|
| Apache 启动失败 | 日志路径无权限 |
| 日志不生成 | 虚拟主机未配置 |
| 日志过大 | 未配置 logrotate |
如果你需要:
可以继续告诉我你的具体需求。