在 CentOS 上,Apache(通常叫 httpd)的日志相关的常见操作主要包括:日志位置、查看日志、日志格式、日志切割、配置日志。下面按常用场景给你说明。
CentOS 上 Apache 一般是通过 httpd 安装的:
yum install httpd
/var/log/httpd/
常见日志文件:
/var/log/httpd/access_log/var/log/httpd/error_logtail -f /var/log/httpd/access_log
tail -f /var/log/httpd/error_log
tail -n 100 /var/log/httpd/error_log
grep 404 /var/log/httpd/access_log
grep "Permission denied" /var/log/httpd/error_log
配置文件位置:
/etc/httpd/conf/httpd.conf
ErrorLog "logs/error_log"
CustomLog "logs/access_log" combined
logs/是相对路径,实际指向/var/log/httpd/
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>
CentOS 默认使用 logrotate:
cat /etc/logrotate.d/httpd
logrotate -f /etc/logrotate.d/httpd
默认策略:
✅ 网站打不开:
tail -f /var/log/httpd/error_log
✅ 403 Forbidden:
DocumentRoot 权限getenforce
✅ 查看访问来源 IP:
awk '{print $1}' /var/log/httpd/access_log | sort | uniq -c | sort -nr
systemctl restart httpd
systemctl status httpd
如果你有具体需求(比如:
可以直接告诉我,我给你具体配置示例。