Tomcat

Posted zhiyuan-yu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Tomcat相关的知识,希望对你有一定的参考价值。

Tomcat配置SSL加密
[root@localhost ~]# keytool -genkeypair -alias tomcat -keyalg RSA -keystore /usr/local/tomcat8/keystore
输入密钥库口令:
再次输入新口令:
您的名字与姓氏是什么?
[Unknown]: crushlinux
您的组织单位名称是什么?
[Unknown]: cloud
您的组织名称是什么?
[Unknown]: cloud
您所在的城市或区域名称是什么?
[Unknown]: beijing
您所在的省/市/自治区名称是什么?
[Unknown]: beijing
该单位的双字母国家/地区代码是什么?
[Unknown]: CN
CN=crushlinux, OU=cloud, O=cloud, L=beijing, ST=beijing, C=CN是否正确?
[否]: y

输入 <tomcat> 的密钥口令
(如果和密钥库口令相同, 按回车):
[root@localhost ~]# vim /usr/local/tomcat8/conf/server.xml

  <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
  maxThreads="150" SSLEnabled="true" scheme="https" secure="true" clientAuth="false" ssIProtocol="TLS" keystoreFile="/usr/local/tomcat8/keystore" keystorePass="123456">
    <!--
    <SSLHostConfig>
    <Certificate certificateKeystoreFile="conf/localhost-rsa.jks"
    type="RSA" />
    </SSLHostConfig>
    -->
  </Connector>

[root@localhost ~]# /usr/local/tomcat8/bin/startup.sh
Using CATALINA_BASE: /usr/local/tomcat8
Using CATALINA_HOME: /usr/local/tomcat8
Using CATALINA_TMPDIR: /usr/local/tomcat8/temp
Using JRE_HOME: /usr/local/java
Using CLASSPATH: /usr/local/tomcat8/bin/bootstrap.jar:/usr/local/tomcat8/bin/tomcat-juli.jar
Tomcat started.

Tocmat虚拟主机
基于域名的虚拟主机
[root@localhost ~]# vim /usr/local/tomcat8/conf/server.xml     //需要两个Host

<Host name="www.a.com" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Context docBase="/web/a" path="" reloadable="flase"/>

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t &quot;%r&quot; %s %b" />

</Host>

<Host name="www.b.com" appBase="webapps"
unpackWARs="true" autoDeploy="true">

<Context docBase="/web/b" path="" reloadable="flase"/>

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t &quot;%r&quot; %s %b" />

</Host>

创建jsp文件

[root@localhost ~]# mkdir /web/a
[root@localhost ~]# mkdir /web/b
[root@localhost ~]# vim /web/a/index.jsp
www.a.com
[root@localhost ~]# vim /web/b/index.jsp
www.b.com
[root@localhost ~]# /usr/local/tomcat8/bin/startup.sh
Using CATALINA_BASE: /usr/local/tomcat8
Using CATALINA_HOME: /usr/local/tomcat8
Using CATALINA_TMPDIR: /usr/local/tomcat8/temp
Using JRE_HOME: /usr/local/java
Using CLASSPATH: /usr/local/tomcat8/bin/bootstrap.jar:/usr/local/tomcat8/bin/tomcat-juli.jar
Tomcat started.

技术图片

 

 技术图片

 

 

基于端口的虚拟主机
[root@localhost ~]# vim /usr/local/tomcat8/conf/server.xml         //基于端口要两个<Service> 所以 ..........
<Service name="Catalina1">

<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="www.a.com" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Context docBase="/web/a" path="" reloadable="flase"/>

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t &quot;%r&quot; %s %b" />

</Host>

</Engine>
</Service>

<Service name="Catalina2">

<Connector port="8090" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Connector port="8010" 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="www.a.com" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Context docBase="/web/b" path="" reloadable="flase"/>

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t &quot;%r&quot; %s %b" />

</Host>

</Engine>
</Service>

基于IP地址的虚拟主机
服务器使用多网卡配置多个IP地址,然后配置WEB服务器,把多个网站绑定在不同的IP上。(该方式浪费IP资源,tomcat不支持该方式)

Tomcat多实例配置

[root@localhost ~]# tar xf apache-tomcat-8.5.40.tar.gz
[root@localhost ~]# mv apache-tomcat-8.5.40 /usr/local/
[root@localhost ~]# mkdir -pv /usr/local/tomcat_instance/tomcat1
mkdir: 已创建目录 "/usr/local/tomcat_instance"
mkdir: 已创建目录 "/usr/local/tomcat_instance/tomcat1"
[root@localhost ~]# cp -R /usr/local/apache-tomcat-8.5.40/conf/ /usr/local/tomcat_instance/tomcat1/
[root@localhost ~]# mkdir /usr/local/tomcat_instance/tomcat1/bin
[root@localhost ~]# touch /usr/local/tomcat_instance/tomcat1/bin/startup.sh
[root@localhost ~]# chmod +x /usr/local/tomcat_instance/tomcat1/bin/startup.sh
[root@localhost ~]# vim /usr/local/tomcat_instance/tomcat1/bin/startup.sh

  #!/bin/bash
  export CATALINA_HOME="/usr/local/apache-tomcat-8.5.40"
  export CATALINA_BASE="/usr/local/tomcat_instance/tomcat1"
  export CATALINA_TMPDIR="$CATALINA_BASE/temp"
  export CATALINA_PID="$CATALINA_BASE/bin/tomcat.pid"
  export JAVA_OPTS="-server -Xms1024m -Xmx1024m -Djava.awt.headless=true -Dtomcat.name=tomcat1"

  #创建logs目录
  if [ ! -d "$CATALINA_BASE/logs" ];then
  mkdir $CATALINA_BASE/logs
  fi

  #创建temp目录
  if [ ! -d "$CATALINA_BASE/temp" ];then
  mkdir $CATALINA_BASE/temp
  fi

  #调用tomcat启动脚本
  bash $CATALINA_HOME/bin/startup.sh "$@"

