一、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

Tomcat介绍、安装、配置_第1张图片
Tomcat介绍、安装、配置_第2张图片

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

解决办法:
下载安装包并上传
Tomcat介绍、安装、配置_第3张图片

解压后,把服务移动重命名

[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

如截图所示
Tomcat介绍、安装、配置_第4张图片

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环境
Tomcat介绍、安装、配置_第5张图片

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介绍、安装、配置_第6张图片

特别说明: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配置文件
Tomcat介绍、安装、配置_第7张图片

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介绍、安装、配置_第8张图片