This post will cover installing Tomcat 9 on CentOS 8.
We’ll be using a bash script below, which is commented so you can see the steps.
We’ll install the latest stable release of Tomcat 9.x from Tomcat.org, as well as OpenJDK 8.
We’ll also update the tomcat-users.xml file to enable manager-gui and admin-gui roles and set their passwords to a random string.
Finally, we’ll create a service file and enable Tomcat to run as a systemd service.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
#!/bin/bash -e # Install Tomcat 9 on CentOS 8 # Save file as tomcat9-centos8.sh # Usage: ./tomcat9-centos8.sh # Main Function function install_tomcat(){ if [ $(grep -cm 1 tomcat /etc/passwd) -eq 0 ]; then useradd -m tomcat fi cd /home/tomcat # Install latest stable Tomcat 9 if [ ! -d apache-tomcat-${TOMCAT_VER} ]; then if [ ! -f apache-tomcat-${TOMCAT_VER}.tar.gz ]; then wget http://www.us.apache.org/dist/tomcat/tomcat-9/v${TOMCAT_VER}/bin/apache-tomcat-${TOMCAT_VER}.tar.gz fi tar xzf apache-tomcat-${TOMCAT_VER}.tar.gz chown -R tomcat:tomcat apache-tomcat-${TOMCAT_VER} rm -rf apache-tomcat-${TOMCAT_VER}.tar.gz fi # Add CATALINA_HOME to environment if [ $(grep -m 1 -c CATALINA_HOME /etc/environment) -eq 0 ]; then cat >>/etc/environment <<EOF export CATALINA_HOME=/home/tomcat/apache-tomcat-${TOMCAT_VER} export CATALINA_BASE=/home/tomcat/apache-tomcat-${TOMCAT_VER} EOF fi cat >>/home/tomcat/apache-tomcat-${TOMCAT_VER}/bin/setenv.sh <<CMD_EOF CATALINA_PID="/home/tomcat/apache-tomcat-${TOMCAT_VER}/temp/tomcat.pid" CMD_EOF # Create passwords for manager-admin and manager-gui and update tomcat-users.xml file TOMCAT_MANAGER_PASS=$(< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c32); TOMCAT_ADMIN_PASS=$(< /dev/urandom tr -dc _A-Z-a-z-0-9 | head -c32); if [ $(grep -m 1 -c 'tomcat manager pass' /root/auth.txt) -eq 0 ]; then echo "tomcat manager pass: ${TOMCAT_MANAGER_PASS}" >> /root/auth.txt else sed -i.save "s/tomcat manager pass: .*/tomcat manager pass: ${TOMCAT_MANAGER_PASS}/" /root/auth.txt fi if [ $(grep -m 1 -c 'tomcat admin pass' /root/auth.txt) -eq 0 ]; then echo "tomcat admin pass: ${TOMCAT_ADMIN_PASS}" >> /root/auth.txt else sed -i.save "s/tomcat admin pass: .*/tomcat admin pass: ${TOMCAT_ADMIN_PASS}/" /root/auth.txt fi cat >/home/tomcat/apache-tomcat-${TOMCAT_VER}/conf/tomcat-users.xml <<EOF <?xml version='1.0' encoding='utf-8'?> <tomcat-users> <role rolename="manager-gui" /> <user username="manager" password="${TOMCAT_MANAGER_PASS}" roles="manager-gui" /> <role rolename="admin-gui" /> <user username="admin" password="${TOMCAT_ADMIN_PASS}" roles="manager-gui,admin-gui" /> </tomcat-users> EOF mkdir -p /home/tomcat/apache-tomcat-${TOMCAT_VER}/conf/Catalina/localhost/ cat >/home/tomcat/apache-tomcat-${TOMCAT_VER}/conf/Catalina/localhost/manager.xml <<EOF <Context privileged="true" antiResourceLocking="false" docBase="\${catalina.home}/webapps/manager"> <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="^.*\$" /> </Context> EOF chown -R tomcat:tomcat /home/tomcat # Create the Tomcat Service cat >/etc/systemd/system/tomcat.service <<CMD_EOF [Unit] Description=Tomcat [Service] Type=forking User=tomcat PIDFile=/home/tomcat/apache-tomcat-${TOMCAT_VER}/temp/tomcat.pid ExecStart=/home/tomcat/apache-tomcat-${TOMCAT_VER}/bin/startup.sh ExecStop=/home/tomcat/apache-tomcat-${TOMCAT_VER}/bin/shutdown.sh [Install] WantedBy=default.target CMD_EOF chmod +x /etc/systemd/system/tomcat.service systemctl daemon-reload systemctl enable tomcat.service systemctl restart tomcat.service } # Install OpenJDK 8 function install_openjdk(){ yum install -y java-1.8.0-openjdk-headless } # Create the auth.txt file to write passwords to touch /root/auth.txt # Preqs for clean install yum -y install wget unzip tar # Disable Selinux if enabled setenforce 0 #Specific CentOS fixes sed -i.save 's/enabled=0/enabled=1/' /etc/yum.repos.d/CentOS-PowerTools.repo # Get latest Tomcat 9 version number TOMCAT_VER=$(wget -qO- --no-check-certificate https://tomcat.apache.org/download-90.cgi | grep '<a href="#9.' | cut -f2 -d'>' | cut -f1 -d'<' | head -n 1) if [ -z "${TOMCAT_VER}" ]; then echo "Error: Failed to get tomcat version"; exit 1; fi # Execute install_openjdk and install_tomcat install_openjdk; install_tomcat; # Save passwords to auth.txt and also display to user echo "Passwords saved in /root/auth.txt" cat /root/auth.txt |
As root, save the above script as tomcat9-centos8.sh and make it executable.
1 |
[root@demo ~]# chmod +x tomcat9-centos8.sh |
Run the script:
1 |
[root@demo ~]# ./tomcat9-centos8.sh |
Upon completion, the GUI and Manager passwords will be displayed as below:
1 2 3 |
Passwords saved in /root/auth.txt tomcat manager pass: HTMpZINCcm6dJ1-MsvMVEljubtO2uCLv tomcat admin pass: E3m5yW6Bhpmx6ryHx5ddlaPyGfI0eNQ8 |
The random passwords will also be saved as auth.txt in the root directory.
1 2 3 4 5 6 7 |
[root@demo ~]# ls -l total 24 -rw-------. 1 root root 5589 Jan 13 21:54 anaconda-ks.cfg -rw-r--r--. 1 root root 85 Mar 16 22:18 auth.txt -rw-------. 1 root root 5355 Jan 13 21:54 original-ks.cfg -rwxr-xr-x. 1 root root 3583 Mar 16 22:17 tomcat9-centos8.sh [root@demo ~]# |
You should now be able to access Tomcat at:
http://domain.com:8080 or http://IP:8080