一、配置Tomcat服务器
1.根据教程在Centos 7上配置tomcat8服务器
启动、关闭tomcat命令:
/usr/local/myTomcat/tomcat/bin/startup.sh
/usr/local/myTomcat/tomcat/bin/shutdown.sh
2.Tomcat的开机启动、关闭
-
cd /etc/init.d
-
chmod 755 /etc/rc.d/init.d/tomcat 文件提权
-
vim tomcat 进入vim编辑界面
-
写入启动shell脚本
#!/bin/bash # This is the init script for starting up the # Jakarta Tomcat server # # chkconfig: 345 91 10 # description: Starts and stops the Tomcat daemon. # # Source function library. . /etc/rc.d/init.d/functions # Get config. . /etc/sysconfig/network # Check that networking is up. [ "${NETWORKING}" = "no" ] && exit 0 export JAVA_HOME=/usr/local/javaweb/jdk1.8.0_192 #自己的jdk安装目录 tomcat_home=/usr/local/myTomcat/tomcat #自己的tomcat安装目录 startup=$tomcat_home/bin/startup.sh shutdown=$tomcat_home/bin/shutdown.sh start(){ echo -n "Starting Tomcat service:" cd $tomcat_home $startup echo "tomcat is succeessfully started up" } stop(){ echo -n "Shutting down tomcat: " cd $tomcat_home $shutdown echo "tomcat is succeessfully shut down." } status(){ numproc=`ps -ef | grep catalina | grep -v "grep catalina" | wc -l` if [ $numproc -gt 0 ]; then echo "Tomcat is running..." else echo "Tomcat is stopped..." fi } restart(){ stop start } # See how we were called. case "$1" in start) start ;; stop) stop ;; status) status ;; restart) restart ;; *) echo $"Usage: $0 {start|stop|status|restart}" exit 1 esac
-
将文件加入到服务队列中
chkconfig --add tomcat
- 查看tomcat 文件是否加入服务列表成功
chkconfig --list
- 设置服务开机自启动
chkconfig tomcat on
3.利用Tomcat部署静态页面
../tomcat/conf/server.xml
- vim 写入
<Host name="localhost" debug="0" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
<Context path="" docBase="file" debug="0" reloadable="true" crossContext="true"/>
<Logger className="org.apache.catalina.logger.FileLogger" directory="logs" prefix="tot_log." suffix=".txt" timestamp="true"/>
</Host>
// docBase= \webapps\file\xxx.html
- 浏览器访问localhost:8080/xxx.html
4.查看日志
- cd home/tomcat/logs
//切换到日志目录
- ls
//查看日志类型
- tail -f catalina.out
//查看运行日志
- tail -n xxx -f catalina.out
//查看最近xxx行日志内容
- Ctrl+c 是退出tail命令
- cat localhost.yyyy-mm-dd.log l grep 关键词
//通过关键词搜索查看日志
- cat localhost.yyyy-mm-dd.log l grep 'xxxx-yy-zz'
//查看固定时间日志
5.日志审计
python脚本
#!/usr/bin/env python
# coding=utf-8
import re
def find_error_log(log_name):
original_file = open(log_name, encoding='utf-8')
extract = []
for line_data in original_file:
error = re.search('error', line_data, re.IGNORECASE)
exception = re.search('exception', line_data, re.IGNORECASE)
java = re.search('at java', line_data, re.IGNORECASE)
com = re.search('at com', line_data, re.IGNORECASE)
org = re.search('at org', line_data, re.IGNORECASE)
sun = re.search('at sun', line_data, re.IGNORECASE)
if error or exception or java or com or org or sun:
log_str = line_data
extract.append(log_str)
# 生成输出文档位置
errlog = 'C:\\Users\\xxx\\Desktop\\checkLog\\out\\catalinaError.txt'
new_file = open(errlog, 'w')
new_file.writelines(extract)
new_file.flush()
new_file.close()
original_file.close()
return "返回啥都行"
if __name__ == '__main__':
# 原日志位置
returnMSG = find_error_log('C:\\Users\\xxx\\Desktop\\checkLog\\into\\catalina.out-20200424')
print(returnMSG)