在CentOS 7上安装Apache Tomcat 10.0.27
#!/bin/bash
# 安装Apache Tomcat 10.0.27
# 定义Tomcat版本和下载链接
TOMCAT_VERSION="10.0.27"
TOMCAT_DOWNLOAD_URL="https://downloads.apache.org/tomcat/tomcat-10/v${TOMCAT_VERSION}/bin/apache-tomcat-${TOMCAT_VERSION}.tar.gz"
# 安装必要的软件包
sudo yum install -y java-11-openjdk-devel
# 创建Tomcat用户
sudo useradd -r -m -U -d /opt/tomcat -s /bin/false tomcat
# 下载并解压Tomcat
cd /tmp
wget "${TOMCAT_DOWNLOAD_URL}"
tar xzf apache-tomcat-"${TOMCAT_VERSION}".tar.gz -C /opt
sudo ln -s /opt/apache-tomcat-"${TOMCAT_VERSION}" /opt/tomcat
sudo chown -RH tomcat: /opt/tomcat
# 设置环境变量
echo "CATALINA_HOME=/opt/tomcat" | sudo tee -a /etc/profile.d/tomcat.sh
sudo chmod +x /etc/profile.d/tomcat.sh
sudo source /etc/profile.d/tomcat.sh
# 创建systemd服务文件
sudo tee /etc/systemd/system/tomcat.service <<EOF
[Unit]
Description=Tomcat 10 Web Application Container
After=network.target
[Service]
Type=forking
Environment=JAVA_HOME=/usr/lib/jvm/jre
Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINA_BASE=/opt/tomcat
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -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
UMask=0007
RestartSec=10
Restart=always
[Install]
WantedBy=multi-user.target
EOF
# 重新加载systemd配置,启动Tomcat服务,并设置开机自启
sudo systemctl daemon-reload
sudo systemctl start tomcat.service
sudo systemctl enable tomcat.service
# 检查Tomcat服务状态
sudo systemctl status tomcat.service
这段脚本会在CentOS 7上安装Apache Tomcat 10.0.27,包括下载、解压缩、配置用户、设置环境变量、创建systemd服务文件,并启用开机自启。确保在执行这段脚本之前,系统已经安装了wget
和systemd
。
评论已关闭