Apache Tomcat is an open-source implementation of the Java Servlet, JavaServer Pages, Java Expression Language and WebSocket technologies.
Tomcat provides a "pure Java" HTTP web server environment in which Java code can run.
First you need to install Java --> Install Java with apt on Ubuntu Linux
Create Tomcat directory
sudo mkdir /opt/tomcat
Navigate to opt directory
Extract the archive in Tomcat directory
sudo tar xzvf apache-tomcat-9*tar.gz -C /opt/tomcat --strip-components=1
Change all files in bin folder permissions
sudo chmod -R 755 /opt/tomcat/bin
Create a systemd Service File
java -XshowSettings:properties -version 2>&1 > /dev/null | grep 'java.home'
java.home = /usr/lib/jvm/java-8-openjdk-amd64/jre
Create the systemd service file for Tomcat
sudo nano /etc/systemd/system/tomcat.service
Paste the following contents into your service file. Modify the value of JAVA_HOME if necessary to match the value you found on your system. You may also want to modify the memory allocation settings that are specified in CATALINA_OPTS:
[Unit] Description=Apache Tomcat Web Application Container After=network.target [Service] Type=forking Environment=JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/jre 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=root Group=root UMask=0007 RestartSec=10 Restart=always [Install] WantedBy=multi-user.target
When you are finished, save and close the file.
Reload the systemd daemon so that it knows about our service file
sudo systemctl daemon-reload
Start the Tomcat service
sudo systemctl start tomcat
Double check that it started without errors
sudo systemctl status tomcat | cat
Adjust the Firewall and Test the Tomcat Server
Tomcat uses port 8080 to accept conventional requests. Allow traffic to that port
sudo ufw allow 8080
If you were able to successfully accessed Tomcat, now is a good time to enable the service file so that Tomcat automatically starts at boot
sudo systemctl enable tomcat
Configure Tomcat Web Management Interface
In order to use the manager web app that comes with Tomcat, we must add a login to our Tomcat server.
We will do this by editing the tomcat-users.xml file:
sudo nano /opt/tomcat/conf/tomcat-users.xml
You will want to add a user who can access the manager-gui and admin-gui (web apps that come with Tomcat). You can do so by defining a user, similar to the example below, between the tomcat-users tags.
Be sure to change the username and password to something secure:
<user username="barrouh" password="password@@" roles="manager-gui,admin-gui"/>
Save and close the file when you are finished.
By default, newer versions of Tomcat restrict access to the Manager and Host Manager apps to connections coming from the server itself. Since we are installing on a remote machine, you will probably want to remove or alter this restriction. To change the IP address restrictions on these, open the appropriate context.xml files.
For the Manager app, type:
sudo nano /opt/tomcat/webapps/manager/META-INF/context.xml
For the Host Manager app, type:
sudo nano /opt/tomcat/webapps/host-manager/META-INF/context.xml
Inside, comment out the IP address restriction to allow connections from anywhere. Alternatively, if you would like to allow access only to connections coming from your own IP address, you can add your public IP address to the list:
context.xml files for Tomcat webapps
<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>
Save and close the files when you are finished.
To put our changes into effect, restart the Tomcat service:
sudo systemctl restart tomcat