1. 程式人生 > >tomcat部署與多實例

tomcat部署與多實例

tomcat安裝 tomcat多實例

tomcat部署實踐

tomcat簡介

Tomcat 是由 Apache 開發的一個 Servlet 容器,用來生成動態頁面的。主要用來跑Java程序的Web應用服務器,屬於輕量級應用服務器,

Tomcat和Nginx、Apache(httpd)、lighttpd等Web服務器一樣,具有處理HTML頁面的功能,另外它還是一個Servlet和JSP容器,獨立的Servlet容器是Tomcat的默認模式。不過,Tomcat處理靜態HTML的能力不如Nginx/Apache服務器。

JDK是整個JAVA的核心,包括了JAVA運行環境,JAVA工具和基礎類庫等 (跑任何java程序或軟件,都需要先安裝jdk)

tomcat安裝

準備好安裝包

[root@tomcat opt]# tar xf jdk-8u45-linux-x64.tar.gz -C /usr/local/  --解壓安裝jdk
[root@tomcat opt]# ls /usr/local/jdk1.8.0_45/   --有目錄文件確認是解壓成功

[root@tomcat opt]# tar xf apache-tomcat-8.0.20.tar.gz -C /usr/local/    --解壓安裝tomcat
[root@tomcat local]# ln -s /usr/local/apache-tomcat-8.0.27 /usr/local/tomcat 

定義tomcat的環境變量

