在 Debian 上,Apache(通常是 Apache2)的日志功能默认就是开启的,只是你可能需要根据需求配置日志路径、级别或格式。下面给你一份从“确认是否已开”到“自定义开启”的完整说明。
Debian 使用 apache2 包,日志默认在:
/var/log/apache2/
常见日志文件:
access.log:访问日志(默认开启)error.log:错误日志(默认开启)other_vhosts_access.log:虚拟主机访问日志(如启用)ls -l /var/log/apache2/
如果有 access.log 和 error.log,说明已开启。
主配置文件:
/etc/apache2/apache2.conf
虚拟主机配置:
/etc/apache2/sites-available/
在全局或虚拟主机中:
ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn
LogLevel 可选:debug, info, notice, warn, errorLogFormat "%h %l %u %t \"%r\" %>s %b" common
CustomLog ${APACHE_LOG_DIR}/access.log common
或使用组合格式(更详细):
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
CustomLog ${APACHE_LOG_DIR}/access.log combined
编辑站点配置,例如:
/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
修改配置后一定要:
apache2ctl configtest
systemctl restart apache2
tail -f /var/log/apache2/access.log
tail -f /var/log/apache2/error.log
chown -R www-data:adm /var/log/apache2
CustomLog /dev/null combined
如果你愿意,可以告诉我:
我可以直接帮你写一份完整配置。