温馨提示×

centos apache怎么日志

小樊
46
2026-10-20 11:56:36
栏目: 智能运维

在 CentOS 上,Apache(通常叫 httpd)的日志相关的常见操作主要包括:日志位置、查看日志、日志格式、日志切割、配置日志。下面按常用场景给你说明。


一、Apache 日志在哪里(CentOS)

CentOS 上 Apache 一般是通过 httpd 安装的:

yum install httpd

默认日志路径

/var/log/httpd/

常见日志文件:

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

二、查看 Apache 日志

1️⃣ 实时查看访问日志

tail -f /var/log/httpd/access_log

2️⃣ 实时查看错误日志(排错最常用)

tail -f /var/log/httpd/error_log

3️⃣ 查看最近 100 行

tail -n 100 /var/log/httpd/error_log

4️⃣ 搜索关键字(如 404、500)

grep 404 /var/log/httpd/access_log
grep "Permission denied" /var/log/httpd/error_log

三、Apache 日志配置(httpd.conf)

配置文件位置:

/etc/httpd/conf/httpd.conf

1️⃣ 日志路径配置

ErrorLog "logs/error_log"
CustomLog "logs/access_log" combined

logs/ 是相对路径,实际指向 /var/log/httpd/

2️⃣ 日志格式(combined 最常用)

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

四、虚拟主机日志(多站点)

<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 日志切割(非常重要)

CentOS 默认使用 logrotate:

查看配置

cat /etc/logrotate.d/httpd

手动执行一次切割

logrotate -f /etc/logrotate.d/httpd

默认策略:

  • 每天或每周
  • 保留 4 周
  • 自动 reload httpd

六、常用排错思路

✅ 网站打不开:

tail -f /var/log/httpd/error_log

✅ 403 Forbidden:

  • 检查 DocumentRoot 权限
  • 检查 SELinux
getenforce

✅ 查看访问来源 IP:

awk '{print $1}' /var/log/httpd/access_log | sort | uniq -c | sort -nr

七、重启 Apache

systemctl restart httpd
systemctl status httpd

如果你有具体需求(比如:

  • 想记录 POST 数据
  • 想按天分割
  • 想分析访问 IP
  • 想配置 JSON 日志

可以直接告诉我,我给你具体配置示例。

0 踩