Linux二进制安装JDK和Tomcat
Posted 白-胖-子
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux二进制安装JDK和Tomcat相关的知识,希望对你有一定的参考价值。
Java
- Sun公司发明的面向对象开发语言
JDK
- Java环境和开发工具,包含JRE
- 要使用JAVA装JDK就对了
- 因为Tomcat是Java写的,所以用Tomcat就必须要装JDK
Tomcat
- JAVA开发的轻量级的Web服务器
- 实质为JavaServlet的容器,
- 可以实现JSP动态应用,表现为Java程序自动生成html内容的功能
二进制安装JDK
1. 下载安装包
- 因为JAVA的SUN公司被Oracle收了,从8以后开始收费了
- 所以下载需要登录Oracle后再下载
- 编译好的二进制包为tar.gz后缀,linux-x64对应CentOS和Ununtu系列64位操作系统
https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html
2. 解压缩并做个软链接
tar xvf jdk-8u241-linux-x64.tar.gz -C /usr/local/
cd /usr/local/
ln -s jdk1.8.0_241/ jdk
3.初始化环境变量
- 注意:JAVA_HOME变量必须设置,否则tomcat启动时会报错
vim /etc/profile.d/jdk.sh
cat /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/
. /etc/profile.d/jdk.sh
不配环境变量出下面错误
Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
At least one of these environment variable is needed to run this program
4. 验证安装
java -version
java version "1.8.0_241"
Java(TM) SE Runtime Environment (build 1.8.0_241-b07)
Java HotSpot(TM) 64-Bit Server VM (build 25.241-b07, mixed mode)
which java
/usr/local/jdk/bin/java
一键安装JDK脚本
DIR=`pwd`
JDK_FILE="jdk-8u291-linux-x64.tar.gz"
JDK_DIR="/usr/local"
color () {
……
echo
}
install_jdk(){
……
java -version && color 0 "JDK 安装完成" || { color 1 "JDK 安装失败" ; exit; }
}
install_jdk
二进制安装Tomcat
- 注意: 安装tomcat 前必须先部署JDK
1. 官网或镜像网站下载:
wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.50/bin/apache-tomcat-8.5.50.tar.gz
2. 解压缩到指定文件夹并做软链接
tar xf apache-tomcat-8.5.50.tar.gz -C /usr/local/
cd /usr/local/
ln -s apache-tomcat-8.5.50/ tomcat
3. 指定PATH变量
echo 'PATH=/usr/local/tomcat/bin:$PATH' > /etc/profile.d/tomcat.sh
. /etc/profile.d/tomcat.sh
- 检查系统变量
echo $PATH
/usr/local/tomcat/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/jdk/bin:/root/bin
4. 查看当前变量设置和命令用法
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
Usage: catalina.sh ( commands ... )
commands:
debug Start Catalina in a debugger
debug -security Debug Catalina with a security manager
jpda start Start Catalina under JPDA debugger
run Start Catalina in the current window
run -security Start in the current window with security manager
start Start Catalina in a separate window
start -security Start in a separate window with security manager
stop Stop Catalina, waiting up to 5 seconds for the process to
end
stop n Stop Catalina, waiting up to n seconds for the process to
end
stop -force Stop Catalina, wait up to 5 seconds and then use kill -KILL
if still running
stop n -force Stop Catalina, wait up to n seconds and then use kill -KILL
if still running
configtest Run a basic syntax check on server.xml - check exit code for
result
version What version of tomcat are you running?
Note: Waiting for the process to end and use of the -force option require that
$CATALINA_PID is defined
5. 查看环境变量和版本信息
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_261-b12
JVM Vendor: Oracle Corporation
6. 启动tomcat
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/jre
Using CLASSPATH:
/usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
7. 查看端口
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
*:*
LISTEN 0 100 *:8009
*:*
8. 查看进程是以root启动的
ps aux|grep tomcat
root 12994 34.1 9.4 2155140 76912 pts/0 Sl 22:38 0:02
/usr/local/jdk/jre/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 13039 0.0 0.1 12108 1076 pts/0 R+ 22:38 0:00 grep --
color=auto tomcat
9. 关闭tomcat
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
或者以下也可以指定10s后停止,默认5s
catalina.sh stop 10
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 128 [::]:22
[::]:*
10. 再次用不同方式启动tomcat
catalina.sh start
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
Tomcat started.
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
*:*
LISTEN 0 100 *:8009
*:*
11. 再次用不同方式关闭tomcat
catalina.sh stop
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
配置 tomcat自启动的 service 文件
1 .环境和用户准备
- 创建tomcat专用帐户
useradd -r -s /sbin/nologin tomcat
- 给tomcat加目录更换所属主和属组
chown -R tomcat.tomcat /usr/local/tomcat/
- 准备service文件中相关环境文件
vim /usr/local/tomcat/conf/tomcat.conf
## 两个变量至少设置一项才能启动 tomcat
cat /usr/local/tomcat/conf/tomcat.conf
JAVA_HOME=/usr/local/jdk
#JRE_HOME=/usr/local/jdk/jre
- 如果不指定上面变量,/var/log/messages文件中会出现下面无法启动错误提示
Mar 15 14:30:09 centos8 startup.sh[1530]: Neither the JAVA_HOME nor the JRE_HOME
environment variable is defined
Mar 15 14:30:09 centos8 startup.sh[1530]: At least one of these environment
variable is needed to run this program
2。 创建tomcat.service文件
cat > /lib/systemd/system/tomcat.service <<SUN
[Unit]
Description=Tomcat
#After=syslog.target network.target remote-fs.target nss-lookup.target
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
SUN
3. 重新加载服务配置文件
systemctl daemon-reload
4. 启动并设为开机运行
systemctl enable --now tomcat
Created symlink /etc/systemd/system/multi-user.target.wants/tomcat.service →/usr/lib/systemd/system/tomcat.service.
5. 查看tomcat服务状态
systemctl status tomcat
● tomcat.service - Tomcat
Loaded: loaded (/usr/lib/systemd/system/tomcat.service; enabled; vendor
preset: disabled)
Active: active (running) since Sat 2020-02-08 23:37:02 CST; 5s ago
Process: 14312 ExecStart=/usr/local/tomcat/bin/startup.sh (code=exited,
status=0/SUCCESS)
Main PID: 14320 (java)
Tasks: 43 (limit: 4895)
Memory: 64.2M
CGroup: /system.slice/tomcat.service
└─14320 /usr/local/jdk/jre/bin/java -
Djava.util.logging.config.file=/usr/local/tomcat/conf/logging>
Feb 08 23:37:02 centos8.localdomain systemd[1]: Starting Tomcat...
Feb 08 23:37:02 centos8.localdomain systemd[1]: Started Tomcat.
6. 查看日志
tail /var/log/messages
Mar 15 14:32:13 centos8 systemd[1]: Reloading.
Mar 15 14:32:23 centos8 systemd[1]: Starting Tomcat...
Mar 15 14:32:23 centos8 startup.sh[1575]: Tomcat started.
Mar 15 14:32:23 centos8 systemd[1]: Started Tomcat.
一键安装tomcat脚本
. /etc/init.d/functions
### 定义变量
DIR=`pwd`
JDK_FILE="jdk-8u291-linux-x64.tar.gz"
TOMCAT_FILE="apache-tomcat-8.5.57.tar.gz"
JDK_DIR="/usr/local"
TOMCAT_DIR="/usr/local"
### 安装JDK
install_jdk(){
}
### 安装tomcat
install_tomcat(){
### 安装文件检查
### 解包并做软链接
### 添加环境变量
### 创建tomcat用户并赋权
### 在tomcat配置文件中写JDK路径
### 创建tomcat启动文件
### 重新加载服务并启动
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
以上是关于Linux二进制安装JDK和Tomcat的主要内容,如果未能解决你的问题,请参考以下文章
Linux一键部署Java环境JDK+Tomcat二进制安装脚本(亲测CentOS和Ubuntu通用)