二进制安装JDK和Tomcat
Oracle JDK的二进制文件安装
https://www.oracle.com/java/technologies/java-se-glance.html #官网下载地址
[root@rocky8 ~]# tar xf jdk-8u341-linux-x64.tar.gz -C /usr/local/
[root@rocky8 ~]# cd /usr/local/
[root@rocky8 local]# ln -s jdk1.8.0_341/ jdk
[root@rocky8 local]# vim /etc/profile.d/jdk.sh
export JAVA_HOME=/usr/local/jdk #J AVA_HOME变量必须设置,否则tomcat启动时会出错误
export PATH=$PATH:$JAVA_HOME/bin
[root@rocky8 local]# . /etc/profile.d/jdk.sh
[root@rocky8 local]# java -version
java version "1.8.0_341"
Java(TM) SE Runtime Environment (build 1.8.0_341-b10)
Java HotSpot(TM) 64-Bit Server VM (build 25.341-b10, mixed mode)
二进制安装Tomcat
https://tomcat.apache.org/ #官网下载
https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/ #清华大学tomcat下载地址
[root@rocky8 ~]# tar xf apache-tomcat-9.0.65.tar.gz -C /usr/local/
[root@rocky8 ~]# cd /usr/local/
[root@rocky8 local]# ln -s apache-tomcat-9.0.65/ tomcat
[root@rocky8 local]# echo 'PATH=/usr/local/tomcat/bin:$PATH' > /etc/profile.d/tomcat.sh
[root@rocky8 local]# . /etc/profile.d/tomcat.sh
[root@rocky8 local]# startup.sh #启动
[root@rocky8 local]# shutdown.sh #停止
[root@rocky8 local]# catalina.sh start #启动 等同shartup
[root@rocky8 local]# catalina.sh stop #停止 等同shutdown.sh
# 配置 tomcat 自启动的 service 文件
[root@rocky8 local]# useradd -r -s /sbin/nologin tomcat
[root@rocky8 local]# vim /usr/local/tomcat/conf/tomcat.conf
JAVA_HOME=/usr/local/jdk
JRE_HOME=/usr/local/jdk/jre
[root@rocky8 local]# chown -R tomcat. /usr/local/tomcat
[root@rocky8 local]# vim /lib/systemd/system/tomcat.service
[Unit]
Description=Tomcat
After=syslog.target network.target
[Service]
Type=forking
EnvironmentFile=/usr/local/tomcat/conf/tomcat.conf
ExecStart=/usr/local/tomcat/bin/startup.sh
ExecStop=/usr/local/tomcat/bin/shutdown.sh
PrivateTmp=true
User=tomcat
Group=tomcat
[Install]
WantedBy=multi-user.target
[root@rocky8 local]# systemctl daemon-reload
[root@rocky8 tomcat]# systemctl start tomcat.service
tomcat日志实现json格式的访问日志
[root@rocky8 tomcat]# vim /usr/local/tomcat/conf/server.xml
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="{"clientip":"%h","ClientUser":"%l","authenticated":"%u","AccessTime":"%t","method":"%r","status":"%s","SendBytes":"%b","Query;string":"%q","partner":"%{Referer}i","AgentVersion":"%{User-Agent}i"}"/> #增加这行,注意,这是一整行
<!-- pattern="%h %l %u %t "%r" %s %b" --> #注释这行
[root@rocky8 tomcat]# systemctl restart tomcat
[root@rocky8 tomcat]# tail -f /usr/local/tomcat/logs/
[root@rocky8 tomcat]# tail -f /usr/local/tomcat/logs/localhost_access_log.2022-09-21.txt
10.0.0.1 - - [21/Sep/2022:22:05:06 +0800] "GET / HTTP/1.1" 200 11230
10.0.0.1 - - [21/Sep/2022:22:05:06 +0800] "GET /tomcat.css HTTP/1.1" 200 5542
10.0.0.1 - - [21/Sep/2022:22:05:06 +0800] "GET /tomcat.svg HTTP/1.1" 200 67795
10.0.0.1 - - [21/Sep/2022:22:05:06 +0800] "GET /bg-upper.png HTTP/1.1" 200 3103
10.0.0.1 - - [21/Sep/2022:22:05:06 +0800] "GET /asf-logo-wide.svg HTTP/1.1" 200 27235
10.0.0.1 - - [21/Sep/2022:22:05:06 +0800] "GET /bg-button.png HTTP/1.1" 200 713
10.0.0.1 - - [21/Sep/2022:22:05:06 +0800] "GET /bg-middle.png HTTP/1.1" 200 1918
10.0.0.1 - - [21/Sep/2022:22:05:06 +0800] "GET /bg-nav.png HTTP/1.1" 200 1401
10.0.0.1 - - [21/Sep/2022:22:05:06 +0800] "GET /favicon.ico HTTP/1.1" 200 21630
10.0.0.1 - - [21/Sep/2022:22:16:53 +0800] "GET / HTTP/1.1" 200 11230
{"clientip":"10.0.0.1","ClientUser":"-","authenticated":"-","AccessTime":"[21/Sep/2022:22:27:49 +0800]","method":"GET / HTTP/1.1","status":"200","SendBytes":"11230","Query;string":"","partner":"-","AgentVersion":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.134 Safari/537.36 Edg/103.0.1264.77"}
[root@rocky8 tomcat]# yum -y install jq #安装jq工具转换成json格式
[root@rocky8 tomcat]# cd
[root@rocky8 ~]# vim test.txt #把日志导入test.txt
[root@rocky8 ~]# cat test.txt
{"clientip":"10.0.0.1","ClientUser":"-","authenticated":"-","AccessTime":"[21/Sep/2022:22:27:54 +0800]","method":"GET / HTTP/1.1","status":"200","SendBytes":"11230","Query;string":"","partner":"-","AgentVersion":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.134 Safari/537.36 Edg/103.0.1264.77"}
[root@rocky8 ~]# cat test.txt |jq #用jq转换
{
"clientip": "10.0.0.1",
"ClientUser": "-",
"authenticated": "-",
"AccessTime": "[21/Sep/2022:22:27:54 +0800]",
"method": "GET / HTTP/1.1",
"status": "200",
"SendBytes": "11230",
"Query;string": "",
"partner": "-",
"AgentVersion": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.134 Safari/537.36 Edg/103.0.1264.77"
}
本文来自博客园,作者:大雨转暴雨,转载请注明原文链接:https://www.cnblogs.com/wdy001/p/16721042.html