How to install tomcat on ubuntu and deploy war file in tomcat
Step 1: Install Java
To install tomcat on your server first you have to install java on your system. To check java is installed or not type
java –version
It will return the current java version if java is installed on your system.
If not then install java first
sudo apt-get update
sudo apt-get install default-jdk
It will install java 11 .
Step2 : Create Tomcat User and Group
Create a new group and system user to run the Apache Tomcat service from the /opt/tomcat directory.
sudo groupadd tomcat
sudo useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat
Step 3: Download Tomcat 9
cd /opt
wget https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.56/bin/apache-tomcat-9.0.56.tar.gz
sudo tar xzvf apache-tomcat-9*tar.gz
sudo mv -r apache-tomcat-9.0.56 tomcat
If you face problem with download you can download it manually from tomcat official site
Go to https://tomcat.apache.org/ and select tomcat version you want to download. Then download the core tar.gz format file.
Step 4: Update Permissions
sudo chgrp -R tomcat /opt/tomcat
chmod 755 /opt/tomcat
sudo chown -R tomcat webapps/ work temp/ logs
Or
sudo chown -R tomcat: /opt/tomcat
Step 5: Create a systemd Service File
we can create the systemd service file name tomcat.service in the /etc/systemd/system directory in typing:
sudo nano /etc/systemd/system/tomcat.service
After editing
sudo systemctl daemon-reload
sudo systemctl start tomcat
sudo systemctl status tomcat
Sometime linux firewall block the port . To allow
sudo ufw allow 8080
Step 6: Deploy war file in Tomcat
Now copy the war file to /opt/tomcat/webapps/ directory.
Then restart the tomcat server.
sudo systemctl restart tomcat
Then go to browser and type the <server-ip>:l8080/<war-name>
Example:
My war file name is war-demo.war and server ip is 10.0.3.116
Note:
- All the web applications are hosted in tomcat webapps directory
- Start, stop, restart the tomcat using system command (systemctl start/stop/restart tomcat) .
- Must have to create user for tomcat and give the permission carefully. Otherwise it will cause error .
- If any error occur check the log file tomcat/logs directory.
- This error typically happen when you don’t give the permission in right way and mismatch the resource name.
References
https://linuxize.com/post/how-to-install-tomcat-9-on-ubuntu-20-04/
https://www.networkinglearning.com/2021/12/how-to-install-apache-tomcat-9-on.html