CentOS7 安装 Java11 & Tomcat 9
1. 离线安装 java
(1). Oracle官网下载 java 包。
Oracle官网下载网址: https://www.oracle.com/java/technologies/javase/jdk11-archive-downloads.html
软件包: jdk-11.0.16_linux-x64_bin.rpm
(2). xftp 上传 到 linux 进行安装
上传目录推荐: /home/username
#ls /home/username/
jdk-11.0.16_linux-x64_bin.rpm
检查当前系统是否存在 java 环境:
# java -version
若有显示 java 版本信息需要卸载:
# rpm -qa|grep jdk // 检查JDK版本信息
卸载:
# rpm -e --nodeps jdk_???
卸载完后即可安装:
# rpm -ivh jdk-11.0.16_linux-x64_bin.rpm
验证安装:
# rpm -qa|grep jdk
(3). 配置 Java 运行环境:
修改配置文件
# vi /etc/profile
...
unset i
unset -f pathmunge
在该文件尾增加以下三行:
# JAVA_HOME
export JAVA_HOME=/usr/java/jdk-11.0.16
export PATH=$PATH:$JAVA_HOME/bin:$JAVA_HOME/lib //注意是分隔符是":"冒号,不是";"分号,输错后面将导致基本命令执行时报错
使配置文件生效:
# source /rtc/profile
任一目录执行验证:
# java -version
openjdk version "11.0.16" 2022-07-19 LTS
OpenJDK Runtime Environment (Red_Hat-11.0.16.0.8-1.el7_9) (build 11.0.16+8-LTS)
OpenJDK 64-Bit Server VM (Red_Hat-11.0.16.0.8-1.el7_9) (build 11.0.16+8-LTS, mixed mode, sharing)
查看安装的目录
# whereis java
java: /usr/bin/java /usr/share/man/man1/java1
网上所传配置慎用:
export JAVA_HOME=/usr/lib/jvm/jvm/java-11-openjdk-11.0.16.0.8-1.el7_9.x86_64
export JRE_HOME= J A V A H O M E / j r e e x p o r t C L A S S P A T H = JAVA_HOME/jre export CLASSPATH= JAVAHOME/jreexportCLASSPATH=JAVA_HOME/lib: J R E H O M E / l i b : JRE_HOME/lib: JREHOME/lib:CLASSPATH
export PATH= P A T H : PATH: PATH:JAVA_HOME/bin:$JRE_HOME/bin
2. 解压缩安装 tomcat
(1). 官网下载 tomcat9 压缩包.
官网 https://tomcat.apache.org/download-90.cgi, 下载: tar.gz (pgp, sha512) 文件
下载目录推荐: /home/username
(2). xftp 上传 到 linux 进行安装
# cd /home/username
# tar -zxvf apache-tomcat-9.0.65.tar.gz
# ls
会发现生成 apache-tomcat-9.0.65 目录
# mv apache-tomcat-9.0.65 /usr/local/tomcat9
或在线下载
# wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-9/v9.0.65/bin/apache-tomcat-9.0.65.tar.gz
# tar xzf apache-tomcat-9.0.65.tar.gz
# mv apache-tomcat-9.0.65 /usr/local/tomcat9
(3). 启动tomcat
# cd /usr/local/tomcat9/
# ./bin/startup.sh
[root@ltgCloudHost tomcat9]# ./bin/startup.sh
Using CATALINA_BASE: /usr/local/tomcat9
Using CATALINA_HOME: /usr/local/tomcat9
Using CATALINA_TMPDIR: /usr/local/tomcat9/temp
Using JRE_HOME: /usr/java/jdk-11.0.16
Using CLASSPATH: /usr/local/tomcat9/bin/bootstrap.jar:/usr/local/tomcat9/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.
最后浏览器中输入 http://localhost:8080
即可访问tomcat服务器了。
- 若不能访问,需开启 阿里云 安全组 (手动加入 8080/Tcp 0.0.0.0 )
[root@ltgCloudHost tomcat9]# firewall-cmd --list-all
public
target: default
icmp-block-inversion: no
interfaces:
sources:
services: dhcpv6-client ssh
ports: //`没有开任何端口`
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
[root@ltgCloudHost tomcat9]# firewall-cmd --list-ports
[root@ltgCloudHost tomcat9]# firewall-cmd --zone=public --add-port=8080/tcp --permanent
success
[root@ltgCloudHost tomcat9]# systemctl restart firewalld
[root@ltgCloudHost tomcat9]# firewall-cmd --list-ports
8080/tcp
[root@ltgCloudHost tomcat9]# firewall-cmd --list-all
public
target: default
icmp-block-inversion: no
interfaces:
sources:
services: dhcpv6-client ssh
ports: 8080/tcp //`己开端口8080`
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
- 若出现 tomcat 主页面 “Server Status” 、“Manager App”、"Host Manager"点击后报错: Access Denie
<1>. 将 conf/tomcat-users.xml 文件中增加以下几行
<role rolename="admin-gui"/>
<role rolename="admin-script"/>
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="manager-jmx"/>
<role rolename="manager-status"/>
<user username="admin" password="admin" roles="admin-gui,admin-script,manager-gui,manager-script,manager-jmx,manager-status"/>
<2>. 将 webapps/manager/META-INF/tomcat-users.xml 、webapps/host-manager/META-INF/tomcat-users.xml 内容更改
<!-- // 原内容注释掉换成下面一行
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
-->
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|\d+\.\d+\.\d+\.\d+" />