Assumption: JDK is already installed.
Copy/upload the appropriate Tomcat tar file
While there a few different ways to install Tomcat, we will start with the tar file available at Apache downloads page. Let’s copy/upload to /usr/local
Next, extract the tar file, like so:tar xzvf apache-tomcat-9.0.54.tar.gz
Shorten the tomcat folder name to tomcat9 for ease of use.
Create Tomcat Group & User:sudo groupadd tomcat
sudo useradd -r -g tomcat -s /bin/false tomcat
[Because the user is required only for ownership purposes, not login purposes, the useradd command uses the -r and -s /bin/false options to create a user that does not have login permissions to your server host.]
Provide ownership to user ‘tomcat’
Assuming the tomcat folder is /usr/local/tomcat9, run: sudo chown -R tomcat:tomcat /usr/local/tomcat9
Provide execute permissions to bin dirsudo chmod -R u+x /usr/local/tomcat9/bin
Create a service for Tomcat
Create a file ‘tomcat.service’ in /etc/systemd/system directory and copy the below, adjust as needed:
[Unit]
Description=Tomcat Server
After=network.target
[Service]
Type=forking
Environment=JAVA_HOME=/usr/local/jdk-14
Environment=CATALINA_HOME=/usr/local/tomcat9
Environment=CATALINA_BASE=/usr/local/tomcat9
Environment=CATALINA_PID=/usr/local/tomcat9/temp/tomcat.pid
ExecStart=/usr/local/tomcat9/bin/startup.sh
ExecStop=/usr/local/tomcat9/bin/shutdown.sh
RestartSec=12
Restart=always
User=tomcat
Group=tomcat
[Install]
WantedBy=multi-user.target
Run chmod a+x tomcat.service to make the file executable.
Reload systemd so it is aware of the new service.sudo systemctl daemon-reload
Make sure all needed databases, users are created before you start Tomcat.
Make sure you give permissions to port 80 through authbind,
Check this page for instructions: https://www.techwhacko.com/run-tomcat-9-0-on-port-80-or-443-with-authbind-ubuntu-18-0
Make sure you do not have nginx/httpd running: systemctl status nginx
systemctl status apache2
systemctl status httpd
In my case, I had Plesk and Nginx running… I thought Nginx might be used by Plesk but it does not appear to be, i.e, shutting down nginx did not harm Plesk. Apparently, some features or Plesk may need Apache or Nginx but not everything.
Start tomcat service like so:sudo systemctl start tomcat
To test if it is running on 80
$ netstat -antup | grep 80
OR
$lsof -i :80
To test if the port can be reached from outside:
nmap -p 80 -T4 -Pn <IP address here>
In my case, I found that I enabled forwarding all requests from http to https, in my tomcat/conf/web.xml and I had to find the block of xml that has < security-context > which needed to be commented out.
Disable services stopped, from starting up at boot time
All the services stopped above, to get the server and applications running correctly must now be disabled at boot time.
Here is how check for each one – httpd, apache2, nginx
sudo systemctl is-enabled httpd
Here is how you disable it
sudo systemctl disable httpd
You can also strong disable it but may not be such a good idea.sudo systemctl mask httpd