1. Introduction
This post shows step by step how to install Tomcat into Ubuntu Server 16.04.
2. Step-by-Step
2.1. Update system packages
$ sudo apt-get update
2.2 Create Tomcat User and Group
a) Create 'tomcat' Group
$ sudo groupadd tomcat
b) Create 'tomcat' Group
$ sudo useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat
2.3 Download Tomcat installer from mirror site
a) Open Tomcat mirror site in your browser and find out current available download version
+---------------------------------------------------------------------------------+
| http://apache.mirrors.ionfish.org/tomcat/tomcat-8/ |
+---------------------------------------------------------------------------------+
| Index of /tomcat/tomcat-8 |
| |
| Name Last modified Size Description |
| ../ - |
| v8.0.47/ 03-Oct-2017 08:50 - |
| v8.5.23/ 01-Oct-2017 18:31 - |
| |
| Apache Server at apache.mirrors.ionfish.org Port 80 |
+---------------------------------------------------------------------------------+
2.4 Download Tomcat install tar.gz for linux
$ cd /tmp
$ curl -O http://apache.mirrors.ionfish.org/tomcat/tomcat-8/v8.5.23/bin/apache-tomcat-8.5.23.tar.gz
2.5. Install Tomcat 8 into /opt/tomcat
a) Unzipping into /opt/tomcat
$ sudo mkdir /opt/tomcat
$ sudo tar xzvf apache-tomcat-8*tar.gz -C /opt/tomcat --strip-components=1
b) Adjusting permisson for 'tomcat' user
$ cd /opt/tomcat
$ sudo chgrp -R tomcat /opt/tomcat
$ sudo chmod -R g+r conf
$ sudo chmod g+x conf
$ sudo chown -R tomcat webapps/ work/ temp/ logs/
2.6. Create a systemd Service file for Tomcat
a) Checking JAVA_HOME
$ sudo update-java-alternatives -l
java-1.8.0-openjdk-amd64 1081 /usr/lib/jvm/java-1.8.0-openjdk-amd64
b) Create systemd service file on /etc/systemd/system/tomcat
$ sudo 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-1.8.0-openjdk-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
c) Reload systemd service to apply change
$ sudo systemctl daemon-reload
$ sudo systemctl start tomcat
d) Configure tomcat service to automatically starts on boot
$ sudo systemctl enable tomcat
e) Manually starting, stopping and status
$ sudo /opt/tomcat/bin/catalina.sh start
$ sudo /opt/tomcat/bin/catalina.sh stop
$ sudo /opt/tomcat/bin/catalina.sh status
2.7. Ajust firewall rule
a) Checking if
$ service ufw status
ufw.service - Uncomplicated firewall
Loaded: loaded (/lib/systemd/system/ufw.service; enabled; vendor preset: enab
Active: active (exited) since Sun 2017-11-05 19:17:17 BRST; 12min ago
Process: 376 ExecStart=/lib/ufw/ufw-init start quiet (code=exited, status=0/SU
:
b) Add firewall rule to accept incomming call on Tomcat port 8080
$ sudo ufw allow 8080
2.8. Configure Tomcat Web Management Interface
a) Edit tomcat-users.xml, be carefull with remarks "<!--" "--->"
$ sudo vim /opt/tomcat/conf/tomcat-users.xml
:
<tomcat-users xmlns="http://tomcat.apache.org/xml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd"
version="1.0">
:
<user username="admin" password="admin" roles="manager-gui,admin-gui,tomcat"/>
:
</tomcat-users>
:
b) Edit context.xml,
$ sudo vim /opt/tomcat/webapps/host-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" />
<Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/>
</Context>
2.9. Open Tomcat in browser
a) Open default Tomcat page
b) Open Tomcat Documentation page
3. References