1.what‘s this?
Stable performance, free Java web application server!
相关:
Java,Javac,JVM,JRE,JDK,Java SE,Java EE,JSP
基本逻辑:
xxx.java-->Javac-->xxx.class-->JVM-->MachineLanguage-->runing
一点儿总结:
一个java程序只有一个jvm进程,但可以开多线程。
tomcat通过servlet处理响应给客户端。
tomcat是JDK+JSP实现的Java EE,servlet的容器。
Tomcat相关组件:
server: 顶级类,一个server算一个tomcat实例。
service: server的子组件,用于封装connector和containor。
connector: 连接器,接收客户端请求处理返回给客户端。
containor: 容器,分析处理请求返回数据给connector。
engine: 定义servlet引擎。
host: 定义虚拟主机。
context: 定义webapp。
wrapper: 真正的servlet容器。
2.why use it?
一些优点:
开源,免费,配置简单,性能稳定。
一些缺点:
处理静态资源不如nginx,apache。
3.How to use it?
3.1.安装部署
1.安装java环境(jdk1.8.0_161)
流程:下载省略,解压,软链,配置环境变量。
tar -zxvf jdk-8u161-linux-x64.tar.gz -C /usr/local/
ln -s /usr/local/jdk1.8.0_161 /usr/local/jdk
sed -i.ori '$a export JAVA_HOME=/usr/local/jdk
export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH' /etc/profile
检查是否安装完毕。
source /etc/profile
[root@izbp1gp76zixjk79d0yfaxz bin]# java -version
java version "1.8.0_161"
Java(TM) SE Runtime Environment (build 1.8.0_161-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.161-b12, mixed mode)
2.安装tomcat
流程:下载,解压,软链,配置环境变量。
wget https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.31/bin/apache-tomcat-8.5.31.tar.gz
tar -zxvf apache-tomcat-8.5.31.tar.gz -C /usr/local
ln -s /usr/local/apache-tomcat-8.5.31 /usr/local/tomcat
echo 'export TOMCAT_HOME=/usr/local/tomcat' >> /etc/profile
source /etc/profile
3.启动脚本:
方法一
#!/bin/bash
# Tomcat init script for linux.
#
# chkconfig: 2345 96 14
# description: The Apache Tomcat servlet/JSP container.
. /etc/profile
CATALINA_HOME=/usr/local/tomcat
export CATALINA_HOME
case $1 in
start)
exec $CATALINA_HOME/bin/catalina.sh start;;
stop)
exec $CATALINA_HOME/bin/catalina.sh stop;;
restart)
$CATALINA_HOME/bin/catalina.sh stop
sleep 2
exec $CATALINA_HOME/bin/catalina.sh start;;
*)
echo "Usage: `basename $0` {start|stop|restart}"
exit 1
;;
esac
方法二
#!/bin/bash
# chkconfig: 2345 74 44
# description: Tomcat is a Java servlet container.
. /etc/profile
TOMCAT_HOME=/usr/local/tomcat8_1
start(){
TOMCAT_PID=`ps -ef | grep "$TOMCAT_HOME" | grep -v "grep" | awk '{print $2}'`
if [ -z $TOMCAT_PID ];then
/bin/bash $TOMCAT_HOME/bin/startup.sh
else
echo "$0 is running"
fi
}
stop(){
TOMCAT_PID=`ps -ef | grep "$TOMCAT_HOME" | grep -v "grep" | awk '{print $2}'`
if [ -z $TOMCAT_PID ];then
echo "$0 is not running"
else
echo "shutdown down $0"
kill -9 "$TOMCAT_PID" && echo "PID $TOMCAT_PID killed"
fi
}
status(){
TOMCAT_PID=`ps -ef | grep "$TOMCAT_HOME" | grep -v "grep" | awk '{print $2}'`
if [ -z $TOMCAT_PID ];then
echo "$0 is not running."
else
echo "$0 is running PID is $TOMCAT_PID"
fi
}
case $1 in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
stop
start
;;
*)
echo "Usage:$0 {start|stop|status|restart}."
;;
esac
4.tomcat的目录结构
.
├── bin //功能脚本
├── conf //配置文件
│ └── Catalina
│ └── localhost
├── lib //存放web能访问的jar包
├── logs //web应用日志
├── temp //临时文件
├── webapps //web应用目录
│ ├── docs
│ │ ├── api
│ │ ├── appdev
│ │ │ └── sample
│ │ │ ├── docs
│ │ │ ├── src
│ │ │ │ └── mypackage
│ │ │ └── web
│ │ │ ├── images
│ │ │ └── WEB-INF
│ │ ├── architecture
│ │ │ ├── requestProcess
│ │ │ └── startup
│ │ ├── config
│ │ ├── elapi
│ │ ├── funcspecs
│ │ ├── images
│ │ │ └── fonts
│ │ ├── jspapi
│ │ ├── servletapi
│ │ ├── tribes
│ │ ├── WEB-INF
│ │ └── websocketapi
│ ├── examples
│ │ ├── jsp
│ │ │ ├── async
│ │ │ ├── cal
│ │ │ ├── checkbox
│ │ │ ├── colors
│ │ │ ├── dates
│ │ │ ├── error
│ │ │ ├── forward
│ │ │ ├── images
│ │ │ ├── include
│ │ │ ├── jsp2
│ │ │ │ ├── el
│ │ │ │ ├── jspattribute
│ │ │ │ ├── jspx
│ │ │ │ ├── misc
│ │ │ │ ├── simpletag
│ │ │ │ └── tagfiles
│ │ │ ├── jsptoserv
│ │ │ ├── num
│ │ │ ├── plugin
│ │ │ │ └── applet
│ │ │ ├── security
│ │ │ │ └── protected
│ │ │ ├── sessions
│ │ │ ├── simpletag
│ │ │ ├── snp
│ │ │ ├── tagplugin
│ │ │ └── xml
│ │ ├── servlets
│ │ │ ├── images
│ │ │ └── nonblocking
│ │ ├── WEB-INF
│ │ │ ├── classes
│ │ │ │ ├── async
│ │ │ │ ├── cal
│ │ │ │ ├── checkbox
│ │ │ │ ├── colors
│ │ │ │ ├── compressionFilters
│ │ │ │ ├── dates
│ │ │ │ ├── error
│ │ │ │ ├── examples
│ │ │ │ ├── filters
│ │ │ │ ├── jsp2
│ │ │ │ │ └── examples
│ │ │ │ │ ├── el
│ │ │ │ │ └── simpletag
│ │ │ │ ├── listeners
│ │ │ │ ├── nonblocking
│ │ │ │ ├── num
│ │ │ │ ├── sessions
│ │ │ │ ├── util
│ │ │ │ ├── validators
│ │ │ │ └── websocket
│ │ │ │ ├── chat
│ │ │ │ ├── drawboard
│ │ │ │ │ └── wsmessages
│ │ │ │ ├── echo
│ │ │ │ └── snake
│ │ │ ├── jsp
│ │ │ │ └── applet
│ │ │ ├── jsp2
│ │ │ ├── lib
│ │ │ └── tags
│ │ └── websocket
│ ├── host-manager
│ │ ├── images
│ │ ├── META-INF
│ │ └── WEB-INF
│ │ └── jsp
│ ├── manager
│ │ ├── images
│ │ ├── META-INF
│ │ └── WEB-INF
│ │ └── jsp
│ └── ROOT
│ └── WEB-INF
└── work //存放JSP编译出的.java,.class文件
└── Catalina
└── localhost
├── docs
├── examples
├── host-manager
├── manager
│ └── org
│ └── apache
│ └── jsp
│ └── WEB_002dINF
│ └── jsp
└── ROOT
└── org
└── apache
└── jsp
3.2.配置管理
Server.xml包含
server
service
connector
containor
engine
host
context
wrapper
<?xml version="1.0" encoding="UTF-8"?>
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
<GlobalNamingResources>
<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>
<Service name="Catalina">
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
<Engine name="Catalina" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
</Engine>
</Service>
</Server>
3.3.站点部署
基本方法:
1.直接放代码。
2.放war包。
3.4.多实例集群部署
1.多实例
cd /usr/local
cp -a tomcat tomcat8_1
cp -a tomcat tomcat8_2
sed -i '22s#8005#8011#;69s#8080#8081#;123s#appBase=".*"# appBase="/data/wwwroot"#' tomcat8_1/conf/server.xml
sed -i '22s#8005#8012#;69s#8080#8082#;123s#appBase=".*"# appBase="/data/wwwroot"#' tomcat8_2/conf/server.xml
for i in {1..2};do /usr/local/tomcat8_$i/bin/startup.sh;done
mkdir /data/wwwroot -p
cp tomcat/webapps/manager/index.jsp /data/wwwroot/
2.集群nginx反向代理
[root@izbp1gp76zixjk79d0yfaxz conf.d]# cat tomcat.conf
upstream web-pools{
server 127.0.0.1:8080;
server 127.0.0.1:8081;
server 127.0.0.1:8082;
}
server {
listen 80;
server_name boyang.geek-zoo.net;
location / {
root html;
index index.jsp index.html index.htm;
proxy_pass http://web-pools;
}
}
3.5.监控
zabbix配置监控tomcat
3.6.安全优化及性能优化
3.6.1.安全优化
降权启动
telnet管理端口保护
ajp连接端口保护
禁用管理端
3.6.2.性能优化
屏蔽dns查询enableLookups="false"
JAVA_OPTS="-Djava.awt.headless=true -Dfile.encoding=UTF-8 -server -Xms1024m -Xmx1024m -XX:NewSize=512m -XX:MaxNewSize=512m -XX:PermSize=512m -XX:MaxPermSize=512m"