1 Installation Java adoptopenjdk 8 hotspot & Tomcat 9
Kaimbacher edited this page 2021-07-21 09:43:26 +00:00

Step 1: Install Java

wget -qO - https://adoptopenjdk.jfrog.io/adoptopenjdk/api/gpg/key/public | sudo apt-key add -
sudo add-apt-repository --yes https://adoptopenjdk.jfrog.io/adoptopenjdk/deb/
sudo apt update
sudo apt install adoptopenjdk-8-hotspot
sudo update-alternatives --config java
java -version

sudo update-java-alternatives -l

Output: adoptopenjdk-8-hotspot-amd64 1081 /usr/lib/jvm/adoptopenjdk-8-hotspot-amd64

Step 2 Install Apache Tomcat 9

see also: https://www.cloudbooklet.com/install-apache-tomcat-on-ubuntu-20-04-google-cloud/

sudo mkdir /opt/tomcat

Download Tomcat with the link you have copied:

cd /tmp
wget http://mirrors.estointernet.in/apache/tomcat/tomcat-9/v9.0.50/bin/apache-tomcat-9.0.50.tar.gz
sudo tar xzvf apache-tomcat-9.0.50.tar.gz -C /opt/tomcat --strip-components=1

Create and setup tomcat user

sudo groupadd tomcat 
sudo useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat

see parameter http://manpages.ubuntu.com/manpages/bionic/de/man8/useradd.8.html

sudo usermod -a -G tomcat administrator

Setup tomcat permissions

cd /opt/tomcat

Setup correct permissions for tomcat user:

sudo chgrp -R tomcat /opt/tomcat
sudo chmod -R g+r /opt/tomcat/conf
sudo chmod g+x /opt/tomcat/conf
sudo chown -R tomcat webapps/ work/ temp/ logs/

Create tomcat service

Now, create a new file for Tomcat inside /etc/systemd/system directory:
sudo nano /etc/systemd/system/tomcat.service

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

[Service]
Type=forking

Environment=JAVA_HOME=/usr/lib/jvm/adoptopenjdk-8-hotspot-amd64
Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINA_BASE=/opt/tomcat
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -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:
sudo systemctl daemon-reload

Now you can start Tomcat server:
sudo systemctl enable tomcat

configure TOMCAT

sudo nano /opt/tomcat/conf/tomcat-users.xml

Add the <user tag within the <tomcat-users tag, which should look like the one below:

<tomcat-users . . .>
    <user username="admin" password="xxx" roles="manager-gui,admin-gui"/>
</tomcat-users> 

By default Tomcat restricts access to Manager and Host manager. So, to allow connections you need to remove the IP restrictions by commenting out the listed lines from the corresponding context.xml files. \

For the Manager app the file that needs be updated is:
sudo nano /opt/tomcat/webapps/manager/META-INF/context.xml

<Context antiResourceLocking="false" privileged="true" >
  <!--<Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />-->
</Context>

For the Host Manager app the file that needs be updated is:
sudo nano /opt/tomcat/webapps/host-manager/META-INF/context.xml

test tomcat installation

sudo systemctl reload tomcat.service

check tomcat installation
http://localhost:8080/
http://localhost:8080/manager/html
http://localhost:8080/host-manager/html