目录
脚本安装JDK
Oracle JDK,Java SE 8u201/202 之后的版本商用需要收费。
OpenJDK是Sun公司发布的JDK开源版本。
##脚本一键安装
##包放到/root目录下
[root@anolis-32 ~]$cat install_jdk.sh
#!/bin/bash
set -o nounset
set -o errexit
DIR="/root"
JDK_FILE="jdk-8u371-linux-x64.tar.gz"
JDK_DIR="/usr/local"
install_jdk(){
if [ ! -f "$DIR/$JDK_FILE" ];then
echo "$JDK_FILE 不存在"
exit;
elif [ -d $JDK_DIR/jdk ];then
echo "JDK 已经安装"
exit
else
[ -d "$JDK_DIR" ] || mkdir -pv $JDK_DIR
fi
tar xf $DIR/$JDK_FILE -C $JDK_DIR
cd $JDK_DIR && ln -s jdk* jdk
cat > /etc/profile.d/jdk.sh <<EOF
export JAVA_HOME=$JDK_DIR/jdk
export PATH=\$PATH:\$JAVA_HOME/bin
#export JRE_HOME=\$JAVA_HOME/jre
#export CLASSPATH=.:\$JAVA_HOME/lib/:\$JRE_HOME/lib/
EOF
. /etc/profile.d/jdk.sh
java -version && echo "JDK 安装完成" || { echo "JDK 安装失败" ; exit; }
}
install_jdk
二进制安装tomcat
##安装tomcat 前必须先部署JDK
##官网:https://tomcat.apache.org/download-80.cgi
##清华:https://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/
[root@anolis31 ~]$cat install_tomcat.sh
#!/bin/bash
set -o nounset
set -o errexit
##文件和执行脚本都放到/root下执行
TOMCAT_FILE="apache-tomcat-9.0.75.tar.gz"
JDK_DIR="/usr/local"
TOMCAT_DIR="/usr/local"
DIR="/root"
install_tomcat(){
if ! [ -f "$DIR/$TOMCAT_FILE" ];then
echo "$TOMCAT_FILE 文件不存在"
exit;
elif [ -d $TOMCAT_DIR/tomcat ];then
echo "TOMCAT 已经安装"
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 &> /dev/null
systemctl is-active tomcat.service &> /dev/null && echo "TOMCAT 安装完成" || {
echo "TOMCAT 安装失败" ; exit; }
}
install_tomcat
#############################################################
##安装后验证,本机验证和外网访问
[root@anolis31 ~]$ss -ntl | grep 8080
LISTEN 0 100 *:8080 *:*
##访问成功
[root@anolis-32 ~]$curl 10.0.0.31:8080 &> /dev/null ;echo $?
0
tomcat文件结构和组成
bin 服务启动、停止等程序
lib 库目录
logs 日志目录
conf 配置文件
webapps 部署目录
work 部署后,提前预热访问,升级后,删除此目录数据才能更新
##catalina.sh为程序工具包,重进终端可以直接输入命令
[root@anolis31 ~]$bash /usr/local/tomcat/bin/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
Using CATALINA_OPTS:
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
部署java应用
WebApp应用的归档格式.war,.jar
war格式,部署到Tomcat的webapps目录下,会自动解包展开和部署上线。
#conf/server.xml中文件配置
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true">
范例:部署基于JAVA的博客系统JPress
##下载war包,拷贝到webapps目录下,注意ROOT目录是放默认内容的,war包到webapps后会自动部署,然后生成软连接
##jpress-v5.0.7版本需要放ROOT目录下,其它版本未知
##将tomcat停止后,将webapps项目ROOT删除,将jpress改名为ROOT.war,这样启动时就生成ROOT默认目录了
[root@anolis31 webapps]$systemctl stop tomcat.service
[root@anolis31 webapps]$rm -rf ROOT
[root@anolis31 ~]$cp jpress-v5.0.7.war /usr/local/tomcat/webapps
[root@anolis31 webapps]$mv jpress-v5.0.7.war ROOT.war
[root@anolis31 webapps]$systemctl start tomcat
[root@anolis31 webapps]$ll
总用量 124884
drwxr-x--- 6 tomcat tomcat 86 7月 1 22:10 ROOT
-rw-r--r-- 1 root root 127880979 7月 1 22:10 ROOT.war
##服务器安装数据库,开机自启,进行配置
[root@anolis-32 ~]$syum -y install mariadb-server
[root@anolis-32 ~]$systemctl enable --now mariadb.service
[root@anolis-32 ~]$mysql -e 'create database jpress;'
[root@anolis-32 ~]$mysql -e "create user jpress@'%' identified by '123';"
[root@anolis-32 ~]$mysql -e "grant all on jpress.* to jpress@'%';"
##浏览器访问http://10.0.0.31:8080/,出现安装页面,配置数据库后可以使用
##发布文章,上传图片,发布评论都可以了,服务器也能查到图片
[root@anolis31 webapps]$ll ROOT/attachment/20240701/a366b8d8771046d692245741e74c2d82.png
-rw-r--r-- 1 tomcat tomcat 472476 7月 1 22:11 ROOT/attachment/20240701/a366b8d8771046d692245741e74c2d82.png
范例:关闭8005端口
##8005是Tomcat的管理端口,默认监听在127.0.0.1上。无需验证就可发送SHUTDOWN (大小写敏感)这个字符串,tomcat接收到后就会关闭此Server。
##设为-1,将关闭此功能,此行不能被注释,否则无法启动tomcat。
[root@anolis31 tomcat]$sed -i.bak '/SHUTDOWN/s/8005/-1/' /usr/local/tomcat/conf/server.xml
标签:服务,JDK,tomcat,##,TOMCAT,root,DIR
From: https://www.cnblogs.com/szlhwei/p/18287288