Tomcat Installation

From DISI
Jump to navigation Jump to search

Written by Jennifer Young on January 17, 2020

This guide is for CentOS 7. Taken from https://phoenixnap.com/kb/install-tomcat-9-on-centos-7 with some modifications.

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

Set the tomcat service to start on boot

   systemctl enable tomcat

Adjust the firewall

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

Setup Web Management Interface

Change the Admin Username and Password

   vim /opt/tomcat/latest/conf/tomcat-users.xml

Inside the <tomcat-users> </tomcat-users> block add the following:

   <role rolename="admin-gui"/>
   <role rolename="manager-gui"/>
   <user username="TomcatAdmin" password="<put-a-good-password-here>" roles="admin-gui,manager-gui"/> 

Password for Tomcat on n-1-136 and others is the usual password

Modify context.xml to access the Tomcat manager in the browser

   vim /opt/tomcat/latest/webapps/manager/META-INF/context.xml

In this file you will see the following line:

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

Important! Comment out that line like below:

Now do the same for the corresponding context.xml in the host-manager directory:

   vim /opt/tomcat/latest/webapps/host-manager/META-INF/context.xml

Comment out the valve statement again

Finally, start tomcat

   systemctl start tomcat

Now go to the IP address of your machine and port 8080 in the browser

For n-1-136 the link is below. This link will NOT work unless you are at UCSF or are using an ssh tunnel with Switchy Omega

   http://10.20.10.136:8080/

The Tomcat manager should appear in the browser

Go to the Manager App section

There is a button in the upper right corner that says Manager app. Go there and scroll down to the Deploy section. Under Deploy, it says "select which .war file to load" Add the arthor-server.war file or any other .war file you wish to deploy

Important: Create setenv.sh for environment variables

If your program requires environment variables to run the best place to put them is in

   /opt/tomcat/apache-tomcat-9.0.30/bin/setenv.sh

You will need to create that file yourself.

   vim setenv.sh

In that file add your environment variables. For example, Arthor requires the arthor.cfg file. Example below shown for n-1-136

   export ARTHOR_CONFIG=/usr/local/tomcat/arthor.cfg

Make the setenv.sh executable

   chmod +x setenv.sh

Change ownership to tomcat

   chown tomcat:tomcat setenv.sh