tomcat 前言
Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP 程序的首选。对于一个初学者来说,可以这样认为,当在一台机器上配置好Apache 服务器,可利用它响应HTML(标准通用标记语言下的一个应用)页面的访问请求。实际上Tomcat是Apache 服务器的扩展,但运行时它是独立运行的,所以当你运行tomcat 时,它实际上作为一个与Apache 独立的进程单独运行的。
java 前言
Java具有大部分编程语言所共有的一些特征,被特意设计用于互联网的分布式环境。Java具有类似于C++语言的形式和感觉,但它要比C++语言更易于使用,而且在编程时彻底采用了一种以对象为导向的方式。
tomcat + java下载
tomcat下载 | java 下载 |
download | download |
Linux 各系统下载使用参考
Red Hat | Rocky Linux | Oracle Linux | AlmaLinux | ubuntu | suselinux | esxi | RHEL标准安装 | 系统安装参考 | YUM参考 | MobaXterm 远程连接工具 | Red Hat Enterprise 9.0 文档 | Kickstart 生成器 |
download | download | download | download | download | download | download | 参考 | 参考 | 配置参考 | download | 参考 | Kickstart |
版本兼容性 |
安装 tomcat+java
-
创建安装自动化脚本
- 实现在线下载tomcat,java,解压安装tomcat,java,环境变量设置,端口更改8082,开启(Server Status ,Manager App,Host Manager 用户名密码admin/admin)删除软件包,企业微信机器人通知
- 以下基于Redhat系统
- tomcat 安装位置 /opt/tomcat
- java 安装位置/opt/jdk
- Redhat 9 functions使用需要执行安装yum install initscripts -y
- curl 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=XXXXX' #更改自己的企业微信机器人地址
- curl -o /etc/yum.repos.d/redhat.repo http://mirrors.aliyun.com/repo/Centos-8.repo #阿里在线repo
- yum install figlet -y #用于将文字转换为放大艺术字(使用figlet Mysql显示)
vim /tomcat_java_install.sh
#!/bin/sh
# -*- coding: utf-8 -*-
# Author: CIASM
# Date: 2023/04/02
<<!
_____ _
|_ _|__ _ __ ___ ___ __ _| |_
| |/ _ \| '_ ` _ \ / __/ _` | __|
| | (_) | | | | | | (_| (_| | |_
|_|\___/|_| |_| |_|\___\__,_|\__|
!
source /etc/rc.d/init.d/functions
#tomcat configuration file
server_xml=/opt/tomcat/conf/server.xml
tomcat_user_xml=/opt/tomcat/conf/tomcat-users.xml
manager_xml=/opt/tomcat/conf/Catalina/localhost/manager.xml
context_xml=/opt/tomcat/webapps/host-manager/META-INF/context.xml
#Define data path variables
data_downloads=/data/downloads
drive=/opt/
tomcat_url=https://dlcdn.apache.org/tomcat/tomcat-10/v10.1.7/bin/
tomcat_gz=apache-tomcat-10.1.7.tar.gz
tomcat_catalogue=apache-tomcat-10.1.7
tomcat_new=tomcat
tomcat_PREFIX=/opt/tomcat
java_url=https://repo.huaweicloud.com/java/jdk/13+33/
java_gz=jdk-13_linux-x64_bin.tar.gz
JAVA_PREFIX=/opt/jdk
java_catalogue=jdk-13
java_catalogue_new=jdk
function install_java (){
if [ ! -d ${JAVA_PREFIX} ];then
yum install net-tools wget -y
mkdir -p $data_downloads
wget -N -P $data_downloads $java_url$java_gz
tar -zxf $data_downloads/$java_gz -C $drive
mv $drive/$java_catalogue $drive/$java_catalogue_new
if [ $? -eq 0 ];then
sed -i '$aexport JAVA_HOME=/opt/jdk' /etc/profile
sed -i '$aexport PATH=$PATH:$JAVA_HOME/bin:$PATH:$CATALINA_HOME/bin' /etc/profile
source /etc/profile
java -version 2>&1 | tee /var/log/java_version.log
action "The java Install Sussess..." /bin/true
else
action "The java Install Failed..." /bin/false
exit 1
fi
else
echo -e "\033[31mThe java already Install...\033[0m"
fi
}
function install_tomcat (){
if [ ! -d ${tomcat_PREFIX} ];then
wget -N -P $data_downloads $tomcat_url$tomcat_gz
tar -zxf $data_downloads/$tomcat_gz -C $drive
mv $drive/$tomcat_catalogue $drive/$tomcat_new
useradd -M -d /opt/tomcat tomcat
chown -R tomcat. /opt/tomcat
chmod +x /opt/tomcat/bin/*.sh
if [ $? -eq 0 ];then
sed -i '$aexport CATALINA_HOME=/opt/tomcat' /etc/profile
source /etc/profile
rm -rf /opt/tomcat/conf/tomcat-users.xml
rm -rf /opt/tomcat/webapps/host-manager/META-INF/context.xml
rm -rf /opt/tomcat/conf/server.xml
/opt/tomcat/bin/version.sh | tee /var/log/tomcat_version.log
Configurin_tomcat
manager_xml
action "The tomcat Install Sussess..." /bin/true
else
action "The tomcat Install Failed..." /bin/false
exit 1
fi
else
echo -e "\033[31mThe tomcat already Install...\033[0m"
fi
}
function Configurin_tomcat (){
cat >>/usr/lib/systemd/system/tomcat.service<<EOF
[Unit]
Description=Apache Tomcat Web Application Container
After=syslog.target network.target mysqld.service tomcat.keygen.service
[Service]
Type=forking
Environment=JAVA_HOME=/opt/jdk
Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINA_BASE=/opt/tomcat
Environment='CATALINA_OPTS=-Xms4096M -Xmx4096M -server -XX:+UseParallelGC'
Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom'
ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh
User=tomcat
Group=tomcat
Restart=always
[Install]
WantedBy=multi-user.target
EOF
cat >>$tomcat_user_xml<<EOF
<?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.
-->
<tomcat-users xmlns="http://tomcat.apache.org/xml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd"
version="1.0">
<!--
By default, no user is included in the "manager-gui" role required
to operate the "/manager/html" web application. If you wish to use this app,
you must define such a user - the username and password are arbitrary.
Built-in Tomcat manager roles:
- manager-gui - allows access to the HTML GUI and the status pages
- manager-script - allows access to the HTTP API and the status pages
- manager-jmx - allows access to the JMX proxy and the status pages
- manager-status - allows access to the status pages only
The users below are wrapped in a comment and are therefore ignored. If you
wish to configure one or more of these users for use with the manager web
application, do not forget to remove the <!.. ..> that surrounds them. You
will also need to set the passwords to something appropriate.
-->
<!--
<user username="admin" password="<must-be-changed>" roles="manager-gui"/>
<user username="robot" password="<must-be-changed>" roles="manager-script"/>
-->
<!--
The sample user and role entries below are intended for use with the
examples web application. They are wrapped in a comment and thus are ignored
when reading this file. If you wish to configure these users for use with the
examples web application, do not forget to remove the <!.. ..> that surrounds
them. You will also need to set the passwords to something appropriate.
-->
<!--
<role rolename="tomcat"/>
<role rolename="role1"/>
<user username="tomcat" password="tomcat" roles="tomcat"/>
<user username="both" password="tomcat" roles="tomcat,role1"/>
<user username="role1" password="tomcat" roles="role1"/>
-->
<user username="admin" password="admin" roles="manager-gui,admin-gui"/>
</tomcat-users>
EOF
cat >>$context_xml<<EOF
<?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 privileged="true" antiResourceLocking="false"
docBase="$catalina.home /webapps/manager">
<CookieProcessor className="org.apache.tomcat.util.http.Rfc6265CookieProcessor"
sameSiteCookies="strict" />
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="^.*$" />
<Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/>
</Context>
EOF
cat >>$server_xml<<EOF
<?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.
-->
<!-- Note: A "Server" is not itself a "Container", so you may not
define subcomponents such as "Valves" at this level.
Documentation at /docs/config/server.html
-->
<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<!-- Security listener. Documentation at /docs/config/listeners.html
<Listener className="org.apache.catalina.security.SecurityListener" />
-->
<!-- APR library loader. Documentation at /docs/apr.html -->
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<!-- Prevent memory leaks due to use of particular java/javax APIs-->
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />
<!-- Global JNDI resources
Documentation at /docs/jndi-resources-howto.html
-->
<GlobalNamingResources>
<!-- Editable user database that can also be used by
UserDatabaseRealm to authenticate users
-->
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
<!-- A "Service" is a collection of one or more "Connectors" that share
a single "Container" Note: A "Service" is not itself a "Container",
so you may not define subcomponents such as "Valves" at this level.
Documentation at /docs/config/service.html
-->
<Service name="Catalina">
<!--The connectors can use a shared executor, you can define one or more named thread pools-->
<!--
<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
maxThreads="150" minSpareThreads="4"/>
-->
<!-- A "Connector" represents an endpoint by which requests are received
and responses are returned. Documentation at :
HTTP Connector: /docs/config/http.html
AJP Connector: /docs/config/ajp.html
Define a non-SSL/TLS HTTP/1.1 Connector on port 8080
-->
<Connector port="8082" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<!-- A "Connector" using the shared thread pool-->
<!--
<Connector executor="tomcatThreadPool"
port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
-->
<!-- Define an SSL/TLS HTTP/1.1 Connector on port 8443 with HTTP/2
This connector uses the NIO implementation. The default
SSLImplementation will depend on the presence of the APR/native
library and the useOpenSSL attribute of the AprLifecycleListener.
Either JSSE or OpenSSL style configuration may be used regardless of
the SSLImplementation selected. JSSE style configuration is used below.
-->
<!--
<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
maxThreads="150" SSLEnabled="true">
<UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />
<SSLHostConfig>
<Certificate certificateKeystoreFile="conf/localhost-rsa.jks"
type="RSA" />
</SSLHostConfig>
</Connector>
-->
<!-- Define an AJP 1.3 Connector on port 8009 -->
<!--
<Connector protocol="AJP/1.3"
address="::1"
port="8009"
redirectPort="8443" />
-->
<!-- An Engine represents the entry point (within Catalina) that processes
every request. The Engine implementation for Tomcat stand alone
analyzes the HTTP headers included with the request, and passes them
on to the appropriate Host (virtual host).
Documentation at /docs/config/engine.html -->
<!-- You should set jvmRoute to support load-balancing via AJP ie :
<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">
-->
<Engine name="Catalina" defaultHost="localhost">
<!--For clustering, please take a look at documentation at:
/docs/cluster-howto.html (simple how to)
/docs/config/cluster.html (reference documentation) -->
<!--
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
-->
<!-- Use the LockOutRealm to prevent attempts to guess user passwords
via a brute-force attack -->
<Realm className="org.apache.catalina.realm.LockOutRealm">
<!-- This Realm uses the UserDatabase configured in the global JNDI
resources under the key "UserDatabase". Any edits
that are performed against this UserDatabase are immediately
available for use by the Realm. -->
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<!-- SingleSignOn valve, share authentication between web applications
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.authenticator.SingleSignOn" />
-->
<!-- Access log processes all example.
Documentation at: /docs/config/valve.html
Note: The pattern used is equivalent to using pattern="common" -->
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b" />
</Host>
</Engine>
</Service>
</Server>
EOF
systemctl daemon-reload && systemctl enable --now tomcat
firewall-cmd --zone=public --add-port=8082/tcp --permanent && firewall-cmd --reload
}
function manager_xml (){
cat >>$manager_xml<<EOF
<?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 privileged="true" antiResourceLocking="false"
docBase="$catalina.home /webapps/manager">
<CookieProcessor className="org.apache.tomcat.util.http.Rfc6265CookieProcessor"
sameSiteCookies="strict" />
<Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="^.*$" />
<Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/>
</Context>
EOF
systemctl restart tomcat
rm -rf $data_downloads/*
}
function Deployment_completion_notification (){
host_ID=`dmidecode -s system-serial-number | sed -r 's/\s+//g'`
host_IP=`ifconfig -a | grep inet | grep -v '127.0.0.1' | awk '{ print $2}' | awk 'NR==1'`
memory_Size=`dmidecode -t memory | grep Size | grep -v No | awk '{sum+=$2} END {printf "%.0fG\n",sum/1^C4}'`
CPU_Model=`cat /proc/cpuinfo | grep 'model name' | awk '{print $6}' | uniq`
Disk_size=`fdisk -l | grep "sda:" | awk '{print $3$4}'`
redhat_version=`cat /etc/redhat-release | grep "release" | awk '{print $6}'`
redhat_core=`cat /proc/version | grep "version" | awk '{print $3}'`
tomcat_version=`cat /var/log/tomcat_version.log | grep "Server version" | awk '{print $4}'`
tomcat_server=`systemctl status tomcat | grep "Active" | awk '{print $2}'`
java_version=`cat /var/log/java_version.log | sed -e 's/"//g' | grep "java version" | awk '{print $3}'`
curl 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=XXXX' \
-H 'Content-Type: application/json' \
-d '
{
"msgtype": "markdown",
"markdown": {
"content": " **system check** <font color=\"info\"> complete </font> \n
> **Host IP Address** \n
[http://'$host_IP'](http://'$host_IP') \n
> **Hardware information** \n
hostSN:<font color=\"info\"> '$host_ID' </font> \n
CPU_Model:<font color=\"info\"> '$CPU_Model' </font> \n
memory_Size:<font color=\"info\"> '$memory_Size' </font> \n
Disk_size:<font color=\"info\"> '$Disk_size' </font> \n
System_version:<font color=\"info\"> '$redhat_version' </font> \n
system_core:<font color=\"info\"> '$redhat_core' </font> \n
> **tomcat install** \n
tomcat_server:<font color=\"info\"> '$tomcat_server' </font> \n
tomcat_version:<font color=\"info\"> '$tomcat_version' </font> \n
java_server:<font color=\"info\"> '$java_version' </font> \n",
}
}'
}
function main (){
install_java
install_tomcat
Deployment_completion_notification
}
main
执行安装
sh /tomcat_java_install.sh
企业微信机器人提醒
访问 http://Ip:8082
Server Status
Manager App
Host Manager
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)