Apache Tomcat is a free and open source Java web and application server. Using Apache Tomcat, you can run and deploy Java web applications, servlets, JSPs, and WebSockets. It implement specifications developed as part of Jakarta EE and released under the Apache License version 2.
Install and Configure Apache Tomcat:
Installing and configuring Apache Tomcat involves the following steps:
Install Java.
Create a Tomcat User.
Install Tomcat.
Create a systemd File for Tomcat.
Configure Tomcat Web User Interface.
Access Tomcat.
1. Install Java on Ubuntu
To install and run latest version of Apache Tomcat on your system, you need latest Java JDK release.
First confirm whether Java is already installed in your system by running this command in terminal application (ctrl+alt+t)
java -version
If you don’t have java then install it via below commands.
sudo apt install default-jdk -y
2. Create Tomcat User on Ubuntu
Now you should create a unique user. First create a new tomcat group that will run the service using below command on terminal application.
sudo groupadd tomcat
After that, create a new tomcat user. Create user members of the Tomcat group with a home directory opt/tomcat for running the Tomcat service:
sudo useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat
3. Install Tomcat on Ubuntu:
Now, change to the /tmp directory by running below command.
cd /tmp
Then download the latest Tomcat core binary release in tar.gz file format from the Tomcat downloads page.
In the terminal run below command.
curl -O https://dlcdn.apache.org/tomcat/tomcat-10/v10.1.20/bin/apache-tomcat-10.1.20.tar.gz
In above command “https://dlcdn.apache.org/tomcat/tomcat-10/v10.1.20/bin/apache-tomcat-10.1.20.tar.gz” is the Tomcat 10.1.20 download link. At the time of the writing the latest version is 10.1.20. You can change above command based on your version.
Now run below commands one by one.
sudo mkdir /opt/tomcat
cd /opt/tomcat
sudo tar xzvf /tmp/apache-tomcat-10.1.20.tar.gz -C /opt/tomcat --strip-components=1
Here”apache-tomcat-10.1.20.tar.gz” is the downloaded file name.
Now, give the Tomcat group ownership over the entire installation directory with the chgrp command:
sudo chgrp -R tomcat /opt/tomcat
Next, you need to give the Tomcat user access to the conf directory to view its contents.
sudo chmod -R g+r conf
sudo chmod g+x conf
Make the Tomcat user the owner of the web apps, work, temp, and logs directories:
sudo chown -R tomcat webapps/ work/ temp/ logs/
4. Create a systemd File for Tomcat:
To run Tomcat as a service, open your nano text editor and create a file name tomcat.service in the /etc/systemd/system/ path using below command.
sudo nano /etc/systemd/system/tomcat.service
Now copy the below configuration details and paste it in your nano text editor.
[Unit] Description=Apache Tomcat Web Application Container After=network.target [Service] Type=forking Environment=JAVA_HOME=/usr/lib/jvm/java-17-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.awt.headless=true -Djava.security.egd=file:/dev/v/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
Now press Ctrl + X on your keyboard. This will attempt to exit nano. Now enter Y and hit the enter key. Again hit the Enter key on your keyboard. This will Save and close the file.
Note: In the above configuration text, for me the java.home is /usr/lib/jvm/java-17-openjdk-amd64. It will change based on your Java Version. If you don’t know, just Run below command to find your java.home
java -XshowSettings:properties -version 2>&1 > /dev/null | grep 'java.home'
Now run below commands one by one. It will start and enable Tomcat.
sudo systemctl daemon-reload
sudo systemctl start tomcat
sudo systemctl enable tomcat
After that you can check Tomcat status using below command.
sudo systemctl status tomcat
Now allow in firewall
sudo ufw allow 8080
5. Configure Tomcat Web User Interface:
Now open the tomcat-users.xml file using below command.
sudo nano /opt/tomcat/conf/tomcat-users.xml
Now, before the </tomcat-users> line add below command. Here you can change username and password based on your taste. .
<role rolename="manager-gui"/> <user username="manikandan" password="Pass@1234" roles="manager-gui,admin-gui"/>
By default, Tomcat Web UI is configured to be accessible only from localhost.
To enable access for the Manager app, edit the following file:
sudo nano /opt/tomcat/webapps/manager/META-INF/context.xml
Comment out the following line:
<!-- <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->
Just add below to comment out.
<!-- -->
Now press Ctrl + X on your keyboard. Now enter Y and hit the enter key.Again hit the Enter key. This will Save and close the file.
To enable access for the Host Manager app, edit the following file:
sudo nano /opt/tomcat/webapps/host-manager/META-INF/context.xml
Comment out the following line:
<!-- <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->
<!-- -->
Now press Ctrl + X on your keyboard. Now enter Y and hit the enter key.Again hit the Enter key. This will Save and close the file.
Now restart the Tomcat service to apply the changes.
sudo systemctl restart tomcat
6. Accessing Tomcat Web UI:
Now open your web browser and enter below url. You will be redirected to Tomcat default page.
If you are accessing from local host, then go to like below
http://localhost:8080
Tomcat Manager app
http://localhost:8080/manager/html
Tomcat host manager app
http://localhost:8080/host-manager/html
Make sure that you entered the account credentials to the tomcat-users.xml file.
If you are accessing remotely then
http://your-server-ip:8080
[OR]
http://your-domain-name_ : 8080
You can also access from your system internal private ip like below
You can get your internal ip using below commands.
hostname -I
ip a
Via GUI
Setting ->Network
If you’re connected to Wi-Fi, choose Wi-Fi, other select Wired. Click on the settings gear next to the network that you’re connected to.