[转载]Tomcat部署与配置
转载来源:
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. 分析负载高的线程栈都是什么业务操作。优化程序并处理问题
[转载]Tomcat部署与配置的更多相关文章
- 转载 Tomcat集群配置学习篇-----分布式应用
Tomcat集群配置学习篇-----分布式应用 现目前基于javaWeb开发的应用系统已经比比皆是,尤其是电子商务网站,要想网站发展壮大,那么必然就得能够承受住庞大的网站访问量:大家知道如果服务器访问 ...
- Tomcat 部署及配置
下载,解压tomcat.jdk 1.解压,链接tomcat.jdk tar xf jdk-8u161-linux-x64.tar.gz .tar.gz .0_161/ /usr/local/ .0_1 ...
- springboot 通过 tomcat 部署的配置
spring-boot 有一个主类,是可以直接 run,然后就可以访问了,但是如果我们想像传统的那种 web 项目一样部署在 tomcat 里,要怎么配置呢.我们一起来看下. pom.xml 里添加如 ...
- web项目脱离Eclipse在Tomcat部署并配置Eclipse调试
简单来说,把WEB项目打成war包后放到webapps目录下启动tomcat便部署成功了,但是因为与Eclipse没有关联,故而无法Debug调试代码.这时在Tomcat的catalina.sh脚本里 ...
- 转载:tomcat实现热部署的配置
tomcat实现热部署的配置 我们实现热部署后,自然就可以通过maven操作tomcat了,所以就需要maven取得操作tomcat的权限,现在这一步就是配置tomcat的可操作权限. 进入tom ...
- window xp Apache与Tomcat集群配置--转载
转载地址:http://www.cnblogs.com/obullxl/archive/2011/06/09/apache-tomcat-cluster-config.html 一. 环境说明 Win ...
- JDK配置及tomcat部署
JDK配置及tomcat部署 2017-09-30-17:54:03 个人原创博客,转载请注明出处,作者,否则依法追究责任,文中引用了一些百度经验的图片,特此注明. 一,配置JDK:1.8.0_66版 ...
- Tomcat安装、配置和部署笔记
首先从Apache的官方网站(http://tomcat.apache.org/)下载Tomcat.有安装版和解压版两种,我个人喜欢用解压版. Tomcat安装(绿色版安装) 1.将下载的Tomcat ...
- Tomcat安装、配置和部署
首先从Apache的官方网站(http://tomcat.apache.org/)下载Tomcat.有安装版和解压版两种,本文档介绍绿色版安装方法. 一.Tomcat安装(绿色版安装) 1.将下载的T ...
随机推荐
- redis命令大全参考手册
redis功能强大,支持数据类型丰富,以下是redis操作命令大全,基本上涵盖了redis所有的命令,并附有解释说明,大家可以收藏.参考,你一定要知道的是:redis的key名要区分大小写,在redi ...
- Qt 编程指南10 QImage Mat QPixmap转换
//示例 pushButtonOpenPicBig按钮clicked单击动作触发 void Qt_Window::on_pushButtonOpenPicBig_clicked() { strin ...
- Mybatis集成(转)
文章转自http://blog.csdn.net/l454822901/article/details/51829653 什么是Mybatis MyBatis 本是apache的一个开源项目iBati ...
- 多线程操作的方法(sleep,)setPriority(Thread.MIN_PRIORITY);yield();
在多线程中所有的操作方法都是从Thread类开始的,所有的操作基本都在Thread类中. 第一取得线程名字 a,在Thread类中,可以通过getName()方法,获得线程的名字,可以通过setNam ...
- PAT A1104 Sum of Number Segments (20 分)——数学规律,long long
Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence. For exam ...
- PAT A1149 Dangerous Goods Packaging (25 分)——set查找
When shipping goods with containers, we have to be careful not to pack some incompatible goods into ...
- 工具 Sublime日志染色
工欲善其事,必先利其器. Preferences -> Browse Packages...
- java从命令行接受多个数字求和输出
一·设计思路 1.定义一个整型变量sum,用于接收和 2.利用循环将命令行数字求和 3.输出参数个数以及参数之和 二·流程图 三·程序源代码 public class JavaAppArguments ...
- python 知识2
零. type()函数怎么使用 type()的使用方法:type(对象)type()是接收一个对象当做参考,之后反回对象的相应类型.>>>type(1)<type 'int'& ...
- Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package
1.在搭建SpringBoot框架时碰到的问题. ** WARNING ** : Your ApplicationContext is unlikely to start due to a @Comp ...