How to Install Apache Tomcat Server on Ubuntu 22.04

How to Install Apache Tomcat Server on Ubuntu 22.04. In this article, we explain Tomcat and its features and after that we navigate to steps how to install the Apache Tomcat server on Ubuntu 22.04.

Tomcat was initially designed as a referenced framework for the Java Servlet API and the first Java Server Pages. Unfortunately, both technologies didn’t last long. However, they have laid the strong foundation for the improvised version of Tomcat.

The older version of Tomcat is widely used by Java servers due to its robustness, extensibility, dependable core engine, and extensive testing by skilled developers.

Shall we start with article How to Install Apache Tomcat Server on Ubuntu 22.04.

What is Apache Tomcat?

The Apache Software Foundation developed and maintains the open source web server for Java programs known as Apache Tomcat. Its purpose is to host and distribute Java servlets, which are server side Java code snippets responsible for managing HTTP responses from client Java applications. 

Further, Apache Tomcat functions as a web server, providing critical web server processing capabilities for the relevant servlets, rather than an entire application server with features such as load balancing and data persistence. Additionally, it supports the init(), service(), and destroy() methods as part of the Java servlet lifecycle.

Advantages of Apache Tomcat

Tomcat Server is the ideal option for the reasons listed below.

Open Source Application

Released under a general public license is an open source application. Hence, it enables anyone to utilize its core files for developing both personal and commercial applications, as it is downloaded and installed on your computer for free.

Flexible

It’s customization feature allows for building applications flexibly, offering users the ability to execute apps differently to meet their unique requirements. Users are free to make changes to the application according to their preferences, and it still works efficiently, producing top notch results.

Additional Security

Apache Tomcat provides users with advanced security features. Organizations frequently use firewalls to safeguard their data, and Tomcat can be installed to accomplish this goal.

Lightweight

In order to function effectively, enterprise level application development requires lightweight applications. Apache Tomcat’s lightweight nature enables users to rapidly obtain data processing capacity, making content development a straightforward process.

Stability

Tomcat has undergone several significant modifications that have greatly improved its stability. Even if one server fails, the remaining servers continue to function, ensuring that your application stays online at all times.

Well Documented

Furthermore, Apache Tomcat is very extensively documented for those interested in learning how to build Tomcat applications. Many online tutorials cover various topics such as setup instructions, startup options, platform support, and server configuration on the Tomcat platform.

Features of Apache Tomcat

Scalability

Well, Tomcat is simply scaled up or down to adapt to changes in application demand. It is therefore a versatile choice for businesses whose needs change.

Clustering

To ensure high availability and scalability, Tomcat supports clustering, which enables several servers to cooperate. This is especially helpful for large scale applications that need load balancing and high availability.

Embeddable

Tomcat offers flexibility in its use because it may be integrated into other programs or frameworks. This makes it possible for developers to make solutions that are unique to their needs.

Extensible

Tomcat may be highly customized with the help of add ons and plugins. Programmers update the server’s functionalities as needed as a result.

Management

Besides, Tomcat offers a web based administration panel that enables administrators to keep an eye on and control the server and its applications. Due to this managing and configuring Tomcat deployments is simple.

Servlet and JSP Support

Apache Tomcat provides full support for Java Servlet API and JavaServer Pages(JSP). Java is consequently a tool that programmers may use to build dynamic, data driven web applications.

Cross Platform

Tomcat is compatible with Windows, Linux, and macOS. Developers that operate across several platforms are drawn to it by its flexibility.

Resource pooling

The connection pooling functionality of Tomcat permits the sharing of many database connections among separate requests. As a result, database-dependent applications might operate more efficiently.

Sessional management

The connection pooling function of Tomcat permits the sharing of many database connections among separate requests. As a result, applications that rely on databases might work more efficiently.

Monitoring and logging

Developers follow the performance of their applications and identify problems as they occur due to Tomcat’s robust monitoring and logging capabilities.

How to Install Apache Tomcat Server on Ubuntu 22.04

In this section, we explain how to install the Apache Tomcat server on Ubuntu.

Prerequisites

  • An Ubuntu 22.04 server is installed on your system.
  • A root user or a user with sudo privileges.

Install Java JDK

Apache Tomcat is based on Java. So the Java JDK must be installed on your server. Install it using the following command.

				
					apt install openjdk-11-jdk
				
			

After installing Java JDK, please verify the Java installation using the following command.

				
					java --version
				
			

You should see the Java version in the following output.

				
					openjdk 11.0.18 2023-01-17
OpenJDK Runtime Environment (build 11.0.18+10-post-Ubuntu-0ubuntu122.04)
OpenJDK 64-Bit Server VM (build 11.0.18+10-post-Ubuntu-0ubuntu122.04, mixed mode, sharing)
				
			

