Tomcat Installation
Written by Jennifer Young on January 17, 2020 (still in progress)
This guide is for CentOS 7
Step 0: Make sure Tomcat is not already installed
Run as root
systemctl status tomcat
If you get an error saying there was no tomcat.service file found, then Tomcat has not yet been installed on this machine.
Step 1: Make sure Java is running on your machine
Check if java is already installed on your machine with
java -version
You should get something that looks similar to this: openjdk version "1.8.0_232" OpenJDK Runtime Environment (build 1.8.0_232-b09) OpenJDK 64-Bit Server VM (build 25.232-b09, mixed mode)
If java is not installed run the following (as root):
yum install java-1.8.0-openjdk-devel
Step 2: Download the latest version of Tomcat from the website
https://tomcat.apache.org/download-90.cgi
Copy the .tar.gz file into /tmp on your machine
Create tomcat user and directory
Tomcat should not be run as root. Create a tomcat user with fewer privileges. This will also create the /opt/tomcat directory on the machine where all tomcat files will be stored
sudo useradd -m -U -d /opt/tomcat -s /bin/false tomcat
Extract the contents of the tar.gz file
As of the writing of this page, the latest version of Tomcat 9 is 9.0.30
tar -xvzf apache-tomcat-9.0.30.tar.gz
Move the extracted files to /opt/tomcat
sudo mv apache-tomcat-9.0.30 /opt/tomcat/
Optional: Create symbolic link for updates
ln –s /opt/tomcat/apache-tomcat-9.0.30 /opt/tomcat/latest
Modify Tomcat User Permissions
chown –R tomcat:tomcat /opt/tomcat sh -c 'chmod +x /opt/tomcat/latest/bin/*.sh'
Create a System Unit File
Create the tomcat.service file
vim /etc/systemd/system/tomcat.service
Paste the below into the file
[Unit] Description=Tomcat 9 servlet container After=network.target [Service] Type=forking User=tomcat Group=tomcat Environment="JAVA_HOME=/usr/lib/jvm/jre" Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom" Environment="CATALINA_BASE=/opt/tomcat/latest" Environment="CATALINA_HOME=/opt/tomcat/latest" Environment="CATALINA_PID=/opt/tomcat/latest/temp/tomcat.pid" Environment="CATALINA_OPTS=-Xms32G -Xmx32G -server -XX:+UseParallelGC" ExecStart=/opt/tomcat/latest/bin/startup.sh ExecStop=/opt/tomcat/latest/bin/shutdown.sh [Install] WantedBy=multi-user.target
Change the Xms and Xmx according to how much memory you want to provide to Tomcat. Save and close the file.
Refresh system
systemctl daemon-reload