1. 程式人生 > >第一篇:Tomcat介紹、安裝、配置

第一篇:Tomcat介紹、安裝、配置

分享圖片 訪問網站 esp ext .tar.gz resp less a20 關閉防火墻

一、Tomcat介紹
  • Tomcat是Apache軟件基金會(Apache Software Foundation)的Jakarta項目中的一個核心項目,有Apache 、Sun和其他一些公司以及個人共同開發而成。
  • java程序寫的網站用Tomcat+jdk來運行
  • Tomcat是一個中間件,真正起作用的,解析java腳本的是JDK
  • JDK(java development kit)是整個java的核心,它包含了java運行環境和一堆java相關的工具 以及java基礎庫
  • 最主流的jdk為Sun公司發布的JDK,除此之外,其實IBM公司也有發布JDK,CentOS上也可以用yum安裝openjdk

二、JDK下載安裝

1.下載JDK
官方地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

技術分享圖片
技術分享圖片

root@docker ~]# cd /usr/local/src/
[root@docker src]# wget  http://download.oracle.com/otn-pub/java/jdk/8u161-b12/2f38c3b165be4555a1fa6e98c45e0808/jdk-8u161-linux-x64.tar.gz
--2018-02-09 21:35:28--  http://download.oracle.com/otn-pub/java/jdk/8u161-b12/2f38c3b165be4555a1fa6e98c45e0808/jdk-8u161-linux-x64.tar.gz
Resolving download.oracle.com (download.oracle.com)... 23.1.162.118
Connecting to download.oracle.com (download.oracle.com)|23.1.162.118|:80... connected.
HTTP request sent, awaiting response... 302 Moved Temporarily
Location: https://edelivery.oracle.com/otn-pub/java/jdk/8u161-b12/2f38c3b165be4555a1fa6e98c45e0808/jdk-8u161-linux-x64.tar.gz [following]
--2018-02-09 21:35:28--  https://edelivery.oracle.com/otn-pub/java/jdk/8u161-b12/2f38c3b165be4555a1fa6e98c45e0808/jdk-8u161-linux-x64.tar.gz
Resolving edelivery.oracle.com (edelivery.oracle.com)... 184.50.91.58, 2600:1417:76:19a::2d3e, 2600:1417:76:181::2d3e
Connecting to edelivery.oracle.com (edelivery.oracle.com)|184.50.91.58|:443... connected.
HTTP request sent, awaiting response... 302 Moved Temporarily
Location: http://download.oracle.com/errors/download-fail-1505220.html [following]
--2018-02-09 21:35:30--  http://download.oracle.com/errors/download-fail-1505220.html
Connecting to download.oracle.com (download.oracle.com)|23.1.162.118|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 5307 (5.2K) [text/html]
Saving to: ‘jdk-8u161-linux-x64.tar.gz’
100%[===================================================================================================================>] 5,307       --.-K/s   in 0s      
2018-02-09 21:35:30 (21.6 MB/s) - ‘jdk-8u161-linux-x64.tar.gz’ saved [5307/5307]
[root@docker src]# ll
total 8
-rw-r--r--. 1 root root 5307 Mar 21  2012 jdk-8u161-linux-x64.tar.gz

[root@docker src]# tar -zxvf jdk-8u161-linux-x64.tar.gz  
gzip: stdin: not in gzip format
tar: Child returned status 1
tar: Error is not recoverable: exiting now

2.解壓報錯,查看文件類型發現,通過上面的下載jdk的方式得到的是一個HTML document

[root@docker src]# file jdk-8u161-linux-x64.tar.gz
jdk-8u161-linux-x64.tar.gz: HTML document, ASCII text, with very long lines, with CRLF line terminators

解決辦法:
下載安裝包並上傳
技術分享圖片

解壓後,把服務移動重mingming

[root@docker src]# tar -zxvf jdk-8u161-linux-x64.tar.gz
[root@docker src]# ll
total 185312
drwxr-xr-x. 8   10  143       255 Dec 20 08:24 jdk1.8.0_161
-rw-r--r--. 1 root root 189756259 Feb  9 21:46 jdk-8u161-linux-x64.tar.gz
[root@docker src]# mv jdk1.8.0_161/ /usr/local/jdk1.8

3.編輯/etc/profile配置文件,把下面配置文件寫入文件底端

JAVA_HOME=/usr/local/jdk1.8/
JAVA_BIN=/usr/local/jdk1.8/bin
JRE_HOME=/usr/local/jdk1.8/jre
PATH=$PATH=:/usr/local/jdk1.8/bin:/usr/local/jdk1.8/jre/bin