定義在單個tomcat的啟動和關閉程序裏
# vim /usr/local/tomcat/bin/startup.sh 
# vim /usr/local/tomcat/bin/shutdown.sh 
--這兩個腳本裏的最前面(但要在 #!/bin/bash 下在)加上下面一段

export JAVA_HOME=/usr/local/jdk1.8.0_45/
export TOMCAT_HOME=/usr/local/tomcat
export CATALINA_HOME=/usr/local/tomcat
export CLASS_PATH=$JAVA_HOME/bin/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tool.jar
export PATH=$PATH:/usr/local/jdk1.8.0_45/bin:/usr/local/tomcat/bin

註意授權,統一權限

[root@tomcat local]# chown root:root /usr/local/jdk1.8.0_45/  -R

檢查tomcat是否安裝成功

[root@tomcat local]# /usr/local/tomcat/bin/version.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/jdk1.8.0_45/
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Server version: Apache Tomcat/8.0.20
Server built:   Feb 15 2015 18:10:42 UTC
Server number:  8.0.20.0
OS Name:        Linux
OS Version:     2.6.32-696.el6.x86_64
Architecture:   amd64
JVM Version:    1.8.0_45-b14
JVM Vendor:     Oracle Corporation

tomcat重要的目錄介紹

[root@tomcat ~]# cd /usr/local/tomcat/
[root@tomcat tomcat]# ll
總用量 112
drwxr-xr-x 2 root root  4096 5月  12 12:25 bin       - Tomcat 腳本存放目錄(如啟動、關閉腳本)。
drwxr-xr-x 2 root root  4096 2月  16 2015 conf       - Tomcat 配置文件目錄。
drwxr-xr-x 2 root root  4096 5月  12 12:25 lib       - web應用調用的jar包存放路徑
-rw-r--r-- 1 root root 56977 2月  16 2015 LICENSE
drwxr-xr-x 2 root root  4096 2月  16 2015 logs       - Tomcat 默認日誌目錄。
-rw-r--r-- 1 root root  1397 2月  16 2015 NOTICE
-rw-r--r-- 1 root root  6781 2月  16 2015 RELEASE-NOTES
-rw-r--r-- 1 root root 16204 2月  16 2015 RUNNING.txt
drwxr-xr-x 2 root root  4096 5月  12 12:25 temp
drwxr-xr-x 7 root root  4096 2月  16 2015 webapps        - web程序存放目錄。
drwxr-xr-x 2 root root  4096 2月  16 2015 work

[root@tomcat tomcat]# ll conf/
總用量 208
-rw------- 1 root root  12374 2月  16 2015 catalina.policy
-rw------- 1 root root   7074 2月  16 2015 catalina.properties
-rw------- 1 root root   1577 2月  16 2015 context.xml
-rw------- 1 root root   3387 2月  16 2015 logging.properties
-rw------- 1 root root   6458 2月  16 2015 server.xml        --tomcat 主配置文件
-rw------- 1 root root   1744 2月  16 2015 tomcat-users.xml
-rw------- 1 root root   1846 2月  16 2015 tomcat-users.xsd  --tomcat 管理用戶配置文件
-rw------- 1 root root 163548 2月  16 2015 web.xml

[root@tomcat tomcat]# cd webapps/
[root@tomcat webapps]# ll
總用量 20
drwxr-xr-x 14 root root 4096 5月  12 12:25 docs          --幫助文檔
drwxr-xr-x  6 root root 4096 5月  12 12:25 examples      --web應用實例
drwxr-xr-x  5 root root 4096 5月  12 12:25 host-manager
drwxr-xr-x  5 root root 4096 5月  12 12:25 manager
drwxr-xr-x  3 root root 4096 5月  12 12:25 ROOT          --默認網站根目錄

tomcat管理命令

[root@tomcat 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/jdk1.8.0_45/
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
[root@tomcat tomcat]# lsof -i:8080      --tomcat默認監聽的端口是8080端口
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
java    1651 root   49u  IPv6  12410      0t0  TCP *:webcache (LISTEN)

[root@tomcat tomcat]# netstat -nltup|grep java      --tomcat的進程名是Java
tcp        0      0 ::ffff:127.0.0.1:8005       :::*              LISTEN      1709/java     
tcp        0      0 :::8009                     :::*              LISTEN      1709/java
tcp        0      0 :::8080                     :::*              LISTEN      1709/java           
[root@tomcat 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/jdk1.8.0_45/
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar

可以加到rc.local裏做成開機自動啟動
[root@tomcat tomcat]# echo /usr/local/tomcat/bin/startup.sh >> /etc/rc.local

在瀏覽器上進行訪問 http://10.0.0.14:8080/ (tomcat的主機IP加8080端口)

查看主配置文件

[root@tomcat conf]# vim server.xml 
        註意: <!--    -->  這符號裏的是註(下面是我截下來去掉了註釋)
 1 <?xml version=‘1.0‘ encoding=‘utf-8‘?>
 22 <Server port="8005" shutdown="SHUTDOWN">    --默認是8005端口監聽來自本機的關閉口令
 23   <Listener className="org.apache.catalina.startup.VersionLoggerListener" />
 28   <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
 30   <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
 31   <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
 32   <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
 37   <GlobalNamingResources>
 41     <Resource name="UserDatabase" auth="Container"
 42               type="org.apache.catalina.UserDatabase"
 43               description="User database that can be updated and saved"
 44               factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
 45               pathname="conf/tomcat-users.xml" />
 46   </GlobalNamingResources> 
 53   <Service name="Catalina">
 69     <Connector port="8080" protocol="HTTP/1.1"      --監聽的默認端口和協議
 70                connectionTimeout="20000"            --超時時間(毫秒)
 71                redirectPort="8443" />
 91     <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />    --AJP協議的監聽端口(用不到註釋掉就好)
103     <Engine name="Catalina" defaultHost="localhost">    --指定域名
114       <Realm className="org.apache.catalina.realm.LockOutRealm">
119         <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
120                resourceName="UserDatabase"/>
121       </Realm>
123       <Host name="localhost"  appBase="webapps"     --配置虛擬主機名和指定站點目錄
124             unpackWARs="true" autoDeploy="true">    --自動解壓war包和自動加載(false不啟用)
135         <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
136                prefix="localhost_access_log" suffix=".txt"
137                pattern="%h %l %u %t "%r" %s %b" />  --配置日誌相關的
139       </Host>
140     </Engine>
141   </Service>
142 </Server>

tomcat多實例

[root@tomcat local]# cd /usr/local/
[root@tomcat local]# cp -a apache-tomcat-8.0.20 tomcat1     --多一個配置文件就多一個實例
[root@tomcat local]# cp -a apache-tomcat-8.0.20 tomcat2

[root@tomcat local]# vim tomcat1/conf/server.xml    --修改配置文件下面幾處
22 <Server port="8005" shutdown="SHUTDOWN">     --修改監聽關閉口令的端口
69     <Connector port="8080" protocol="HTTP/1.1"   --修改每個實例監聽不同的端口
91     <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> --最好註釋掉以免端口沖突被占用

sed命令修改端口的方法:
[root@tomcat local]# sed -i ‘s#8005#8011#;s#8080#8081#‘ tomcat1/conf/server.xml
[root@tomcat local]# sed -i ‘s#8005#8012#;s#8080#8082#‘ tomcat2/conf/server.xml

web站點部署
就是直接將程序目錄放在webapps目錄下面
或
將程序打包成war包,然後上傳到webapps目錄下面。

啟動服務:
[root@tomcat local]# /usr/local/tomcat1/bin/startup.sh 
Using CATALINA_BASE:   /usr/local/tomcat1
Using CATALINA_HOME:   /usr/local/tomcat1
Using CATALINA_TMPDIR: /usr/local/tomcat1/temp
Using JRE_HOME:        /usr/local/jdk1.8.0_45/
Using CLASSPATH:       /usr/local/tomcat1/bin/bootstrap.jar:/usr/local/tomcat1/bin/tomcat-juli.jar
Tomcat started.

[root@tomcat local]# /usr/local/tomcat2/bin/startup.sh 
Using CATALINA_BASE:   /usr/local/tomcat2
Using CATALINA_HOME:   /usr/local/tomcat2
Using CATALINA_TMPDIR: /usr/local/tomcat2/temp
Using JRE_HOME:        /usr/local/jdk1.8.0_45/
Using CLASSPATH:       /usr/local/tomcat2/bin/bootstrap.jar:/usr/local/tomcat2/bin/tomcat-juli.jar
Tomcat started.

[root@tomcat local]# netstat -nltup|grep java
tcp        0      0 ::ffff:127.0.0.1:8005       :::*              LISTEN      1644/java         tcp        0      0 :::8009                     :::*              LISTEN      1644/java         tcp        0      0 ::ffff:127.0.0.1:8011       :::*              LISTEN      2133/java         tcp        0      0 ::ffff:127.0.0.1:8012       :::*              LISTEN      2159/java         tcp        0      0 :::8080                     :::*              LISTEN      1644/java        tcp        0      0 :::8081                     :::*              LISTEN      2133/java         tcp        0      0 :::8082                     :::*              LISTEN      2159/java 

tomcat部署與多實例