安装 Java 环境


下载 Java安装包

第一步:查看系统版本

[root@localhost localhost]# getconf LONG_BIT
64

第二步:下载对应版本的JDK
JDK安装

开始安装

第一步:执行安装步骤

# 创建安装目录
[root@localhost localhost]# mkdir /usr/local/java
# 将下载好的JDK的安装包解压到安装目录中
[root@localhost localhost]# tar -zxvf jdk-8u171-linux-x64.tar.gz -C /usr/local/java/

第二步:添加环境变量

1.打开文件

[root@localhost localhost]# vim /etc/profile

2.在文件的末尾追加一下内容

export JAVA_HOME=/usr/local/java/jdk1.8.0_171
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH

3.加载新增配置

[root@localhost localhost]# source /etc/profile

4.添加软连接

[root@localhost localhost]# ln -s /usr/local/java/jdk1.8.0_171/bin/java /usr/bin/java

5.检查版本

[root@localhost localhost]# java -version
java version "1.8.0_211"
Java(TM) SE Runtime Environment (build 1.8.0_211-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.211-b12, mixed mode)

安装 tomcat


第一步:下载安装包

wget http://mirror.bit.edu.cn/apache/tomcat/tomcat-8/v8.5.31/bin/apache-tomcat-8.5.31.tar.gz

第二步:执行安装步骤

[root@localhost localhost]# tar xf apache-tomcat-8.5.31.tar.gz -C /usr/local/
[root@localhost localhost]# cd /usr/local/
[root@localhost /usr/local]# ln -sv apache-tomcat-8.5.31 tomcat

第三步:配置环境变量

1.打开文件

[root@localhost localhost]# vim /etc/profile.d/tomcat.sh

2.添加以下内容

CATALINA_BASE=/usr/local/tomcat
PATH=$CATALINA_BASE/bin:$PATH
export PATH CATALINA_BASE

3.加载新增配置

[root@localhost localhost]# source  /etc/profile.d/tomcat.sh

4.查看tomcat的状态

[root@localhost localhost]# catalina.sh version
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/local/java/jdk1.8.0_211/jre
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Server version: Apache Tomcat/8.5.40
Server built:   Apr 10 2019 14:31:19 UTC
Server number:  8.5.40.0
OS Name:        Linux
OS Version:     3.10.0-862.el7.x86_64
Architecture:   amd64
JVM Version:    1.8.0_211-b12
JVM Vendor:     Oracle Corporation

配置 tomcat

第一步:配置server.xml

vim /usr/local/tomcat/conf/server.xml 

<Connector port="80" protocol="HTTP/1.1"      //默认端口为8080,改为自己想要开放的端口
           connectionTimeout="20000"
           redirectPort="8443" />

第二步:配置tomcat-users.xml和webapps/manager/META-INF/context.xml和webapps/host-manager/META-INF/context.xml

1.tomcat有manager-gui的管理页面,想要使用必须配置管理用户,不使用可以跳过此步。

<role rolename="manager-gui"/>                                       //指定用户可以使用的接口为manager-gui
<user username="tomcat" password="tomcat" roles="manager-gui"/>      //用户名和密码为tomcat,在manager-gui接口使用

2.cat webapps/manager/META-INF/context.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at
 
      http://www.apache.org/licenses/LICENSE-2.0
 
  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<Context antiResourceLocking="false" privileged="true" >
<!--
  <Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />         #可以改为本机的IP地址。默认为127.0.0.1,所以我们访问的时候不行。
-->
  <Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/>
</Context>

测试

第一步:创建测试页面

1.打开页面

/usr/local/tomcat/webapps/test/index.jsp

2.页面内添加以下内容

<%@ page language="java" %>
<%@ page import="java.util.*" %>
<html>
    <head>
        <title>test</title>
    </head>
    <body>
        <%
            out.println("Hello World!");      //嵌入java语言
        %>
    </body>
</html>

第二步:启动测试

catalina.sh start
最后修改:2020 年 10 月 28 日 10 : 13 PM
如果觉得我的文章对你有用,请随意赞赏