在 Apache 中开启日志记录,主要通过修改 配置文件 来实现。下面按常见场景给你一个完整说明(适用于 Apache 2.4,Linux 环境)。
Apache 主要有两类日志:
Apache 需要 mod_log_config 模块(通常默认已加载)。
apachectl -M | grep log_config
没有的话,在配置中启用:
LoadModule log_config_module modules/mod_log_config.so
编辑 Apache 配置文件(常见路径):
/etc/httpd/conf/httpd.conf
/etc/apache2/apache2.conf
/etc/apache2/sites-enabled/000-default.conf
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
CustomLog logs/access_log combined
✅ 说明:
combined:常用日志格式logs/access_log:相对路径(相对于 ServerRoot)CustomLog /var/log/apache2/access.log combined
ErrorLog logs/error_log
或:
ErrorLog /var/log/apache2/error.log
LogLevel warn
可选级别(从低到高):
debug → info → notice → warn → error → crit → alert → emerg
生产环境建议:
LogLevel warn
<VirtualHost *:80>
ServerName www.example.com
DocumentRoot /var/www/html
CustomLog /var/log/apache2/example-access.log combined
ErrorLog /var/log/apache2/example-error.log
</VirtualHost>
✅ 每个站点可以单独记录日志,方便排查问题。
apachectl -t # 检查配置是否正确
systemctl restart apache2 # Ubuntu
systemctl restart httpd # CentOS
tail -f /var/log/apache2/access.log
tail -f /var/log/apache2/error.log
chown -R apache:apache /var/log/apache2
如果你愿意,可以告诉我:
我可以给你一份 完全贴合你环境的配置示例。