温馨提示×

debian apache日志怎样配置

小樊
35
2026-10-04 07:00:24
栏目: 智能运维

在 Debian 上,Apache(通常是 Apache2)的日志配置主要涉及 错误日志(ErrorLog) 和 访问日志(CustomLog / access log),配置文件一般在 /etc/apache2/ 目录下。

下面按常见场景给你说明如何配置。


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

Debian 下 Apache 日志默认在:

/var/log/apache2/
├── access.log      # 访问日志
├── error.log       # 错误日志
├── other_vhosts_access.log

二、主配置文件位置

Apache 主配置:

/etc/apache2/apache2.conf

虚拟主机配置:

/etc/apache2/sites-available/
/etc/apache2/sites-enabled/

模块配置(如日志格式):

/etc/apache2/mods-available/
/etc/apache2/mods-enabled/

三、基本日志配置(全局)

1️⃣ 错误日志

在 apache2.conf 或虚拟主机中:

ErrorLog ${APACHE_LOG_DIR}/error.log

日志级别(可选):

LogLevel warn

常见级别:

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

2️⃣ 访问日志

CustomLog ${APACHE_LOG_DIR}/access.log combined

combined 是内置日志格式,内容类似:

IP - - [时间] "请求" 状态码 字节 "Referer" "User-Agent"

四、虚拟主机中配置日志(推荐)

示例(/etc/apache2/sites-available/example.conf):

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

    ErrorLog ${APACHE_LOG_DIR}/example_error.log
    CustomLog ${APACHE_LOG_DIR}/example_access.log combined
</VirtualHost>

启用站点:

a2ensite example.conf
systemctl reload apache2

五、自定义日志格式

查看默认格式:

cat /etc/apache2/apache2.conf | grep LogFormat

自定义示例:

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" myfmt
CustomLog ${APACHE_LOG_DIR}/access.log myfmt

六、按时间分割日志(常用)

方法 1:使用 apache2-logrotate(默认)

Debian 已自带:

/etc/logrotate.d/apache2

默认每天或每周轮转,保留 14 天。

查看:

cat /etc/logrotate.d/apache2

方法 2:使用 rotatelogs(按大小/时间)

CustomLog "|/usr/bin/rotatelogs /var/log/apache2/access_%Y%m%d.log 86400" combined

七、常见排错

日志不生效?

apache2ctl configtest
systemctl restart apache2

权限问题?

chown -R www-data:adm /var/log/apache2

八、总结(最简配置)

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

如果你愿意,可以告诉我:

  • 是 Debian 几
  • 想 记录什么日志
  • 是否 多站点 / HTTPS / Docker

我可以给你更精确的配置示例。

0 踩