Install Apache Tomcat

It is a good idea to create a dedicated user to run the Apache Tomcat server. Create a new user called tomcat using the following command.

				
					useradd -m -U -d /opt/tomcat -s /bin/false tomcat
				
			

Next, download the latest version of Apache Tomcat from its official website using the following command.

				
					wget https://archive.apache.org/dist/tomcat/tomcat-10/v10.0.20/bin/apache-tomcat-10.0.20.tar.gz
				
			

Once the Apache Tomcat is downloaded, extract the downloaded file with the following command.

				
					tar -xvf apache-tomcat-10.0.20.tar.gz
				
			

Next, move the content of the extracted directory to the Apache Tomcat home directory.

				
					mv apache-tomcat-10.0.20/* /opt/tomcat
				
			

Then, change ownership of the Tomcat directory to tomcat.

				
					chown -R tomcat: /opt/tomcat
				
			

Next, set the execution permission on Tomcat binary file.

				
					sh -c 'chmod +x /opt/tomcat/bin/*.sh'
				
			

Once you are done, please proceed to the next step.

Create a Systemd Service File for Apache Tomcat

It is also recommended to create a systemd service file to manage the Apache Tomcat service. Create it with the following command.

				
					nano /etc/systemd/system/tomcat.service
				
			

Add the following configurations.

				
					[Unit]
Description=Apache Tomcat
After=network.target

[Service]
Type=forking

User=tomcat
Group=tomcat

Environment=JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
Environment=CATALINA_PID=/opt/tomcat/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINA_BASE=/opt/tomcat
Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"

ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh

ExecReload=/bin/kill $MAINPID
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

				
			

Save and close the file then reload the systemd daemon to apply the changes.

				
					systemctl daemon-reload
				
			

Next, start the Apache Tomcat service and enable it to start at system reboot with the following command.

				
					systemctl enable --now tomcat
				
			

Also check the status of the Apache Tomcat service with the following command.

				
					systemctl status tomcat
				
			

This shows you the active status of Apache Tomcat.

To check the Apache Tomcat listening ports, run the following command.

				
					ss -antpl | grep -i java
				
			

You should see Tomcat ports on the following screen.

Configure Apache Tomcat

At this point, the Apache Tomcat is installed and running on your server. Now, you need to add an admin user and password to secure the Apache Tomcat. You can do it by editing tomcat-users.xml file.

				
					nano /opt/tomcat/conf/tomcat-users.xml
				
			

Add the following lines above the line </tomcat-users>:

				
					
   <role rolename="admin-gui"/>
   <role rolename="manager-gui"/>
   <user username="admin" password="securepassword" roles="admin-gui,manager-gui"/>

				
			

Save and close the file when you are finished.

Enable Apache Tomcat Remote Access

By default, Apache Tomcat is accessed only from the localhost. To access the Apache Tomcat from the remote machine, you need to edit the context.xml file and remove the access restriction.

To enable remote access for Tomcat Manager, edit the context.xml file.

				
					nano /opt/tomcat/webapps/manager/META-INF/context.xml
				
			

Remove the following lines:

				
					  <Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />

				
			

To enable remote access for Tomcat Host Manager, edit the context.xml file.

				
					nano /opt/tomcat/webapps/host-manager/META-INF/context.xml
				
			

Remove the following lines.

				
					  <Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />

				
			

Save and close the file when you are done. Then, restart the Tomcat service to implement the changes.

				
					systemctl restart tomcat
				
			

Access Apache Tomcat

Now, Apache Tomcat is installed on your server. Access it using the URL http://your-server-ip:8080 on your web browser.  You should see the Tomcat dashboard on the following screen.

Click on the Manager App. You will be asked to provide Tomcat admin username and password as shown below.

Provide your admin username, password and click on the Sign in button. You should see the Tomcat Manager App on the following screen.

Next, click on the Host Manager. You should see the Host Manager on the following screen.

To check the Apache Tomcat status, click on the Status button. You should see the following screen.

Thank you for reading How to Install Apache Tomcat Server on Ubuntu 22.04. We shall conclude the article now. 

How to Install Apache Tomcat Server on Ubuntu 22.04 Conclusion

Java development teams may face challenges in choosing the most suitable application server for their needs. However, it is essential to consider the application’s requirements constantly. This prevents the selection of a tool that offers more features than necessary, leading to additional strain on memory, start up times and setup.

If unsure about which server works best, it is recommended to try Tomcat first and determine whether it meets the application’s requirements. The installation and deployment process for Tomcat takes only a few minutes, making it a quick and easy option to try.

Feel free to explore more of our Apache Tomcat content by navigating to our blog over here

Avatar for Hitesh Jethva
Hitesh Jethva

I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x