Tomcat
目录
- Tomcat
LNMT
Linux Nginx MySQL Tomcat (java)
Tomcat Resin:用来安装java程序代码 运行
JVM java Virtual machine Java虚拟机
JDK java开发环境
优点:
Tomcat运行时占用的系统资源小,扩展性好,支持负载均衡与邮件服务等开发应用系统常用的功能;
Tomcat是一个开源的web服务器 ;
Tomcat是一个小型的轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP程序的首选。
1. Tomcat快速安装yum
1.准备Java基础环境
[root@nginx-web02]# yum install java -y
mkdir /server && cd /server
2.下载并安装Tomcat服务
wget http://mirror.bit.edu.cn/apache/tomcat/tomcat-9/v9.0.31/bin/apache-tomcat-9.0.12.tar.gz
[root@nginx-web02 server]# tar zxvf apache-tomcat-9.0.31.tar.gz
[root@nginx-web02 server]# cd apache-tomcat-9.0.31
3.启动Tomcat服务
[root@nginx-web02 apache-tomcat-9.0.31]# ./bin/startup.sh
[root@nginx-web02 server]# netstat -anpt | grep "java"
tcp6 0 0 :::8080 :::* LISTEN 1643/java
tcp6 0 0 127.0.0.1:8005 :::* LISTEN 1643/java
4.测试http://127.0.0.1:8080
2. Tomcat安装源码包
1. 安装jdk
解压
tar zxvf jdk-8u191-linux-x64.gz -C /application/
[root@tomcat-server tools]# ln -s /application/jdk1.7.0_65/ /application/jdk
[root@tomcat-server application]# ll
总用量 0
lrwxrwxrwx 1 root root 25 3月 3 14:18 jdk -> /application/jdk8u191/
drwxr-xr-x 8 10 143 233 6月 17 2014 jdk8u191
配置环境变量
[root@tomcat-server ~]# cat >>/etc/profile<<'EOF' //EOF加单引号,不解析符号
export JAVA_HOME=/application/jdk
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tool.tar
EOF
[root@tomcat-server ~]# source /etc/profile
[root@tomcat-server ~]# java -version
2. 安装tomcat
[root@tomcat-server ~]# tar zxvf apache-tomcat-9.0.31.tar.gz -C /application/
[root@tomcat-server ~]# ln -s /application/apache-tomcat-9.0.31/ /application/tomcat
echo 'export TOMCAT_HOME=/application/tomcat' >>/etc/profile
source /etc/profile
[root@tomcat-server ~]# /application/tomcat/bin/version.sh
[root@tomcat-server ~]# /application/tomcat/bin/start.sh //启动
[root@tomcat-server ~]# /application/tomcat/bin/shutdown.sh //关闭
3. tomcat必备姿势
1.tomcat目录结构
[root@tomcat-server tomcat]# ll
总用量 124
drwxr-x--- 2 root root 4096 3月 3 14:31 bin
-rw-r----- 1 root root 18982 2月 6 03:36 BUILDING.txt
drwx------ 2 root root 238 2月 6 03:36 conf
-rw-r----- 1 root root 5409 2月 6 03:36 CONTRIBUTING.md
drwxr-x--- 2 root root 4096 3月 3 14:31 lib
-rw-r----- 1 root root 57092 2月 6 03:36 LICENSE
drwxr-x--- 2 root root 6 2月 6 03:32 logs
-rw-r----- 1 root root 2333 2月 6 03:36 NOTICE
-rw-r----- 1 root root 3255 2月 6 03:36 README.md
-rw-r----- 1 root root 6898 2月 6 03:36 RELEASE-NOTES
-rw-r----- 1 root root 16262 2月 6 03:36 RUNNING.txt
drwxr-x--- 2 root root 30 3月 3 14:31 temp
drwxr-x--- 7 root root 81 2月 6 03:34 webapps
drwxr-x--- 2 root root 6 2月 6 03:32 work
bin tomcat管理命令 startup.sh shutdown.sh catalina.sh
- cactlina.sh 是startup.sh shtdown.sh都会调用
- 以后对于tomcat优化(jvm优化 设置最大内存 最小内存)和配置监控功能
conf配置
- server.xml(nginx.conf)tomcat主配置文件
- web.xml (增加 插件 优化)
- tomcat-users.xml (tomcat管理配置文件)
logs日志
cactlina.out tomcat的核心日志详细 持续变大 持续增加
cacalina.2019-08-20.log cacalina.out的切割日志
localhost_access_log.2020-03-03.txt tomcat 访问日志
[root@tomcat-server /]# logrotate ^C //对日志的定时切割
[root@tomcat-server /]# cat /etc/logrotate.d/syslog
/var/log/cron
/var/log/maillog
/var/log/messages
/var/log/secure
/var/log/spooler
{
missingok
sharedscripts
postrotate
/bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true
endscript
}
- webapps tomcat的站短目录
2. 配置tomcat管理功能
vim application/tomcat/conf/tomcat-users.xml
<role rolename="admin-gui"/>
<role rolename="host-gui"/>
<role rolename="manager-gui"/>
<user username="admin" password="admin" roles="admin-gui,host-gui,manager-gui"/>
[root@tomcat-server /]# vim application/tomcat/conf/tomcat-users.xml
[root@tomcat-server /]# /application/tomcat/bin/shutdown.sh
Using CATALINA_BASE: /application/tomcat
Using CATALINA_HOME: /application/tomcat
Using CATALINA_TMPDIR: /application/tomcat/temp
Using JRE_HOME: /usr
Using CLASSPATH: /application/tomcat/bin/bootstrap.jar:/application/tomcat/bin/tomcat-juli.jar
[root@tomcat-server /]# ps -ef | grep java
root 19025 12453 0 16:15 pts/0 00:00:00 grep --color=auto java
[root@tomcat-server /]# /application/tomcat/bin/startup.sh
Using CATALINA_BASE: /application/tomcat
Using CATALINA_HOME: /application/tomcat
Using CATALINA_TMPDIR: /application/tomcat/temp
Using JRE_HOME: /usr
Using CLASSPATH: /application/tomcat/bin/bootstrap.jar:/application/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@tomcat-server /]# netstat -anpt | grep "8080"
tcp6 0 0 :::8080 :::* LISTEN 19051/java
tcp6 0 0 ::1:59898 ::1:8080 TIME_WAIT -
http://172.16.20.107:8080/
3.部署jpress
mysql配置
mysql> create database jpress;
Query OK, 1 row affected (0.00 sec)
mysql> grant all on jpress.* to 'jpress'@'172.16.20.%' identified by '123.com.cn';
Query OK, 0 rows affected (0.00 sec)
tomcat-web配置
cp jpress.war /application/tomcat/webapps/
http://172.16.20.107:8080/jpress
[root@tomcat-server wars]# cat /application/tomcat/webapps/jpress/WEB-INF/classes/db.properties
#Auto create by JPress
#Tue Mar 03 16:45:37 CST 2020
db_name=jpress
db_host_port=3306
db_tablePrefix=jpress_
db_host=172.16.20.109
db_password=123.com.cn
db_user=jpress
4.tomcat主配置文件
#shutdown 端口 连接到这个端口并输入后面的暗号,就可以把tomcat关闭
<Server port="8005" shutdown="SHUTDOWN">
#tomcat管理端
<GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
#优化tomcat
<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
maxThreads="150" minSpareThreads="4"/>
#tomcat web功能
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
#8009 ajp 工作方式
<Connector protocol="AJP/1.3"
address="::1"
port="8009"
redirectPort="8443" />
# host 虚拟主机
# host name == server_name
# appBase == root
# unpackWARs 自动解压
# autoDeploy 自动部署
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<!-- SingleSignOn valve, share authentication between web applications
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.authenticator.SingleSignOn" />
-->
<!-- Access log processes all example.
Documentation at: /docs/config/valve.html
Note: The pattern used is equivalent to using pattern="common" -->
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
# tomcat 访问日志的格式
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
5.tomcat 多实例
[root@tomcat-server tools]# tar zxvf apache-tomcat-9.0.31.tar.gz
[root@tomcat-server tools]# cp -r apache-tomcat-9.0.31 tomcat_8081
[root@tomcat-server tools]# cp -r apache-tomcat-9.0.31 tomcat_8082
[root@tomcat-server tools]# sed -i 's#8080#8081#g' tomcat_8081/conf/server.xml
[root@tomcat-server tools]# sed -i 's#8005#8006#g' tomcat_8081/conf/server.xml
[root@tomcat-server tools]# sed -i 's#8009#8010#g' tomcat_8081/conf/server.xml
[root@tomcat-server tools]# sed -i 's#8080#8082#g' tomcat_8082/conf/server.xml
[root@tomcat-server tools]# sed -i 's#8005#8007#g' tomcat_8082/conf/server.xml
[root@tomcat-server tools]# sed -i 's#8009#8011#g' tomcat_8082/conf/server.xml
[root@tomcat-server application]# /application/tomcat_8081/bin/startup.sh
[root@tomcat-server application]# /application/tomcat_8082/bin/startup.sh
进程已路径来区分
# 注意 书写tomcat相关脚本(启动 重启 监控)
# 精确过滤
[root@tomcat-server application]# ps -ef | grep "/tomcat/"
root 20333 1 1 16:46 pts/0 00:02:50 /usr/bin/java -Djava.util.logging.config.file=/application/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -classpath /application/tomcat/bin/bootstrap.jar:/application/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/application/tomcat -Dcatalina.home=/application/tomcat -Djava.io.tmpdir=/application/tomcat/temp org.apache.catalina.startup.Bootstrap start
root 25309 12453 0 19:32 pts/0 00:00:00 grep --color=auto /tomcat/
tomcat监控
简单命令
现成脚本
通过zabbix进行监控
- 自定义监控
- 通过jmx监控
jsp java ps
- -lvm
jmap 导出jvm信息
jstackj stat 导出java进程
故障案例:系统负载过高,发现tomcat占用cpu较高
zabbix通过jmx监控tomcat (tomcat 需要开启监控功能)
#修改
catalina.sh
CATALINA_OPTS="$CATALINA_OPTS
-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=12345
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false
-Djava.rmi.server.hostname=10.0.0.17"
#说明:
CATALINA_OPTS="$CATALINA_OPTS #修改tomcat启动参数
-Dcom.sun.management.jmxremote #开启tomcat远程管理功能
-Dcom.sun.management.jmxremote.port=12345 #远程管理功能除了12345端口还会随机生成2个端口 开放所有端口(单独设置 安全组规则)
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false
-Djava.rmi.server.hostname=10.0.0.17" #指定本机的ip地址
""
- jsp/top/htop 精确定位那个java进程导致
- jstack 导出java详细
- cacalina.out日志
- jmap 导出jvm信息 通过软件mat进行分析