如截圖所示
技術分享圖片

4.加載/etc/profile配置文件,使該文件立即生效

source /etc/profile

5.查看java版本

[root@docker src]# java -version
openjdk version "1.8.0_151"
OpenJDK Runtime Environment (build 1.8.0_151-b12)
OpenJDK 64-Bit Server VM (build 25.151-b12, mixed mode)

6.查看當前系統是否安裝其他版本的jdk

[root@docker src]# rpm -qa |grep jdk
copy-jdk-configs-2.2-5.el7_4.noarch
java-1.8.0-openjdk-headless-1.8.0.151-5.b12.el7_4.x86_64
java-1.8.0-openjdk-1.8.0.151-5.b12.el7_4.x86_64

三、安裝Tomcat

1.下載安裝包,並上傳到Linux環境
技術分享圖片

2.把tomcat軟件包放在指定的目錄並解壓

[root@docker ~]# mv apache-tomcat-8.5.27.tar.gz  /usr/local/src/
[root@docker ~]# cd /usr/local/src/
[root@docker src]# ll
total 194628
-rw-r--r--. 1 root root   9536557 Feb 10 06:35 apache-tomcat-8.5.27.tar.gz
-rw-r--r--. 1 root root 189756259 Feb  9 21:46 jdk-8u161-linux-x64.tar.gz
[root@docker src]# tar -zxvf apache-tomcat-8.5.27.tar.gz

3.把tomcat解壓後的目錄文件移動重命名,方便管理服務

[root@docker src]# mv apache-tomcat-8.5.27 /usr/local/tomcat
[root@docker src]# ll
total 194628
-rw-r--r--. 1 root root   9536557 Feb 10 06:35 apache-tomcat-8.5.27.tar.gz
-rw-r--r--. 1 root root 189756259 Feb  9 21:46 jdk-8u161-linux-x64.tar.gz

4.啟動tomcat

[root@docker src]# /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:        /
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.

5.查看tomcat服務進程是否啟動

[root@docker src]# ps -ef |grep tomcat
root     12810     1 18 06:51 pts/0    00:00:05 //bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dignore.endorsed.dirs= -classpath /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomca -Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap start
root     12892 12625  0 06:52 pts/0    00:00:00 grep --color=auto tomcat

6.關閉tomcat服務

[root@docker src]# /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:        /
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@docker src]# !ps
ps -ef |grep tomcat
root     12966 12625  0 06:53 pts/0    00:00:00 grep --color=auto tomcat

7.查看tomca監聽的端口

[root@docker src]# netstat -nlp |grep java
tcp6       0      0 :::8080                 :::*                    LISTEN      12984/java          
tcp6       0      0 127.0.0.1:8005          :::*                    LISTEN      12984/java          
tcp6       0      0 :::8009                 :::*                    LISTEN      12984/java      

8.可在瀏覽器登錄tomcat頁面,前提是需要關閉防火墻或者放行tomcat的訪問端口8080

[root@docker src]# iptables -F
[root@docker src]# iptables -X
[root@docker src]# iptables -Z

技術分享圖片

特別說明:tomcat會用到三個端口,8080提供web服務的端口 ,8005為管理端口、8009是為第三方服務調用的端口,如httpd和Tomcat結合時使用

四、配置Tomcat監聽端口為80

1.默認情況下,tomcat會使用8080、8005、8009三個端口,8080端口是提供web服務的

[root@docker src]# netstat -nlp | grep java
tcp6       0      0 :::8080                 :::*                    LISTEN      12984/java          
tcp6       0      0 127.0.0.1:8005          :::*                    LISTEN      12984/java          
tcp6       0      0 :::8009                 :::*                    LISTEN      12984/java     

2.當我們想要直接使用域名或者IP直接訪問網站,不需要添加端口的話,只能把tomcat的監聽端口修改為80,編輯Tomcat的配置文件 /usr/local/tomcat/conf/server.xml配置文件
技術分享圖片

3.先關閉tomcat服務,再重啟服務

[root@docker src]# /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:        /
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@docker src]# /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:        /
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.

4.查看tomcat的監聽端口是否為80

[root@docker src]# netstat -nlp |grep java
tcp6       0      0 :::80                   :::*                    LISTEN      13427/java   ##tomcat監聽端口修改成80      
tcp6       0      0 127.0.0.1:8005          :::*                    LISTEN      13427/java          
tcp6       0      0 :::8009                 :::*                    LISTEN      13427/java         

5.此時在瀏覽器直接輸入主機IP就可以訪問tomcat服務

技術分享圖片

第一篇:Tomcat介紹、安裝、配置