How to install manually Apache Tomcat 9 on CentOS 7 minimal Linux

We will install currently the latest version of Apache Tomcat on CentOS 7 minimal Linux machine…and currently (on day 16/08/2018) that is version 9.0.10. Because we will do this on minimal install, we don’t have Java either, so we will install the latest Java – also manually and for the day of writing, that is version Tomcat cannot be started without the JAVA_HOME variable.

# Download Java jdk from official webite (link here)
# In this case we will use Java version 8u181
tar zxvf jdk-8u181-linux-x64.tar.gz
rm -f jdk-8u181-linux-x64.tar.gz
mv jdk1.8.0_181/ /opt/
alternatives --install /usr/bin/java java /opt/jdk1.8.0_181/bin/java 2

# Chose your 'newly' installed version of java (in this case 1) and press enter, 
# and later check is that version of java - present. Screenshot here.
alternatives --config java

# Many application need JAVA_HOME and JRE_HOME as a variables, so we will put them on profile file
# to became global variable
cp /etc/profile ~/etc-profile-backup-$(date +%Y%m%d%H%M)
echo "export JAVA_HOME=/opt/jdk1.8.0_181" >> /etc/profile
echo "export JRE_HOME=/opt/jdk1.8.0_181/jre" >> /etc/profile

# Download Tomcat. On script writing newest Tomcat version was 9.0.10 and we will install it here.
curl -O
curl -O

# Check SHA512 of downloaded file. If you get any record from next command you have
# done something wrong witg download and try again.
diff  <(echo "$(sha512sum apache-tomcat-9.0.10.tar.gz | awk '{print $1}')" ) <(echo "$(cat apache-tomcat-9.0.10.tar.gz.sha512 | awk '{print $1}')") 

# Extract and move Tomcat to /opt
tar zxvf apache-tomcat-9.0.10.tar.gz 
mv /opt/tomcat /opt/tomcat-$(date +%Y%m%d%H%M) 2>/dev/null # Just in case
mv apache-tomcat-9.0.10 /opt/tomcat

# Add CATALINA_HOME environment variable if not exists
[ -z ${CATALINA_HOME} ] && echo "export CATALINA_HOME='/opt/tomcat/'" >> /etc/profile
. /etc/profile || echo "CATALINA_HOME already exists. Please check"

# Add user for Tomcat and grant ownerwhip on folder
useradd -r tomcat --shell /bin/false
chown -R tomcat:tomcat /opt/tomcat/

# Create a a systemd file. 
# We will give to Tomcat in this scenario 1GB of RAM (see CATALINA_OPTS value)
[ ! -f /etc/systemd/system/tomcat.service ] &&
echo "[Unit]
Description=Apache Tomcat 9

Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'

[Install]" >> /etc/systemd/system/tomcat.service || echo "File is missing"

# Reload daemon because of newly added service
systemctl daemon-reload

# Open Firewalld port for tomcat
firewall-cmd --add-port=8080/tcp
firewall-cmd --add-port=8080/tcp --permanent

# Start, enable and check tomcat as a service
systemctl start tomcat
systemctl enable tomcat
systemctl status tomcat

Now you can go to web address http://SERVER-IP-ADDRESS:8080 and test do you see default Tomcat page – if you see it – a job is done successfully.

# If you want to have Tomcat on http port (80) easyest way if to make
# port forward on firewalld level with next commands
firewall-cmd --add-forward-port=port=80:proto=tcp:toport=8080 --permanent
firewall-cmd --reload
# If you want you can test install of simple web application with sample from
# Apache Tomcat website. When you run this commands go with your browser to
# http://tomcaturl/sample to test sample app (see screenshot bellow)
cd /opt/tomcat/webapps/
curl -O