Tomcat是Apache 軟件基金會(Apache Software Foundation)的Jakarta 項目中的一個核心項目,由Apache、Sun 和其餘一些公司及我的共同開發而成。因爲有了Sun 的參與和支持,最新的Servlet 和JSP 規範老是能在Tomcat 中獲得體現,Tomcat 5支持最新的Servlet 2.4 和JSP 2.0 規範。由於Tomcat 技術先進、性能穩定,並且免費,於是深受Java 愛好者的喜好並獲得了部分軟件開發商的承認,成爲目前比較流行的Web 應用服務器。
Tomcat 服務器是一個免費的開放源代碼的Web 應用服務器,屬於輕量級應用服務器,在中小型系統和併發訪問用戶不是不少的場合下被廣泛使用,是開發和調試JSP 程序的首選。對於一個初學者來講,能夠這樣認爲,當在一臺機器上配置好Apache 服務器,可利用它響應HTML(標準通用標記語言下的一個應用)頁面的訪問請求。實際上Tomcat是Apache 服務器的擴展,但運行時它是獨立運行的,因此當你運行tomcat 時,它實際上做爲一個與Apache 獨立的進程單獨運行的。
訣竅是,當配置正確時,Apache 爲HTML頁面服務,而Tomcat 實際上運行JSP 頁面和Servlet。另外,Tomcat和IIS等Web服務器同樣,具備處理HTML頁面的功能,另外它仍是一個Servlet和JSP容器,獨立的Servlet容器是Tomcat的默認模式。不過,Tomcat處理靜態HTML的能力不如Apache服務器。目前Tomcat最新版本爲9.0。html
tomcat就是傳說中的中間件之一,tomcat自己是一個容器,專門用來運行java程序,java語言開發的網頁.jsp就應該運行於tomcat中。而tomcat自己的運行也依賴於jdk環境。
tomcat應用場景:lnmt
tomcat官網 :http://tomcat.apache.org/java
tomcat應用場景:lnmt
tomcat官網 :連接: http://tomcat.apache.org/.linux
以部署兩個項目爲例:web
//關閉防火牆 [root@localhost ~]# systemctl stop firewalld [root@localhost ~]# systemctl disable firewalld Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service. Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service. [root@localhost ~]# vim /etc/selinux/config **//將第六行中的 enforcing 改成 disabled** # This file controls the state of SELinux on the system. # SELINUX= can take one of these three values: # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - No SELinux policy is loaded. SELINUX=disabled [root@localhost ~]# setenforce 0 //安裝以前請確保本地倉庫可用 //安裝jdk環境 [root@localhost ~]# yum -y install java-1.8.0* ……下載過程略 [root@localhost src]# cd /usr/local/ [root@localhost local]# mkdir tomcat //下載tomcat [root@localhost ~]# cd /usr/src/ [root@localhost src]# wget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.8/bin/apache-tomcat-9.0.8.tar.gz ……下載過程略 [root@localhost src]# ls apache-tomcat-9.0.35.tar.gz debug kernels //部署項目一 [root@localhost src]# tar xf apache-tomcat-9.0.35.tar.gz [root@localhost src]# ls apache-tomcat-9.0.35 apache-tomcat-9.0.35.tar.gz debug kernels [root@localhost src]# mv apache-tomcat-9.0.35 tomcat1 [root@localhost src]# mv tomcat1 /usr/local/tomcat/ //部署項目二 [root@localhost src]# tar xf apache-tomcat-9.0.35.tar.gz [root@localhost src]# ls apache-tomcat-9.0.35 apache-tomcat-9.0.35.tar.gz debug kernels [root@localhost src]# mv apache-tomcat-9.0.35 tomcat2 [root@localhost src]# mv tomcat2 /usr/local/tomcat/ [root@localhost src]# cd /usr/local/tomcat/ [root@localhost tomcat]# ls tomcat1 tomcat2 //分別在兩個項目中進行測試 //tomcat1 [root@localhost tomcat]# cd tomcat1/webapps/ [root@localhost webapps]# ls docs examples host-manager manager ROOT //建立測試文檔 [root@localhost webapps]# mkdir test [root@localhost webapps]# cd test/ //編寫一個java頁面進行測試 [root@localhost test]# vim index.jsp [root@localhost test]# cat index.jsp <html> <head> <title>test</title> </head> <body> <% out.println("I am tomcat1"); %> </body> </html> //在tomcat2中的操做與tomcat1中相同 [root@localhost tomcat]# cd tomcat2/webapps/ [root@localhost webapps]# ls docs examples host-manager manager ROOT //建立測試文檔 [root@localhost webapps]# mkdir test [root@localhost webapps]# cd test/ //編寫一個java頁面進行測試 [root@localhost test]# vim index.jsp [root@localhost test]# cat index.jsp <html> <head> <title>test</title> </head> <body> <% out.println("I am tomcat1"); %> </body> </html> //啓動tomcat //啓動tomcat1,端口爲8080和8005 [root@localhost ~]# /usr/local/tomcat/tomcat1/bin/catalina.sh start Using CATALINA_BASE: /usr/local/tomcat/tomcat1 Using CATALINA_HOME: /usr/local/tomcat/tomcat1 Using CATALINA_TMPDIR: /usr/local/tomcat/tomcat1/temp Using JRE_HOME: /usr Using CLASSPATH: /usr/local/tomcat/tomcat1/bin/bootstrap.jar:/usr/local/tomcat/tomcat1/bin/tomcat-juli.jar Tomcat started. [root@localhost bin]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:22 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 100 :::8080 :::* LISTEN 0 128 :::22 :::* LISTEN 0 100 ::1:25 :::* LISTEN 0 1 ::ffff:127.0.0.1:8005 :::* //tomcat2沒法與tomcat1同樣啓動,由於他們的服務端口是同樣的,若是再啓動tomcat2就會有衝突。 //要想啓動tomcat2,須要更改tomcat2的服務端口 [root@localhost ~]# cd /usr/local/tomcat/tomcat2/conf/ [root@localhost conf]# ls catalina.policy context.xml jaspic-providers.xsd server.xml tomcat-users.xsd catalina.properties jaspic-providers.xml logging.properties tomcat-users.xml web.xml [root@localhost conf]# vim server.xml ....... //將prot後的8005端口改成你想改的。 <Server port="8006" shutdown="SHUTDOWN"> <Listener className="org.apache.catalina.startup.VersionLoggerListener" /> ....... ....... //將prot後的8080端口改成你想改的。 //將redirectPort後的8443端口改成你想改的。 <Connector port="8081" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8445" /> ....... //改完後就能夠啓動tomcat2 [root@localhost ~]# /usr/local/tomcat/tomcat2/bin/catalina.sh start Using CATALINA_BASE: /usr/local/tomcat/tomcat2 Using CATALINA_HOME: /usr/local/tomcat/tomcat2 Using CATALINA_TMPDIR: /usr/local/tomcat/tomcat2/temp Using JRE_HOME: /usr Using CLASSPATH: /usr/local/tomcat/tomcat2/bin/bootstrap.jar:/usr/local/tomcat/tomcat2/bin/tomcat-juli.jar Tomcat started. [root@localhost bin]# ss -antl State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:22 *:* LISTEN 0 100 127.0.0.1:25 *:* LISTEN 0 100 :::8080 :::* LISTEN 0 100 :::8081 :::* LISTEN 0 128 :::22 :::* LISTEN 0 100 ::1:25 :::* LISTEN 0 1 ::ffff:127.0.0.1:8005 :::* LISTEN 0 1 ::ffff:127.0.0.1:8006 :::*
當點擊圖中說指的內容時,會被拒絕。
修改配置文件/usr/local/tomcat/conf/tomcat-users.xmlapache
[root@localhost ~]# vim /usr/local/tomcat/conf/tomcat-users.xml ...... <role rolename="manager-gui"/> <role rolename="admin-gui"/> <user username="tomcat" password="123456" roles="manager-gui,admin-gui"/> </tomcat-users> ## 在此行前面添加以上內容
**注意:admin-gui是管理Host Manager項的,manager-gui是管理Server status和Manager App項bootstrap
修改配置文件/usr/local/tomcat/webapps/manager/META-INF/context.xml** <Context antiResourceLocking="false" privileged="true" > <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="192\.168\.100\.1|127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> ##此處要容許宿主機的ip訪問 <Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/> </Context>
重啓tomcatvim
[root@localhost ~]# /usr/local/tomcat/bin/catalina.sh stop Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar [root@localhost ~]# /usr/local/tomcat/bin/catalina.sh start Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Tomcat started.
如今已經能夠進入Server status和Manager App了,點擊對應的按鈕,輸入密碼便可進入,頁面以下:
**可是如今還不能進入Host Managertomcat
修改配置文件/usr/local/tomcat/webapps/host-manager/META-INF/context.xml**服務器
<Context antiResourceLocking="false" privileged="true" > <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="192\.168\.100\.1|127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> ##一樣加上容許宿主機ip訪問 <Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/> </Context>
重啓tomcat服務session
[root@localhost ~]# /usr/local/tomcat/bin/catalina.sh stop Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar [root@localhost ~]# /usr/local/tomcat/bin/catalina.sh start Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar Tomcat started.
點擊Host Manager的按鈕,輸入密碼便可進入,頁面以下: