1 JDK
1.1 JDK和JRE的关系
JRE: 包含JVM和JAVA库
JDK: 包含JRE和开发工具
单纯的运行java程序, 只需JRE
开发人员需要安装JDK
1.2 JDK版本
Oracle JDK: JDK8, JDK11 长期支持版本
OpenJDK: 1.8.0, 1.11.0
1.3 OpenJDK 安装
- CentOS 8 利用 yum 安装OpenJDK
[16:01:04 root@jdk ~]#yum -y install java-11-openjdk.x86_64
[16:04:24 root@jdk ~]#java --version
openjdk 11.0.9.1 2020-11-04 LTS
OpenJDK Runtime Environment 18.9 (build 11.0.9.1+1-LTS)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.9.1+1-LTS, mixed mode, sharing)
- java编译器, javac的安装
jdk-devel包提供javac, 因此如果需要编译java程序, 还需要安装devel包
[16:12:10 root@jdk ~]#yum -y install java-11-openjdk-devel.x86_64
[16:12:29 root@jdk ~]#javac --version
javac 11.0.9.1
- java编译演示
# java代码
[16:15:54 root@jdk ~]#ll
total 8
-rw-r--r-- 1 root root 114 Mar 21 16:14 HelloWorld.java
[16:15:12 root@jdk ~]#vim HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
# javac 编译
[16:14:50 root@jdk ~]#javac HelloWorld.java
# 编译后生成.class文件, 也就是字节码
[16:15:54 root@jdk ~]#ll
total 8
-rw-r--r-- 1 root root 426 Mar 21 16:14 HelloWorld.class
-rw-r--r-- 1 root root 114 Mar 21 16:14 HelloWorld.java
# 运行代码
[16:15:56 root@jdk ~]#java HelloWorld
Hello World!
1.4 OracleJDK安装
- 需要在Oracle官网注册
https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html
- 还原之前的虚拟机, yum安装OracleJDK的rpm包
[16:32:10 root@jdk ~]#ll
total 110348
-rw-r--r-- 1 root root 112994496 Oct 24 21:59 jdk-8u271-linux-x64.rpm
[16:32:11 root@jdk ~]#yum -y install jdk-8u271-linux-x64.rpm
# OracleJDK的rpm包直接提供javac工具
[16:33:24 root@jdk ~]#java -version
java version "1.8.0_271"
Java(TM) SE Runtime Environment (build 1.8.0_271-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.271-b09, mixed mode)
[16:33:40 root@jdk ~]#javac -version
javac 1.8.0_271
- 初始化环境变量, 为了支持Tomcat
# 将/usr/java/default目录, 作为JAVA_HOME, 因为yum安装的oraclejdk, java的所有文件都是在这个default目录中
[16:33:43 root@jdk ~]#ll /usr/java/default/
total 25784
drwxr-xr-x 2 root root 4096 Mar 21 16:32 bin
-rw-r--r-- 1 root root 3244 Sep 17 2020 COPYRIGHT
drwxr-xr-x 3 root root 132 Mar 21 16:32 include
-rw-r--r-- 1 root root 5220228 Sep 10 2020 javafx-src.zip
-rw-r--r-- 1 root root 195 Sep 17 2020 jmc.txt
drwxr-xr-x 6 root root 198 Mar 21 16:32 jre
drwxr-xr-x 4 root root 31 Mar 21 16:32 legal
drwxr-xr-x 4 root root 223 Mar 21 16:32 lib
-rw-r--r-- 1 root root 44 Sep 17 2020 LICENSE
drwxr-xr-x 4 root root 47 Mar 21 16:32 man
-rw-r--r-- 1 root root 159 Sep 17 2020 README.html
-rw-r--r-- 1 root root 424 Sep 17 2020 release
-rw-r--r-- 1 root root 21147082 Sep 17 2020 src.zip
-rw-r--r-- 1 root root 190 Sep 10 2020 THIRDPARTYLICENSEREADME-JAVAFX.txt
-rw-r--r-- 1 root root 190 Sep 17 2020 THIRDPARTYLICENSEREADME.txt
# 将$JAVA_HOME/bin加入到PATH变量
[16:35:54 root@jdk ~]#vim /etc/profile.d/jdk.sh
export JAVA_HOME=/usr/java/default
export PATH=$JAVA_HOME/bin:$PATH
[16:38:25 root@jdk ~]#. /etc/profile.d/jdk.sh
# 非必选项
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=$JAVA_HOME/lib:$JRE_HOME/lib
- 再准备一个虚拟机, 进行OracleJDK的二进制安装
[16:47:11 root@jdk ~]#ls
jdk-8u271-linux-x64.tar.gz
[16:47:12 root@jdk ~]#tar xf jdk-8u271-linux-x64.tar.gz -C /usr/local
[16:52:36 root@jdk ~]#cd /usr/local
[16:52:38 root@jdk /usr/local]#ls
bin etc games include jdk1.8.0_271 lib lib64 libexec sbin share src
[16:52:39 root@jdk /usr/local]#ln -s jdk1.8.0_271/ jdk
[16:52:50 root@jdk /usr/local]#ll
total 0
drwxr-xr-x. 2 root root 6 May 11 2019 bin
drwxr-xr-x. 2 root root 6 May 11 2019 etc
drwxr-xr-x. 2 root root 6 May 11 2019 games
drwxr-xr-x. 2 root root 6 May 11 2019 include
lrwxrwxrwx 1 root root 13 Mar 21 16:52 jdk -> jdk1.8.0_271/
drwxr-xr-x 8 10143 10143 273 Sep 17 2020 jdk1.8.0_271
drwxr-xr-x. 2 root root 6 May 11 2019 lib
drwxr-xr-x. 2 root root 6 May 11 2019 lib64
drwxr-xr-x. 2 root root 6 May 11 2019 libexec
drwxr-xr-x. 2 root root 6 May 11 2019 sbin
drwxr-xr-x. 5 root root 49 Oct 29 21:17 share
drwxr-xr-x. 2 root root 6 May 11 2019 src
# 初始化环境变量
[16:53:40 root@jdk /usr/local]#vim /etc/profile.d/jdk.sh
export JAVA_HOME=/usr/local/jdk
export PATH=$PATH:$JAVA_HOME/bin
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=$JAVA_HOME/lib/:$JRE_HOME/lib
[16:55:24 root@jdk /usr/local]#. /etc/profile.d/jdk.sh
[16:56:26 root@jdk /usr/local]#java -version
java version "1.8.0_271"
Java(TM) SE Runtime Environment (build 1.8.0_271-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.271-b09, mixed mode)
[16:56:37 root@jdk /usr/local]#javac -version
javac 1.8.0_271
[16:56:41 root@jdk /usr/local]#which java
/usr/local/jdk/bin/java
2. Tomcat 单机部署
2.1 安装 Oracle JDK
- RPM安装(8u271)
从Oracle官网登录, 下载RPM包, 直接yum安装
[18:02:57 root@201 ~]#ls -lh jdk-8u271-linux-x64.rpm
-rw-r--r-- 1 root root 108M Oct 24 21:59 jdk-8u271-linux-x64.rpm
[18:05:00 root@201 ~]#yum -y install jdk-8u271-linux-x64.rpm
[18:05:33 root@201 ~]#java -version
java version "1.8.0_271"
Java(TM) SE Runtime Environment (build 1.8.0_271-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.271-b09, mixed mode)
[18:45:14 root@201 ~]#which java
/usr/bin/java
[18:45:17 root@201 ~]#ll /usr/bin/java
lrwxrwxrwx 1 root root 22 Nov 1 18:45 /usr/bin/java -> /etc/alternatives/java
[18:45:37 root@201 ~]#ll /etc/alternatives/java
lrwxrwxrwx 1 root root 37 Nov 1 18:45 /etc/alternatives/java -> /usr/java/jdk1.8.0_271-amd64/bin/java
[18:45:43 root@201 ~]#ll /usr/java/jdk1.8.0_271-amd64/bin/java
-rwxr-xr-x 1 root root 8712 Sep 17 01:11 /usr/java/jdk1.8.0_271-amd64/bin/java
设置环境变量
yum安装的oracle-jdk, JAVA的工作目录为/usr/java/default, 里面存放了所有JAVA相关的文件
JAVA的二进制程序都是保存在/usr/java/default/bin目录下, 因此要把该目录添加到环境变量
[18:45:52 root@201 ~]#vim /etc/profile.d/jdk.sh
export JAVA_HOME=/usr/java/default
export PATH=$JAVA_HOME/bin:$PATH
[18:47:40 root@201 ~]#. /etc/profile.d/jdk.sh
[18:47:46 root@201 ~]#echo $PATH
/usr/java/default/bin:/data/scripts:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
- 二进制安装(8u271)
[18:56:49 root@202 ~]#ls -h jdk-8u271-linux-x64.tar.gz -l
-rw-r--r-- 1 root root 137M Oct 24 22:32 jdk-8u271-linux-x64.tar.gz
[18:56:52 root@202 ~]#tar xvf jdk-8u271-linux-x64.tar.gz -C /usr/local
[18:57:05 root@202 ~]#cd /usr/local
[18:57:10 root@202 /usr/local]#ls
bin etc games include jdk1.8.0_271 lib lib64 libexec sbin share src
[18:57:10 root@202 /usr/local]#ln -s jdk1.8.0_271 jdk
[18:57:32 root@202 /usr/local]#ls
bin etc games include jdk jdk1.8.0_271 lib lib64 libexec sbin share src
[18:57:32 root@202 /usr/local]#ll jdk
lrwxrwxrwx 1 root root 12 Nov 1 18:57 jdk -> jdk1.8.0_271
设置环境变量
[18:57:56 root@202 /usr/local]#vim /etc/profile.d/jdk.sh
export JAVA_HOME=/usr/local/jdk
export PATH=$PATH:$JAVA_HOME/bin
[18:59:10 root@202 /usr/local]#. /etc/profile.d/jdk.sh
[18:59:27 root@202 /usr/local]#echo $PATH
/data/scripts:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/jdk/bin
[18:59:30 root@202 /usr/local]#java -version
java version "1.8.0_271"
Java(TM) SE Runtime Environment (build 1.8.0_271-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.271-b09, mixed mode)
[18:59:44 root@202 /usr/local]#which java
/usr/local/jdk/bin/java
2.2 安装 Tomcat
Tomcat自身就是一个JAVA程序, 运行在JAVA环境中, 然后在Tomcat里运行JAVA程序, 类似容器.
CentOS 7 自带的Tomcat为 7.0版本, 不推荐使用.
目前比较主流的是8.5.x版本. 但是CentOS8上还没有Tomcat包, 因此, 可以在官网下载二进制包, 通过二进制安装Tomcat
2.2.1 二进制安装Tomcat
解压缩, 创建软连接
[19:15:07 root@202 ~]#ls
anaconda-ks.cfg apache-tomcat-8.5.59.tar.gz jdk-8u271-linux-x64.tar.gz
[19:20:04 root@202 ~]#tar -xvf apache-tomcat-8.5.59.tar.gz -C /usr/local
[19:21:04 root@202 ~]#cd /usr/local
[19:29:49 root@202 /usr/local]#ls
apache-tomcat-8.5.59 bin etc games include jdk jdk1.8.0_271 lib lib64 libexec sbin share src
[19:39:50 root@202 /usr/local]#ln -s apache-tomcat-8.5.59 tomcat
[19:20:03 root@202 /usr/local]#ls
apache-tomcat-8.5.59 bin etc games include jdk jdk1.8.0_271 lib lib64 libexec sbin share src tomcat
指定PATH变量
[19:21:08 root@202 /usr/local]#echo "PATH=/usr/local/tomcat/bin:$PATH" > /etc/profile.d/tomcat.sh
[19:21:41 root@202 /usr/local]#. /etc/profile.d/tomcat.sh
[19:21:46 root@202 /usr/local]#echo $PATH
/usr/local/tomcat/bin:/data/scripts:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/jdk/bin:/root/bin
catalina.sh 脚本
[19:21:51 root@202 /usr/local]#catalina.sh
Using CATALINA_BASE: /usr/local/tomcat #变量会自动创建
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/jdk
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
...
stop Stop Catalina, waiting up to 5 seconds for the process to end #默认关闭Tomcat需要等待5秒, 5秒之后开始释放资源, 因此, 关闭Tomcat后, 建议稍等几秒再开启, 防止数据丢失或者异常关闭,导致下次无法启动Tomcat
stop n Stop Catalina, waiting up to n seconds for the process to end #也可以手动指定
[17:20:55 root@202 /usr/local]#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/jdk/jre
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Server version: Apache Tomcat/8.5.59
Server built: Oct 6 2020 16:57:18 UTC
Server number: 8.5.59.0
OS Name: Linux
OS Version: 4.18.0-193.el8.x86_64
Architecture: amd64
JVM Version: 1.8.0_271-b09
JVM Vendor: Oracle Corporation
startup.sh 脚本
- 用来启动Tomcat, 内部会调用catalina.sh
[19:22:35 root@202 /usr/local]#startup.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/jdk
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.
- shutdown.sh: 用来关闭Tomcat
监听端口
[19:24:59 root@202 /usr/local]#ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 100 *:8080 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 1 [::ffff:127.0.0.1]:8005 *:*
查看Tomcat是以root身份运行的一个java程序, 来调用Tomcat信息
[19:25:35 root@202 /usr/local]#ps aux | grep tomcat
root 1372 3.2 10.1 2152924 81988 pts/0 Sl 19:24 0:04 /usr/local/jdk/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 -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -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/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap start
root 1419 0.0 0.1 12108 1044 pts/0 R+ 19:26 0:00 grep --color=auto tomcat
创建Service文件
- Service文件可以从yum安装的版本里面拷贝
#创建tomcat用户
[19:31:11 root@202 ~]#useradd -r -s /sbin/nologin tomcat
#准备service文件中的环境变量
[19:31:26 root@202 ~]#vim /usr/local/tomcat/conf/tomcat.conf
JAVA_HOME=/usr/local/jdk # 如果是yum安装的openjdk或者oraclejdk, 那么JAVA_HOME为/usr/java/default
#JRE_HOME=/usr/local/jdk/jre
#两个变量至少设置一项才能启动 tomcat
#修改工作目录权限, $CATALINA_BASE
[19:37:30 root@202 ~]#chown -R tomcat.tomcat /usr/local/tomcat/ #要补全目录才能递归修改
#创建service文件
[19:38:11 root@202 ~]#vim /lib/systemd/system/tomcat.service
[Unit]
Description=Tomcat
[Unit]
Description=Tomcat
After=syslog.target network.target
[Service]
Type=forking
EnvironmentFile=/usr/local/tomcat/conf/tomcat.conf
ExecStart=/usr/local/tomcat/bin/startup.sh
ExecStop=/usr/local/tomcat/bin/shutdown.sh
PrivateTmp=true
User=tomcat
Group=tomcat
[Install]
WantedBy=multi-user.target
加载daemon, 先用shutdown.sh关闭Tomcat, 5秒后用systemctl启动tomcat
[19:42:23 root@202 ~]#systemctl daemon-reload
[17:34:02 root@202 /usr/local]#shutdown.sh
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/local/jdk/jre
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:
[19:42:34 root@202 ~]#systemctl enable --now tomcat
Created symlink /etc/systemd/system/multi-user.target.wants/tomcat.service → /usr/lib/systemd/system/tomcat.service.
[19:42:45 root@202 ~]#ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 0.0.0.0:22 0.0.0.0:*
LISTEN 0 100 *:8080 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 1 [::ffff:127.0.0.1]:8005 *:*
注意:
Tomcat的核心分为3个部分:
- Web容器: 处理静态页面;
- JSP容器: 把jsp页面翻译成一般的 servlet
- catalina: 是一个 servlet 容器, 用于处理 servlet
2.2.2 一键部署JDK和Tomcat安装脚本
#!/bin/bash
. /etc/init.d/functions
DIR=`pwd`
JDK_FILE="jdk-8u271-linux-x64.tar.gz"
TOMCAT_FILE="apache-tomcat-8.5.59.tar.gz"
JDK_DIR="/usr/local"
TOMCAT_DIR="/usr/local"
install_jdk(){
if ! [ -f "$DIR/$JDK_FILE" ]; then
action "$JDK_FILE 文件不存在" false
exit;
elif [ -d $JDK_DIR/jdk ]; then
action "JDK 已经安装" false
exit;
else
[ -d "$JDK_DIR" ] || mkdir -pv $JDK_DIR
fi
tar xvf $DIR/$JDK_FILE -C $JDK_DIR
cd $JDK_DIR && ln -s jdk1.8* jdk
cat > /etc/profile.d/jdk.sh <<EOF
export JAVA_HOME=$JDK_DIR/jdk
export JRE_HOME=\$JAVA_HOME/jre
export CLASSPATH=\$JAVA_HOME/lib/:\$JRE_HOME/lib
export PATH=\$PATH:\$JAVA_HOME/bin
EOF
. /etc/profile.d/jdk.sh
java -version && action "JDK 安装完成" || { action "JDK 安装失败" false ; exit; }
}
install_tomcat(){
if ! [ -f "$DIR/$TOMCAT_FILE" ];then
action "$TOMCAT_FILE 文件不存在" false
exit;
elif [ -d $TOMCAT_DIR/tomcat ];then
action "TOMCAT 已经安装" false
exit;
else
[ -d "$TOMCAT_DIR" ] || mkdir -pv $TOMCAT_DIR
fi
tar xf $DIR/$TOMCAT_FILE -C $TOMCAT_DIR
cd $TOMCAT_DIR && ln -s apache-tomcat-* tomcat
echo "PATH=$TOMCAT_DIR/tomcat/bin:"'$PATH' > /etc/profile.d/tomcat.sh
id tomcat &> /dev/null || useradd -r -s /sbin/nologin tomcat
cat > $TOMCAT_DIR/tomcat/conf/tomcat.conf <<EOF
JAVA_HOME=$JDK_DIR/jdk
EOF
chown -R tomcat.tomcat ${TOMCAT_DIR}/tomcat/
cat > /lib/systemd/system/tomcat.service <<EOF
[Unit]
Description=Tomcat
After=syslog.target network.target
[Service]
Type=forking
EnvironmentFile=$TOMCAT_DIR/tomcat/conf/tomcat.conf
ExecStart=$TOMCAT_DIR/tomcat/bin/startup.sh
ExecStop=$TOMCAT_DIR/tomcat/bin/shutdown.sh
RestartSec=3
PrivateTmp=true
User=tomcat
Group=tomcat
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now tomcat.service
systemctl is-active tomcat.service &> /dev/null && action "TOMCAT 安装完成" || { action "TOMCAT 安装失败" false; exit; }
}
install_jdk
install_tomcat