[root@localhost ~]# vim /usr/local/tomcat_instance/tomcat1/bin/shutdown.sh

  #!/bin/bash
  export CATALINA_HOME="/usr/local/apache-tomcat-8.5.40"
  export CATALINA_BASE="/usr/local/tomcat_instance/tomcat1"
  export CATALINA_TMPDIR="$CATALINA_BASE/temp"
  export CATALINA_PID="$CATALINA_BASE/bin/tomcat.pid"

  bash $CATALINA_HOME/bin/shutdown.sh "$@"

[root@localhost ~]# chmod +x /usr/local/tomcat_instance/tomcat1/bin/shutdown.sh
[root@localhost ~]# mkdir /usr/local/tomcat_instance/tomcat1/webapps/ROOT -p
[root@localhost ~]# vim /usr/local/tomcat_instance/tomcat1/webapps/ROOT/index.jsp

<h1>tomcat1</h1>

[root@localhost ~]# /usr/local/tomcat_instance/tomcat1/bin/startup.sh
Using CATALINA_BASE: /usr/local/tomcat_instance/tomcat1
Using CATALINA_HOME: /usr/local/apache-tomcat-8.5.40
Using CATALINA_TMPDIR: /usr/local/tomcat_instance/tomcat1/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/apache-tomcat-8.5.40/bin/bootstrap.jar:/usr/local/apache-tomcat-8.5.40/bin/tomcat-juli.jar
Using CATALINA_PID: /usr/local/tomcat_instance/tomcat1/bin/tomcat.pid
Tomcat started.
[root@localhost ~]# netstat -lnpt | grep java
tcp6 0 0   :::8080        :::*   LISTEN    1985/java
tcp6 0 0   127.0.0.1:8005      :::*    LISTEN    1985/java
tcp6 0 0   :::8009       :::*   LISTEN    1985/java

[root@localhost ~]# cd /usr/local/tomcat_instance/
[root@localhost tomcat_instance]# ls
tomcat1
[root@localhost tomcat_instance]# cp -r tomcat1 tomcat2
[root@localhost tomcat_instance]# cp -r tomcat1 tomcat3
[root@localhost tomcat_instance]# ls
tomcat1 tomcat2 tomcat3
[root@localhost tomcat_instance]# cd tomcat2
[root@localhost tomcat2]# ls
bin conf logs temp webapps work
[root@localhost tomcat2]# vim bin/startup.sh
将文件中的tomcat1改成tomcat2
[root@localhost tomcat2]# vim bin/shutdown.sh
将文件中的tomcat1改成tomcat2
[root@localhost tomcat2]# rm -rf bin/tomcat.pid
[root@localhost tomcat2]# vim conf/server.xml
修改端口号不要和tomcat1的重复
22 <Server port="8003" shutdown="SHUTDOWN">

69 <Connector port="8082" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
116 <Connector port="8011" protocol="AJP/1.3" redirectPort="8443" />


[root@localhost tomcat2]# vim webapps/ROOT/index.jsp

 

<h1>tomcat2</h1>

[root@localhost tomcat2]# /usr/local/tomcat_instance/tomcat2/bin/startup.sh
Using CATALINA_BASE: /usr/local/tomcat_instance/tomcat2
Using CATALINA_HOME: /usr/local/apache-tomcat-8.5.40
Using CATALINA_TMPDIR: /usr/local/tomcat_instance/tomcat2/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/apache-tomcat-8.5.40/bin/bootstrap.jar:/usr/local/apache-tomcat-8.5.40/bin/tomcat-juli.jar
Using CATALINA_PID: /usr/local/tomcat_instance/tomcat2/bin/tomcat.pid
Tomcat started.
tomcat3的同tomcat2一样更改
[root@localhost tomcat3]# /usr/local/tomcat_instance/tomcat3/bin/startup.sh
Using CATALINA_BASE: /usr/local/tomcat_instance/tomcat3
Using CATALINA_HOME: /usr/local/apache-tomcat-8.5.40
Using CATALINA_TMPDIR: /usr/local/tomcat_instance/tomcat3/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/apache-tomcat-8.5.40/bin/bootstrap.jar:/usr/local/apache-tomcat-8.5.40/bin/tomcat-juli.jar
Using CATALINA_PID: /usr/local/tomcat_instance/tomcat3/bin/tomcat.pid
Tomcat started.
[root@localhost tomcat3]# netstat -lnpt |grep java
tcp6 0 0 :::8011     :::*     LISTEN 2546/java
tcp6 0 0 :::8080      :::*      LISTEN 1985/java
tcp6 0 0 :::8081      :::*    LISTEN 2384/java
tcp6 0 0 :::8082     :::*      LISTEN 2546/java
tcp6 0 0 127.0.0.1:8003 :::*     LISTEN 2546/java
tcp6 0 0 127.0.0.1:8004 :::*     LISTEN 2384/java
tcp6 0 0 127.0.0.1:8005 :::*     LISTEN 1985/java
tcp6 0 0 :::8009      :::*     LISTEN 1985/java
tcp6 0 0 :::8010     :::*      LISTEN 2384/java

技术图片

 

 技术图片

 

 技术图片

 

以上是关于Tomcat的主要内容,如果未能解决你的问题,请参考以下文章

TomcatTomcat 原理架构

TomcatTomcat 用法 问题 整理

TomcatTomcat性能分析

TomcatTomcat的类加载机制

TomcatTomcat 介绍及使用教程

TomcatTomcat 介绍及使用教程