CentOS 上 Apache(httpd)日志为空,通常不是“没请求”,而是日志路径、权限、配置或 Selinux等问题导致。下面按最常见原因逐一说明:
CentOS 中 Apache 叫 httpd,默认日志位置是:
/var/log/httpd/access_log
/var/log/httpd/error_log
而不是:
/var/log/apache2/ # 这是 Debian/Ubuntu
确认方式:
ls -l /var/log/httpd/
如果服务没启动,当然不会有日志。
systemctl status httpd
启动:
systemctl start httpd
systemctl enable httpd
如果你用了 VirtualHost,日志可能不在默认位置:
<VirtualHost *:80>
DocumentRoot /var/www/html
CustomLog /var/www/logs/access.log combined
ErrorLog /var/www/logs/error.log
</VirtualHost>
检查:
httpd -S
error_log 为空很正常,只要没出错。
但 access_log 为空就有问题。
Apache 用户(通常是 apache)没有写日志目录权限:
ps aux | grep httpd
修复权限:
chown -R apache:apache /var/log/httpd
chmod 755 /var/log/httpd
SELinux 开启时,日志目录上下文不对会导致“写不进去但服务不报错或只报权限”。
检查:
getenforce
如果是 Enforcing,修复上下文:
restorecon -Rv /var/log/httpd
检查主配置:
grep -R "CustomLog" /etc/httpd/
grep -R "ErrorLog" /etc/httpd/
如果有:
CustomLog off
或
ErrorLog /dev/null
日志就是空的。
例如:
测试本地:
curl http://127.0.0.1
某些情况下日志不是实时写,但 CentOS 默认不会。
systemctl status httpd
ls -l /var/log/httpd
httpd -S
curl http://127.0.0.1
getenforce
如果你愿意,可以把以下信息贴出来,我可以直接帮你定位:
httpd -S 输出/etc/httpd/conf/httpd.conf 中 Log 相关配置ls -lZ /var/log/httpd