Centos7下Tomcat环境安装及使用脚本安装

2022-12-21 19:09:25 浏览数 (2)

一、安装Java yum install java-1.8.0-openjdk.x86_64 安装完毕后,运行java -version 将输出如下内容: openjdk version "1.8.0_91" OpenJDK Runtime Environment (build 1.8.0_91-b14) OpenJDK 64-Bit Server VM (build 25.91-b14, mixed mode) 二、添加tomcat用户 groupadd tomcat useradd -s /bin/nologin -g tomcat -d /usr/local/tomcat tomcat 三、下载Tomcat wget http://www-us.apache.org/dist/tomcat/tomcat-8/v8.0.33/bin/apache-tomcat-8.0.33.tar.gz sudo tar -zxvf apache-tomcat-8.0.33.tar.gz -C /usr/local/tomcat --strip-components=1 cd /usr/local/tomcat chown -R tomcat . chgrp -R tomcat conf chmod g rwx conf chmod g r conf/* chown -R tomcat logs/ temp/ webapps/ work/ 四、为Tomcat创建systemd unit 文件 vi /etc/systemd/system/tomcat.service [Unit] Description=Apache Tomcat Web Application Container After=syslog.target 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=/bin/kill -15 $MAINPID User=tomcat Group=tomcat [Install] WantedBy=multi-user.target 五、 安装haveged yum install haveged systemctl start haveged.service && systemctl enable haveged.service 六、启动Tomcat 并开启防火墙 systemctl start tomcat.service && systemctl enable tomcat.service firewall-cmd --zone=public --permanent --add-port=8080/tcp && firewall-cmd --reload 访问 http://[Your-Host-IP]:8080 预览是否正常。 七、配置Tomcat 管理界面 vi /usr/local/tomcat/conf/tomcat-users.xml 在 <tomcat-users ...>...</tomcat-users>中加入一个admin账号: <user username="yourusername" password="yourpassword" roles="manager-gui,admin-gui"/> 重启Tomcat

sudo systemctl restart tomcat.service

附脚本

代码语言:javascript复制
#!/usr/bin/env bash
yum install java-1.8.0-openjdk.x86_64
groupadd tomcat && mkdir /usr/local/tomcat &&
useradd -s /bin/nologin -g tomcat -d /usr/local/tomcat tomcat
wget -c http://mirrors.shu.edu.cn/apache/tomcat/tomcat-8/v8.5.31/bin/apache-tomcat-8.5.31.tar.gz &&
tar -zxvf apache-tomcat-8.5.31.tar.gz -C /usr/local/ && cd /usr/local/ &&
mv apache-tomcat-8.5.31 tomcat && cd tomcat &&
chown -R tomcat . && chgrp -R tomcat conf && chmod g rwx conf && chmod g r conf/*
touch /etc/systemd/system/tomcat.service
#将一下代码添加到tomcat.service上去
cat>>/etc/systemd/system/tomcat.service<<EOF
[Unit]
Description=Apache Tomcat Web Application Container
After=syslog.target 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=/bin/kill -15 $MAINPID

User=tomcat
Group=tomcat

[Install]
WantedBy=multi-user.target
EOF
yum install haveged && systemctl start haveged.service &&systemctl enable haveged.service
firewall-cmd --zone=public --permanent --add-port=8080/tcp && firewall-cmd --reload
systemctl start tomcat.service &&systemctl enable tomcat.service

0 人点赞