007 CentOS 7.9 apache-tomcat-9.0.89安装及配置
#!/bin/bash
# 安装Java环境
yum install java-1.8.0-openjdk-devel -y
# 验证Java安装
java -version
# 创建Tomcat用户
groupadd tomcat
useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat
# 解压Tomcat到/opt/tomcat
tar xf apache-tomcat-9.0.89.tar.gz -C /opt/
ln -s /opt/apache-tomcat-9.0.89 /opt/tomcat
# 设置权限
chown -R tomcat:tomcat /opt/tomcat
# 设置Tomcat的环境变量
cat >> /opt/tomcat/bin/setenv.sh <<EOF
CATALINA_HOME=/opt/tomcat
CATALINA_BASE=/opt/tomcat
CATALINA_TMPDIR=/opt/tomcat/temp
JRE_HOME=/usr/lib/jvm/java-1.8.0-openjdk
CLASSPATH=\$JRE_HOME/lib/rt.jar:\$CATALINA_HOME/lib/servlet-api.jar:\$CATALINA_HOME/lib/jsp-api.jar
EOF
chmod +x /opt/tomcat/bin/setenv.sh
# 设置为服务
cat >> /etc/systemd/system/tomcat.service <<EOF
[Unit]
Description=Tomcat 9 servlet container
After=network.target
[Service]
Type=forking
User=tomcat
Group=tomcat
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
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable tomcat.service
这段代码首先通过yum安装Java环境,然后创建了一个名为tomcat的用户来运行Tomcat服务。接着,它将Tomcat解压到/opt/目录并创建一个符号链接指向新的目录。然后设置相应的权限,并为Tomcat创建一个setenv.sh文件,以设置环境变量。最后,它将Tomcat配置为systemd服务,以便可以使用systemctl命令来管理Tomcat服务。
评论已关闭