在 CentOS 上,Apache 通常叫 httpd(而不是 apache2,apache2 是 Debian/Ubuntu 的叫法)。下面分别说明如何开启/配置 Apache 日志。
# 查看是否安装
rpm -qa | grep httpd
# 启动 Apache
systemctl start httpd
# 设置开机自启
systemctl enable httpd
# 查看状态
systemctl status httpd
CentOS 下 Apache 默认已经开启日志,位置在:
# 错误日志
/var/log/httpd/error_log
# 访问日志
/var/log/httpd/access_log
查看日志:
tail -f /var/log/httpd/access_log
tail -f /var/log/httpd/error_log
编辑主配置文件:
vi /etc/httpd/conf/httpd.conf
确保有以下内容(默认是有的):
ErrorLog "logs/error_log"
CustomLog "logs/access_log" combined
如果是虚拟主机,在 <VirtualHost> 中也要有:
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/html
ErrorLog /var/log/httpd/example.com-error.log
CustomLog /var/log/httpd/example.com-access.log combined
</VirtualHost>
修改后重启:
systemctl restart httpd
LogLevel warn
可改为:
LogLevel debug
(调试用,生产环境不建议)
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
如果日志写不进去,可能是 SELinux 限制:
# 查看
getenforce
# 临时关闭(不推荐)
setenforce 0
正确做法:
restorecon -Rv /var/log/httpd
firewall-cmd --permanent --add-service=http
firewall-cmd --reload
如果是 Ubuntu/Debian:
apt install apache2
systemctl start apache2
# 日志在
/var/log/apache2/access.log
/var/log/apache2/error.log
如果你能告诉我:
我可以给你一份精准配置示例。