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是否正確?
[否]: yjava

輸入 <tomcat> 的密鑰口令
(若是和密鑰庫口令相同, 按回車):
[root@localhost ~]# vim /usr/local/tomcat8/conf/server.xmllinux

  <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>web

[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.apache

Tocmat虛擬主機
基於域名的虛擬主機
[root@localhost ~]# vim /usr/local/tomcat8/conf/server.xml     //須要兩個Hostbootstrap

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

<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" />tomcat

</Host>bash

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

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

<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

 

 

 

 

相關文章
相關標籤/搜索