Install Tomcat on CentOS 8

This is an easy one. Note that this article only covers the installation but not the configuration of Tomcat.

You can copy and paste the below lines straight into a terminal and everything should be installed.

# Step 1: Installing Java for Tomcat
dnf -y install java-11-openjdk-devel
java --version # Verify that java is installed correctly

# Step 2: Install Tomcat and make it as a system service
export TOMCAT_VER="9.0.30"
groupadd --system tomcat
useradd -d /opt/tomcat -r -s /bin/false -g tomcat tomcat
dnf -y install wget
wget https://archive.apache.org/dist/tomcat/tomcat-9/v${TOMCAT_VER}/bin/apache-tomcat-${TOMCAT_VER}.tar.gz
tar xvf apache-tomcat-${TOMCAT_VER}.tar.gz -C /opt/
rm -f /opt/tomcat
ln -s /opt/apache-tomcat-${TOMCAT_VER}/ /opt/tomcat
chown -R tomcat:tomcat /opt/tomcat
chown -R tomcat:tomcat /opt/apache-tomcat-${TOMCAT_VER}/
cat > /etc/systemd/system/tomcat.service << EOF
[Unit]
Description=Tomcat Server
After=syslog.target network.target

[Service]
Type=forking
User=tomcat
Group=tomcat

#Environment=JAVA_HOME=/usr/lib/jvm/jre
Environment='JAVA_OPTS=-Djava.awt.headless=true'
Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINA_BASE=/opt/tomcat
Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M'
ExecStart=/opt/tomcat/bin/catalina.sh start
ExecStop=/opt/tomcat/bin/catalina.sh stop

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl start tomcat
systemctl enable tomcat

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