1、安装依赖
yum install glib2 glib2-devel GeoIP-devel ncurses-devel zlib zlib-develyum install gcc -y
2、安装goaccess
# wget https://tar.goaccess.io/goaccess-1.3.tar.gz
# tar -xzvf goaccess-1.3.tar.gz
# cd goaccess-1.3/
# ./configure --enable-utf8 --enable-geoip=legacy
# make
# make install
3、修改nginx.conf文件的日志存储格式
log_format main '$remote_addr - $remote_user [$time_local] requesthost:"$http_host"; "$request" requesttime:"$request_time"; '
'$status $body_bytes_sent "$http_referer" - $request_body'
'"$http_user_agent" "$http_x_forwarded_for"';
4、修改文件/usr/local/etc/goaccess.conf改成goaccess格式标准对应为
time-format %H:%M:%S
date-format %d/%b/%Y
log-format "%x","%h",%^,%^,"%m","%U","%s",%^,"%b","%D",%^,"%R","%u"
log-format COMBINED
5、测试生成页面
goaccess -f /var/log/nginx/access.log -o /你的目录路径/log.html --log-format=COMBINED
6、加入定时任务
编写定时执行脚本 goaccess.sh
#!/bin/bash
LANG="zh_CN.UTF-8"
goaccess -p /goaccess配置目录/goaccess.conf -f /nginx日志目录/access.log -o /日志输出目录/log$(date +%Y%m%d).html --log-format=COMBINED
设置定时任务,第天凌晨2点执行 >/dev/null 2>&1表现不输出日志
# crontab -e
0 2 * * * /bin/bash /root/goaccess.sh >/dev/null 2>&1
7、重启定时任务
systemctl restart crond #重启
systemctl status crond #查看状态
注意:若定时任务执行出错,类似:
import _mysql ImportError: libmysqlclient.so.18: cannot open shared object file: No such file or directory
可能是你缺少 libmysqlclient.so.18,或是高于 libmysqlclient.so.18
你可以查看mysql版本,安装相应的mysql-community-libs-compat。如我的mysql版本是5.7,安装如下:
yum --enablerepo=mysql57-community install mysql-community-libs-compat
创建软链
#/usr/lib64/mysql 你的libmysqlclient.so.18所在位置
#lib64你的系统位数
ln -s /usr/lib64/mysql/libmysqlclient.so.18 /usr/lib64/libmysqlclient.so.18