Apache TomCat Installation Guide

Create web users

groupadd webgroup
useradd -g webgroup -s /bin/bash webuser

Installation

Download the last version of TomCat 8 here (tar.gz version): http://tomcat.apache.org
cd /opt
wget http://ftp.tudelft.nl/apache/tomcat/tomcat-8/v8.0.32/bin/apache-tomcat-8.0.32.tar.gz
tar zxvf apache-tomcat-8.0.32.tar.gz -C /opt/
ln -s /opt/apache-tomcat-8.0.32 /opt/tomcat

Set directory access

rm -rf /opt/tomcat/logs/*
rm -rf /opt/tomcat/temp/*
rm -rf /opt/tomcat/work/*
rm -rf /opt/tomcat/webapps/*

mkdir -p /opt/tomcat/conf/Catalina/localhost
chown webuser:webgroup -R /opt/tomcat/conf/Catalina
chown webuser:webgroup -R /opt/tomcat/webapps
chown webuser:webgroup -R /opt/tomcat/logs
chown webuser:webgroup -R /opt/tomcat/temp
chown webuser:webgroup -R /opt/tomcat/work

chmod 0644 /opt/tomcat/conf/*
chmod 0775 /opt/tomcat/conf/Catalina
chmod 0775 /opt/tomcat/webapps
chmod 0775 /opt/tomcat/logs
chmod 0775 /opt/tomcat/temp
chmod 0775 /opt/tomcat/work

Add extra libraries to TomCat

Add the needed libraries to tomcat:
cd /opt/tomcat/lib
mkdir external
cd external

wget http://repo1.maven.org/maven2/org/slf4j/jul-to-slf4j/1.7.18/jul-to-slf4j-1.7.18.jar
wget http://repo1.maven.org/maven2/org/slf4j/log4j-over-slf4j/1.7.18/log4j-over-slf4j-1.7.18.jar
wget http://repo1.maven.org/maven2/org/slf4j/jcl-over-slf4j/1.7.18/jcl-over-slf4j-1.7.18.jar
wget http://repo1.maven.org/maven2/ch/qos/logback/logback-classic/1.1.3/logback-classic-1.1.3.jar
wget http://repo1.maven.org/maven2/ch/qos/logback/logback-core/1.1.3/logback-core-1.1.3.jar
wget http://repo1.maven.org/maven2/org/slf4j/slf4j-api/1.7.18/slf4j-api-1.7.18.jar
wget http://repo1.maven.org/maven2/org/postgresql/postgresql/9.4.1208/postgresql-9.4.1208.jar

Load the jars in the "external" directory, edit the file "/opt/tomcat/conf/catalina.properties":
common.loader="${catalina.base}/lib","${catalina.base}/lib/*.jar","${catalina.home}/lib","${catalina.home}/lib/*.jar","${catalina.home}/lib/external/",
"${catalina.home}/lib/external/*.jar"


Tuning the Tomcat server

Create the file "/opt/tomcat/bin/setenv.sh" with the following content:
JAVA_OPTS="-Xmx2048m -Xms2048m"
JAVA_OPTS="${JAVA_OPTS} -XX:+UseParallelGC -XX:+UseParallelOldGC -XX:LargePageSizeInBytes=2m -XX:ParallelGCThreads=2"
JAVA_OPTS="${JAVA_OPTS} -server -Dlogback.configurationFile=${CATALINA_HOME}/conf/logback.xml -Dlogback.ContextSelector=JNDI"
export JAVA_OPTS

Set up logback

Create the file "/opt/tomcat/conf/logback.xml" with the following content:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
     <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
          <file>${catalina.base}/logs/${CONTEXT_NAME}.log</file>
          <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
               <fileNamePattern>logFile.%d{yyyy-MM-dd}.log</fileNamePattern>
               <maxHistory>3</maxHistory>
          </rollingPolicy>
          <encoder>
              <pattern>%date %-5level [%logger{0}:%line] %message%n</pattern>
          </encoder>
     </appender>

     <root level="WARN">
          <appender-ref ref="FILE" />
     </root>
</configuration>

Set up JNDI database connection and propery file location

Create the file '/opt/tomcat/conf/Catalina/localhost/context.xml.default' with the following content:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<!-- JNDI Resources -->
   <Resource
name="jdbc/appname"
auth="Container"
type="javax.sql.DataSource"

username="username"
password="password"
driverClassName="org.postgresql.Driver"
url="jdbc:postgresql://127.0.0.1/databasename"

validationQuery="SELECT 1"/>
<Environment
name="properties/appname"
type="java.lang.String"
value="file:${catalina.base}/conf/Catalina/localhost/appname.properties"/>
</Context>

Startup script

Create the file '/etc/systemd/system/tomcat.service' with the following content:
# Systemd unit file for tomcat
[Unit]
Description=Apache Tomcat Web Application Container
After=syslog.target network.target

[Service]
Type=forking

Environment=JAVA_HOME=/usr/lib/jvm/jre
Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINA_BASE=/opt/tomcat
Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom'

ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/bin/kill -15 $MAINPID

User=webuser
Group=webgroup

[Install]
WantedBy=multi-user.target

Save and exit. This script tells the server to run the Tomcat service as the tomcat user, with the settings specified.

Now reload Systemd to load the Tomcat unit file:
systemctl daemon-reload

Now you can start the Tomcat service with this systemctl command:
systemctl start tomcat

If you want to enable the Tomcat service, so it starts on server boot, run this command:
systemctl enable tomcat