在 CentOS 上,Apache(通常叫 httpd)的日志功能一般是默认开启的。下面分几种常见情况说明如何“开启/配置/查看”日志。
rpm -qa | grep httpd
systemctl status httpd
如果没装:
yum install httpd -y
systemctl start httpd
systemctl enable httpd
CentOS 下 Apache 日志通常在:
/var/log/httpd/
常见日志文件:
access_log:访问日志error_log:错误日志Apache 主配置文件:
/etc/httpd/conf/httpd.conf
检查以下配置是否存在(一般默认就有):
ErrorLog "logs/error_log"
CustomLog "logs/access_log" combined
说明:
ErrorLog:错误日志CustomLog:访问日志combined:日志格式(推荐)如果注释掉了,取消注释即可。
示例:
<VirtualHost *:80>
ServerName www.example.com
DocumentRoot /var/www/html
ErrorLog /var/log/httpd/www-error.log
CustomLog /var/log/httpd/www-access.log combined
</VirtualHost>
保存后:
httpd -t
systemctl restart httpd
如果日志目录不是默认目录,可能写不进去:
查看:
getenforce
临时关闭测试:
setenforce 0
正式解决(恢复默认日志目录最省事):
restorecon -Rv /var/log/httpd
tail -f /var/log/httpd/access_log
tail -f /var/log/httpd/error_log
| 问题 | 解决 |
|---|---|
| 没日志 | 检查 CustomLog 是否配置 |
| 日志不更新 | systemctl restart httpd |
| 权限问题 | 目录属主 root:apache,权限 755 |
如果你能告诉我:
我可以给你更精确的配置示例。