在 CentOS 上开启 Apache(httpd)日志,通常分为 访问日志(access log) 和 错误日志(error log)。CentOS 默认使用 httpd(Apache),下面一步一步说明。
httpd -v
systemctl status httpd
如果没装:
yum install httpd -y
systemctl start httpd
systemctl enable httpd
CentOS 下 Apache 日志默认在:
/var/log/httpd/
├── access_log # 访问日志
└── error_log # 错误日志
vim /etc/httpd/conf/httpd.conf
确保以下配置存在(默认就有):
ErrorLog "logs/error_log"
LogLevel warn
<IfModule log_config_module>
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
CustomLog "logs/access_log" combined
</IfModule>
说明:
ErrorLog:错误日志CustomLog:访问日志combined:常用日志格式如果你有虚拟主机配置(如 /etc/httpd/conf.d/*.conf):
<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
访问网站后查看:
tail -f /var/log/httpd/access_log
tail -f /var/log/httpd/error_log
LogLevel debug
getenforce
如果是 Enforcing,可临时测试:
setenforce 0
firewall-cmd --add-service=http --permanent
firewall-cmd --reload
如果你是用 CentOS 8 / Stream / Apache 2.4,或者 用了宝塔 / panel,可以告诉我,我给你对应配置。