转载来源:
http://ibash.cc/frontend/article/2/
感觉挺好的 自己之前总是怕麻烦 其实是水平不够.
一句话介绍Tomcat
Tomcat是一个免费的开源的Web应用服务器,属于轻量级的应用服务器,具有处理HTML页面的功能;另外它还是一个Servlet和JSP容器,独立的Servlet容器是Tomcat的默认模式。
Tomcat安装
note:本文的JDK版本为1.8,Tomcat版本为8.0,安装过程均采用源码安装
部署JDK
下载页面: http://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.html
或者搜索oracle JDK download下载
- # tar zxf jdk-8u101-linux-x64.tar.gz
- # mv jdk1.8.0_101 /usr/local/jdk
- # chown -R root:root /usr/local/jdk/
- # echo "###for jdk" >> /etc/profile
- # echo "export JAVA_HOME=/usr/local/jdk" >> /etc/profile
- # echo "export PATH=$JAVA_HOME/bin:$JAVE_HOME/jre/bin:$PATH" >> /etc/profile
- # echo "export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar" >> /etc/profile
- # echo "export TOMCAT_HOME=/usr/local/tomcat" >> /etc/profile
- # source /etc/profile
- # java -version
- java version "1.8.0_101"
- Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
- Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode)
部署Tomcat
官方网址: http://tomcat.apache.org/
- # wget http://mirrors.cnnic.cn/apache/tomcat/tomcat-8/v8.0.36/bin/apache-tomcat-8.0.36.tar.gz
- # tar zxf apache-tomcat-8.0.36.tar.gz
- # mv apache-tomcat-8.0.36 /usr/local/tomcat
- # chown -R root:root /usr/local/tomcat/
- # echo "###for tomcat" >> /etc/profile
- # echo "export TOMCAT_HOME=/usr/local/tomcat" >> /etc/profile
- # source /etc/profile
启动Tomcat
- # /usr/local/tomcat/bin/startup.sh
- Using CATALINA_BASE: /usr/local/tomcat
- Using CATALINA_HOME: /usr/local/tomcat
- Using CATALINA_TMPDIR: /usr/local/tomcat/temp
- Using JRE_HOME: /usr/local/jdk
- Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
- Tomcat started.
- # ps -ef|grep tomcat
- root 2735 1 99 23:00 pts/0 00:00:11 /usr/local/jdk/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -classpath /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap start
- root 2748 2328 0 23:00 pts/0 00:00:00 grep tomcat
- # ss -lntup|grep java
- tcp LISTEN 0 1 ::ffff:127.0.0.1:8005 :::* users:(("java",2735,77))
- tcp LISTEN 0 100 :::8009 :::* users:(("java",2735,52))
- tcp LISTEN 0 100 :::8080 :::* users:(("java",2735,47))
- # /etc/init.d/iptables stop
在浏览器访问tomcat默认的站点,输入地址 192.168.1.199:8080(注意默认端口是8080)
Tomcat目录结构
- # ls -l /usr/local/tomcat
- 总用量 112
- drwxr-xr-x 2 root root 4096 7月 25 04:03 bin ==> 启动关闭脚本
- drwxr-xr-x 3 root root 4096 7月 25 04:23 conf ==>配置文件目录
- drwxr-xr-x 2 root root 4096 7月 25 04:03 lib ==> 存放库文件,jar包
- -rw-r--r-- 1 root root 57011 6月 9 21:58 LICENSE
- drwxr-xr-x 2 root root 4096 7月 25 04:04 logs ==>日志目录
- -rw-r--r-- 1 root root 1444 6月 9 21:58 NOTICE
- -rw-r--r-- 1 root root 6739 6月 9 21:58 RELEASE-NOTES
- -rw-r--r-- 1 root root 16195 6月 9 21:58 RUNNING.txt
- drwxr-xr-x 3 root root 4096 7月 25 04:23 temp ==>临时文件夹
- drwxr-xr-x 7 root root 4096 6月 9 21:56 webapps ==>站点根目录
- drwxr-xr-x 3 root root 4096 7月 25 04:04 work ==>工作时编译的.java和.class文件
Tomcat配置文件注解
- # cat /usr/local/tomcat/conf/server.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <Server port="8005" shutdown="SHUTDOWN"><!--表示一个运行于JVM的tomcat实例,该元素代表整个容器,是Tomcat实例的顶层元素,不能作为子元素出现-->
- <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" /><!--这里的xml用来配置用户的管理段-->
- </GlobalNamingResources>
- <!--服务组件,关联connector和engine,一个server内部只有一个service-->
- <Service name="Catalina"><!--配置一个叫Catalina的服务,Tomcat有两种连接类的引擎,HTTP和AJP两种-->
- <Connector port="8080" protocol="HTTP/1.1"
- connectionTimeout="20000"<!--连接的超时时间-->
- redirectPort="8443" /><!--一般用于使用Nginx做代理-->
- <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /><!--使用apache的场景大多使用AJP,两者保留一个即可-->
- <Engine name="Catalina" defaultHost="localhost"><!--引擎配置,核心组件,负责通过connector接收用户请求,并将请求转发到对应的虚拟主机host,
- defaultHost表示缺省处理的主机名-->
- <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"><!--主机配置,可以配置虚拟主机,但是不建议配置;appBase站点路径;unpackWARS自动解压war包;
- autoDeploy自动部署;这是属于热部署,默认都是true,也就是每次部署都不用重启Tomcat,生产不建议开启热部署-->
- <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>
开启Tomcat管理界面(生产环境勿开启)
- # vim /usr/loca/tomcat/conf/tomcat-users.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <tomcat-users xmlns="http://tomcat.apache.org/xml"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd"
- version="1.0">
- <role rolename="manager-gui"/><!--定义角色-->
- <role rolename="admin-gui"/>
- <user username="tomcat" password="tomcat" roles="manager-gui,admin-gui"/><!--定义用户名密码和角色-->
- </tomcat-users>
重新启动Tomcat
- # /usr/local/tomcat/bin/shutdown.sh
- Using CATALINA_BASE: /usr/local/tomcat
- Using CATALINA_HOME: /usr/local/tomcat
- Using CATALINA_TMPDIR: /usr/local/tomcat/temp
- Using JRE_HOME: /usr/local/jdk
- Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
- # /usr/local/tomcat/bin/startup.sh
- Using CATALINA_BASE: /usr/local/tomcat
- Using CATALINA_HOME: /usr/local/tomcat
- Using CATALINA_TMPDIR: /usr/local/tomcat/temp
- Using JRE_HOME: /usr/local/jdk
- Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
- Tomcat started.
此时在浏览器就可以使用用户名tomcat密码tomcat来访问管理界面了
报错403解决方法
403 Access Denied
You are not authorized to view this page.
By default the Host Manager is only accessible from a browser running on the same machine as Tomcat. If you wish to modify this restriction, you‘ll need to edit the Host Manager‘s context.xml file.
- # vim /usr/local/tomcat/conf/Catalina/localhost/manager.xml
- <Context privileged="true" antiResourceLocking="false"
- docBase="${catalina.home}/webapps/manager">
- <Valve className="org.apache.catalina.valves.RemoteAddrValve"
- allow="192\.168\.1\.102" />
- </Context>
note:路径为 安装目录/conf/[enginename]/[hostname]/manager.xml
Tomcat安全建议
1. telnet管理端口的保护
- <Server port="8005" shutdown="SHUTDOWN">
- 修改为
- <Server port="8192" shutdown="stop">
- 端口范围8000~8999,关键字SHUTDOWN要改掉
2. ajp的连接端口更改
- ajp连接端口主要用于apache的mod_jk模块(该模块可以转发JSP请求到tomcat),保护该端口的目的在于防止线下的测试流量被mod_jk转发到线上的tomcat服务器,同样端口范围为8000~8999
- <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
- 修改为
- <Connector port="8193" protocol="AJP/1.3" redirectPort="8443" />
3. 禁止管理界面,删除默认的站点目录,更改默认的站点目录
4. 使用低级别的用户权限启动tomcat
5. 启动脚本权限回收
6. 将tomcat放置于nginx后面,保证安全的同时提高性能
更改后的server.xml配置如下
- <?xml version=‘1.0‘ encoding=‘utf-8‘?>
- <Server port="8192" shutdown="stop"><!--修改telnet端口和指令-->
- <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" server="iBashWebServer"
- maxThreads="600" minSpareThreads="100" maxSpareThreads="500" acceptCount="700" enableLookups="false" /><!--修改server header,
- 增大线程数,最小空闲进程,最大空闲进程,禁止DNS解析-->
- <Connector port="8193" protocol="AJP/1.3" redirectPort="8443" /><!--修改ajp的默认端口-->
- <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">
- <Context path="" docBase="/usr/local/tomcat_webapps/" debug="0" reloadable="false" crossContext="true" ></Context>
- <!--修改tomcat站点根目录-->
- <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
- prefix="localhost_access_log" suffix=".txt"
- pattern="%h %l %u %t %r %s % b %{Referer}i %{User-Agent}i %D" resolveHosts="false" />
- </Host>
- </Engine>
- </Service>
- </Server>
Nginx整合Tomcat的几种配置介绍
1. 单纯的nginx反向代理tomcat配置
- server {
- listen 80;
- server_name www.test.org;
- location / {
- proxy_pass http://www.test.org:8080;
- }
- }
note: 将请求www.test.org:80的所有请求,直接转发给http://www.test.org:8080,相当于直接访问http://www.test.org:8080。
2. 按照请求类型配置Nginx
- server {
- listen 80;
- server_name www.test.org;
- index index.jsp;
- charset utf-8;
- access_log logs/www.test.org.access.log main;
- root /www/html/www.test.org;
- location ~ .*\.(jsp|do)?$ {
- proxy_pass http://www.test.org:8080;
- }
- location ~ .*\.(html|js|css|png|gif)?$ {
- root /usr/local/tomcat_webapps/;
- }
- }
3. 配置负载均衡
首先复制多个tomcat目录,启动多个tomcat实例,然后配置nginx
- upstream web_pools {
- server 127.0.0.1:8081;
- server 127.0.0.1:8082;
- }
- server {
- listen 80;
- server_name www.test.org;
- location / {
- root html;
- index index.jsp index.html index.htm;
- proxy_pass http://web_pools;
- }
- }
开启JMX用于监控
- # vim /usr/local/tomcat/bin/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=192.168.1.199"
- # OS specific support. $var _must_ be set to either true or false.
线上故障排除思路
1. jps获取Java进程的PID
2. jstack pid >> java.txt 导出CPU占用高的进程的线程栈
3. top -H -p PID 查看对应进程的哪个线程占用CPU过高
4. echo "obase=16; PID"|bc将线程的PID转换为16进制
5. 在第二步导出的java.txt中查找转换成为16进制的线程的PID。找到对应的线程栈。
6. 分析负载高的线程栈都是什么业务操作。优化程序并处理问题