安装OpenJDK

dnf install java-1.8.0-openjdk* -y

下载和安装Tomcat 9

wget https://www-eu.apache.org/dist/tomcat/tomcat-9/v9.0.36/bin/apache-tomcat-9.0.36.tar.gz -P /tmp
tar -xf /tmp/apache-tomcat-9.0.36.tar.gz -C /usr/local/tomcat/
chown -R root: /usr/local/tomcat/
sh -c 'chmod +x /usr/local/tomcat/apache-tomcat-9.0.36/bin/*.sh'

防火墙开启端口

firewall-cmd --permanent --zone=public --add-port=8080/tcp
firewall-cmd --reload

将Tomcat设置为作为服务运行

在/etc/systemd/system/目录中创建tomcat.service文件

vi /etc/systemd/system/tomcat.service

[Unit]
Description=Tomcat9 servlet
After=syslog.target network.target

[Service]
Type=forking

User=root
Group=root

Environment="JAVA_HOME=/usr/lib/jvm/jre"
Environment="JAVA_OPTS=-Djava.awt.headless=true"

Environment="CATALINA_BASE=/usr/local/tomcat/apache-tomcat-9.0.36"
Environment="CATALINA_HOME=/usr/local/tomcat/apache-tomcat-9.0.36"
Environment="CATALINA_PID=/usr/local/tomcat/apache-tomcat-9.0.36/temp/tomcat.pid"
Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"

ExecStart=/usr/local/tomcat/apache-tomcat-9.0.36/bin/startup.sh
ExecStop=/usr/local/tomcat/apache-tomcat-9.0.36/bin/shutdown.sh

[Install]
WantedBy=multi-user.target

chmod 777 /etc/systemd/system/tomcat.service

通知systemd刷新服务

systemctl daemon-reload

开机启动 tomcat

systemctl enable tomcat

启动 tomcat

systemctl start tomcat

打开网址

http://192.168.197.103:8080/

Centos8 安装Tomcat_JAVA

这时候打开管理界面manager webapp是访问不了的,因为只允许本机访问。需要进行下面设置

/usr/local/tomcat/apache-tomcat-9.0.36/conf/tomcat-users.xml 追加下面内容

<tomcat-users>
   <role rolename="manager"/>
   <role rolename="manager-gui"/>
   <role rolename="manager-script"/>  
   <role rolename="manager-status"/>
   <user username="tomcat" password="tomcat" roles="manager,manager-gui,manager-script,manager-status"/>
</tomcat-users>

tomcat为管理界面账号

默认情况下,Tomcat Web管理界面配置为仅允许从本地主机访问。

打开 /usr/local/tomcat/apache-tomcat-9.0.36/webapps/manager/META-INF/context.xml 文件并注释下面内容

<!-- <Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->

重启 tomcat

systemctl restart tomcat

之后管理界面可以登录

Centos8 安装Tomcat_JAVA_02