tomcat容器部署和tomcat虛擬主機及其優化

2020-08-13 12:34:18

tomcat容器部署和tomcat虛擬主機及其優化

一、Tomcat部署

【1】上傳JDK軟體包和tomcat壓縮包並解壓

cd /opt 
mkdir tomcat
rpm -ivh jdk-8u201-linux-x64.rpm
tar zxvf apache-tomcat-9.0.16.tar.gz -C /usr/local

【2】宣告JDK全域性變數

cd /usr/java/jdk1.8.0_201-amd64/
pwd
/usr/java/jdk1.8.0_201-amd64/
vim /etc/profile
  export JAVA_HOME=/usr/java/jdk1.8.0_201-amd64
  export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
  export PATH=$JAVA_HOME/bin:$PATH
source /etc/profile

【3】檢視Java版本是否改變

java -version
   java version "1.8.0_201"
   Java(TM) SE Runtime Environment (build 1.8.0_201-b09)
   Java HotSpot(TM) 64-Bit Server VM (build 25.201-b09, mixed mode)

【4】優化命令爲系統識別

ln -s /usr/local/apache-tomcat-9.0.16/bin/shutdown.sh /usr/bin/
ln -s /usr/local/apache-tomcat-9.0.16/bin/startup.sh /usr/bin/

【5】啓動tomcat並驗證

/usr/bin/startup.sh 
netstat -natp |grep 8080

在这里插入图片描述

【6】優化啓動

vim /usr/java/jdk1.8.0_201-amd64/jre/lib/security/java.security
    # In addition, if "file:/dev/random" or "file:/dev/urandom" is
    # specified, the "NativePRNG" implementation will be more preferred than
    # SHA1PRNG in the Sun provider.
    securerandom.source=file:/dev/urandom

二、虛擬主機設定

【1】在tomcat工作目錄下建立站點

cd /usr/local//apache-tomcat-9.0.16/webapps/
mkdir kgc benet
vim kgc/index.jsp
   <h1>this is kgc page</h1>
vim benet/index.jsp
   <h1>this is benet page</h1>

【2】tomcat虛擬主機設定

vim /usr/local/apache-tomcat-9.0.16/conf/server.xml
      <Host name="www.kgc.com" appBase="/usr/local/apache-tomcat-9.0.16/webapps"
            unpackWARs="true" autoDeploy="true"
            xmlValidation="false" xmlNamespaceAware="false">
          <Context docBase="/usr/local/apache-tomcat-9.0.16/webapps/kgc"
                   path="" reloadable="true" />
      </Host>

      <Host name="www.benet.com" appBase="/usr/local/apache-tomcat-9.0.16/webapps"
            unpackWARs="true" autoDeploy="true"
            xmlValidation="false" xmlNamespaceAware="false">
          <Context docBase="/usr/local/apache-tomcat-9.0.16/webapps/benet"
                   path="" reloadable="true" />
       </Host>

【3】設定域名

yum install -y bind
vim /etc/named.conf
   listen-on port 53 { any; };
   allow-query     { any; };
vim /etc/named.rfc1912.zones
   zone "kgc.com" IN {
        type master;
        file "kgc.com.zone";
        allow-update { none; };
   };
   zone "benet.com" IN {
        type master;
        file "benet.com.zone";
        allow-update { none; };
   };
cp -p /var/named/named.localhost /var/named/kgc.com.zone
vim /var/named/kgc.com.zone
   www IN  A       192.168.60.10
cp -p /var/named/kgc.com.zone /var/named/benet.com.zone

【4】啓動服務和tomcat

systemctl start named
/usr/local/apache-tomcat-9.0.16/bin/shutdown.sh 
/usr/local/apache-tomcat-9.0.16/bin/startup.sh 

【5】存取不同的域名顯示不同的結果

www.kgc.com:8080
在这里插入图片描述
www.benet.com:8080
在这里插入图片描述

三、Tomcat優化

【1】修改tomcat組態檔

vim /usr/local/apache-tomcat-9.0.16/conf/server.xml
    <Connector port="8080" 
      protocol="HTTP/1.1     
      connectionTimeout="20000"
      redirectPort="8443"
      minSpareThreads="50" ##############最小空閒執行緒數
      enableLookups="false"  #########是否反查域名
      disableUploadTimeout="true" ###########上傳時候是否使用超時機制 機製
      acceptCount="300"    #########可傳入最大連線請求的佇列長度,預設100
      maxThreads="500"  ############最大執行緒數 
      processorCache="500"   #########
      URIEncoding="UTF-8" #########tomcat容器編碼格式
      compression="on"   ############是否對響應的數據進行壓縮
      compressionMinSize="2048" #########表示壓縮響應的最小值
      compressableMimeType="text/html,text/xml,text/javascript,text/css,
      text/plain,image/gif,image/jpg,image/png"
    /> ########指定對那些檔案進行壓縮

【2】重新啓動tomcat

/usr/local/apache-tomcat-9.0.16/bin/shutdown.sh 
/usr/local/apache-tomcat-9.0.16/bin/startup.sh