Apache2日志在CentOS中的查看方法
在CentOS系統中,Apache2(通常以httpd
服務名稱運行)的日志文件默認存儲在/var/log/httpd/
目錄下,主要包括兩類核心日志:
access_log
。error_log
。tail -f
命令可實時跟蹤訪問日志的最新內容,按Ctrl+C
停止監控。sudo tail -f /var/log/httpd/access_log
sudo tail -f /var/log/httpd/error_log
-n
參數。sudo tail -n 20 /var/log/httpd/access_log
若日志文件較大,可使用less
命令分頁查看,支持上下箭頭翻頁、PgUp/PgDn
快速翻頁,按q
退出。
sudo less /var/log/httpd/access_log
若需實時追蹤日志更新,可在less
中按下Shift+F
進入“跟隨模式”(類似tail -f
)。
通過grep
命令可篩選日志中的特定信息(如某個IP地址、狀態碼、關鍵字等),支持正則表達式:
sudo grep '192.168.1.1' /var/log/httpd/access_log
sudo grep 'error' /var/log/httpd/error_log
sudo grep ' 404 ' /var/log/httpd/access_log
若需查看某一時間段的日志(如2025年10月15日的訪問日志),可結合grep
和正則表達式:
sudo grep '10/Oct/2025' /var/log/httpd/access_log
其中10/Oct/2025
為Apache默認的時間格式(日/月/年),可根據實際情況調整。
Lnav是一款增強型日志查看工具,支持語法高亮、實時監控、SQL查詢等功能,可提升日志分析效率。
sudo yum install lnav
sudo lnav /var/log/httpd/access_log /var/log/httpd/error_log
GoAccess是一款實時Web日志分析工具,支持終端和瀏覽器展示,可生成HTML報表,適合快速查看訪問統計(如PV、UV、熱門頁面等)。
sudo yum install goaccess
sudo goaccess /var/log/httpd/access_log --log-format=COMBINED
sudo systemctl status httpd
),并檢查配置文件中的CustomLog
和ErrorLog
指令是否正確設置。logrotate
工具)可避免占用過多磁盤空間。