Manually Install Tomcat7 on Ubuntu 16.04

Problem

Tomcat7 in the Ubuntu 16.04 repository was compiled using Java8 and this caused a problem with OpenMRS that needed Java7.

This issue has been mentioned also on stackoverflow and also apache bugzilla.

Solution

The solution was to download Apache manually. I used Apache v7.0.70. You can download and extract the folder in the tmp folder:

root@whiscardz # cd /tmp/
root@whiscardz /tmp # curl -O http://archive.apache.org/dist/tomcat/tomcat-7/v7.0.70/bin/apache-tomcat-7.0.70.tar.gz 
 % Total % Received % Xferd Average Speed Time Time Time Current
 Dload Upload Total Spent Left Speed
100 8715k 100 8715k 0 0 2155k 0 0:00:04 0:00:04 --:--:-- 2155k

root@whiscardz /tmp # ll apache-tomcat-7.0.70.tar.gz 
-rw-r--r-- 1 root root 8924465 Jan 13 13:00 apache-tomcat-7.0.70.tar.gz
root@whiscardz /tmp # tar xzvf apache-tomcat-7*tar.gz -C /opt/tomcat --strip-components=1

root@whiscardz /tmp # ll /opt/tomcat/
total 124
drwxr-xr-x 9 root root 4096 Jan 13 13:01 ./
drwxr-xr-x 11 root root 4096 Jan 13 12:55 ../
drwxr-xr-x 2 root root 4096 Jan 13 13:01 bin/
drwxr-xr-x 2 root root 4096 Jun 15 2016 conf/
drwxr-xr-x 2 root root 4096 Jan 13 13:01 lib/
-rw-r--r-- 1 root root 56846 Jun 15 2016 LICENSE
drwxr-xr-x 2 root root 4096 Jun 15 2016 logs/
-rw-r--r-- 1 root root 1239 Jun 15 2016 NOTICE
-rw-r--r-- 1 root root 8965 Jun 15 2016 RELEASE-NOTES
-rw-r--r-- 1 root root 16195 Jun 15 2016 RUNNING.txt
drwxr-xr-x 2 root root 4096 Jan 13 13:01 temp/
drwxr-xr-x 7 root root 4096 Jun 15 2016 webapps/
drwxr-xr-x 2 root root 4096 Jun 15 2016 work/
root@whiscardz /tmp # 

Create a tomcat user and group:

root@whiscardz # groupadd tomcat
root@whiscardz # useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat

cd to the directory and then change the owner and permissions and group of the files and directories:

root@whiscardz # cd /opt/tomcat/

root@whiscardz /opt/tomcat # chgrp -R tomcat conf
root@whiscardz /opt/tomcat # chmod g+rwx conf
root@whiscardz /opt/tomcat # chmod g+r conf/*
root@whiscardz /opt/tomcat # chown -R tomcat work/ temp/ logs/ 

Then check the path of Java. This will be used in the JAVA_HOME env variable only appending /jre to the end of the result.

See JAVA_HOME Environment variable in the systemd service file below:

root@whiscardz ~ # update-java-alternatives -l
java-7-oracle 1 /usr/lib/jvm/java-7-oracle 

Create a systemd service file below. Make sure to edit the JAVA_HOME;CATALINA_PID;  CATALINA_HOME; CATALINA_BASE; CATALINA_OPTS; ExecStart and ExecStop Variables to suite your environment

root@whiscardz ~ # vim /etc/systemd/system/tomcat.service

[Unit]
Description=Apache Tomcat Web Application Container
After=network.target

[Service]
Type=forking

Environment=JAVA_HOME=/usr/lib/jvm/java-7-oracle/jre
Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINA_BASE=/opt/tomcat
Environment='CATALINA_OPTS=-Xmx2048M -Xms2048M -XX:PermSize=512m -XX:MaxPermSize=512m -XX:NewSize=256m -server -XX:+UseParallelGC'
Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom'

ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh

User=tomcat
Group=tomcat
UMask=0007
RestartSec=10
Restart=always

[Install]
WantedBy=multi-user.target

Reload the systemd daemon so that it picks up your tomcat.service file above. Then you can start tomcat. You can also stop tomcat and check if it stops.

Then you can enable the service so that it is started when the machine is rebooted :

root@whiscardz ~ # systemctl daemon-reload
root@whiscardz ~ # systemctl start tomcat

root@whiscardz ~ # systemctl stop tomcat
root@whiscardz ~ # systemctl enable tomcat 

Incase you should down tomcat using the shutdown.sh script alone and it doesnt shutdown, check out this post.

Resources

How To Install Apache Tomcat 8 on Ubuntu 16.04

Install Apache Tomcat 9 on CentOS 7 / RHEL 7 / Ubuntu 16.04

How to Install Tomcat 7.0.68 Server on Ubuntu, Debian & LinuxMint

Tomcat shutdown does not kill process

Tomcat doesn’t stop. How can I debug this?

Tomcat 7 doesn’t shut down, process keeps running?

Apache Tomcat 8 Start stop script init.d script

Starting, Stopping, and Restarting Tomcat