温馨提示×

centos apache如何日志

小樊
36
2026-10-26 11:40:59
栏目: 智能运维

在 CentOS 上,Apache(通常是 httpd)的日志位置和配置方式如下:


一、Apache 日志文件位置(默认)

在 CentOS 中,Apache 一般通过 httpd 安装:

yum install httpd

1. 默认日志目录

/var/log/httpd/

常见日志文件:

  • 访问日志:/var/log/httpd/access_log
  • 错误日志:/var/log/httpd/error_log

查看日志示例:

tail -f /var/log/httpd/access_log
tail -f /var/log/httpd/error_log

二、Apache 日志相关配置

配置文件位置:

/etc/httpd/conf/httpd.conf

虚拟主机配置一般在:

/etc/httpd/conf.d/*.conf

1. 访问日志配置

CustomLog "logs/access_log" combined

常见日志格式:

  • common
  • combined(包含 Referer 和 User-Agent)

2. 错误日志配置

ErrorLog "logs/error_log"
LogLevel warn

LogLevel 可选:

  • debug
  • info
  • notice
  • warn(默认)
  • error
  • crit

三、虚拟主机单独配置日志

示例:

<VirtualHost *:80>
    ServerName www.example.com
    DocumentRoot /var/www/html/example

    ErrorLog /var/log/httpd/example_error.log
    CustomLog /var/log/httpd/example_access.log combined
</VirtualHost>

修改后重启 Apache:

systemctl restart httpd

四、日志轮转(Logrotate)

CentOS 默认使用 logrotate 管理 Apache 日志:

配置文件:

/etc/logrotate.d/httpd

示例内容:

/var/log/httpd/*log {
    missingok
    notifempty
    sharedscripts
    delaycompress
    postrotate
        /bin/systemctl reload httpd.service > /dev/null 2>&1 || true
    endscript
}

手动执行:

logrotate -f /etc/logrotate.d/httpd

五、常见排查命令

# 实时查看访问日志
tail -f /var/log/httpd/access_log

# 查看错误
grep -i error /var/log/httpd/error_log

# 查看某个 IP 的访问
grep "192.168.1.100" /var/log/httpd/access_log

# 查看 404 请求
grep " 404 " /var/log/httpd/access_log

如果你是指:

  • ✅ 如何开启访问/错误日志
  • ✅ 如何按虚拟主机分离日志
  • ✅ 如何分析 Apache 日志
  • ✅ 如何用 cron + split 按天切割日志

可以告诉我你的具体需求,我可以进一步说明。

0 踩