Discover how to install Apache Tomcat on Rocky Linux 9 with our detailed guide. Empower your web application development and deployment process by harnessing the capabilities of this powerful and versatile servlet container. Follow our step-by-step instructions for seamless integration on your Linux environment. #centlinux #linux #apachetomcat
Table of Contents
What is Apache Tomcat?
Apache Tomcat (called “Tomcat” for short) is a free and open-source implementation of the Jakarta Servlet, Jakarta Expression Language, and WebSocket technologies. It provides a “pure Java” HTTP web server environment in which Java code can also run. Thus it’s a Java web application server, although not a full JEE application server.
Tomcat is developed and maintained by an open community of developers under the auspices of the Apache Software Foundation, released under the Apache License 2.0 license.
What is Apache Tomcat used for?
Apache Tomcat is a widely used open-source servlet container that is used for deploying and running Java-based web applications. It functions as a lightweight web server that serves Java Servlets and JavaServer Pages (JSP) over the HTTP protocol. Here are some common use cases for Apache Tomcat:
- Hosting Java Web Applications: Apache Tomcat is primarily used for hosting Java web applications, including dynamic websites, web services, and enterprise applications. It provides a runtime environment for executing Java Servlets, JSP pages, and other Java-based web components.
- Java Servlets and JSP Development: Developers use Apache Tomcat as a development and testing environment for building Java Servlets and JSP-based web applications. Tomcat’s lightweight and fast startup make it ideal for rapid development and iterative testing of web applications.
- Java Enterprise Edition (Java EE) Compatibility: Apache Tomcat implements the Java Servlet, JavaServer Pages, Java Expression Language (EL), and Java WebSocket APIs, making it compatible with Java EE specifications. Developers can deploy Java EE applications to Tomcat without requiring a full Java EE application server.
- Embedding in Applications: Apache Tomcat can be embedded within other Java applications to provide web server functionality. This allows developers to package their applications with an embedded servlet container, simplifying deployment and distribution.
- Integration with Frameworks: Apache Tomcat is often integrated with various Java frameworks and technologies, such as Spring Framework, Struts, Hibernate, and Apache CXF. These frameworks leverage Tomcat’s servlet container capabilities to build and deploy web applications more efficiently.
- Web Services and Microservices: Apache Tomcat can be used to deploy and run web services and microservices built using Java technologies. It provides a scalable and reliable runtime environment for hosting lightweight services that communicate over HTTP.
Overall, Apache Tomcat is a versatile and widely used servlet container that provides developers with a lightweight, scalable, and efficient platform for deploying Java-based web applications. Its ease of use, robustness, and compatibility with Java standards make it a popular choice for web application development and deployment in both development and production environments.
Apache vs Tomcat
Apache and Tomcat are often compared because they are both used in the realm of web server technology, but they serve different purposes and have different functionalities:
- Apache HTTP Server (Apache):
- Apache HTTP Server, commonly known as Apache, is a powerful and widely used open-source web server software.
- It is designed to serve static content over the HTTP protocol and can handle a variety of HTTP requests, including serving HTML files, images, and other static resources.
- Apache supports various modules and extensions that allow developers to extend its functionality, such as mod_rewrite for URL rewriting, mod_ssl for SSL/TLS encryption, and mod_proxy for reverse proxying.
- Apache is often used as a front-end web server in conjunction with other technologies, such as application servers or servlet containers like Tomcat, to serve dynamic content generated by web applications.
- Apache Tomcat:
- Apache Tomcat is an open-source servlet container that is used to deploy and run Java-based web applications.
- It is designed specifically for executing Java Servlets, JavaServer Pages (JSP), and other Java-based web components.
- Tomcat provides a runtime environment for Java web applications, handling HTTP requests, managing servlets, and processing JSP pages.
- While Tomcat can serve static content like HTML files and images, its primary purpose is to execute Java web applications and serve dynamic content generated by those applications.
In summary, Apache and Tomcat serve different roles in web server technology. Apache HTTP Server is primarily used as a general-purpose web server for serving static content, while Apache Tomcat is specifically designed for executing Java web applications. In many cases, Apache and Tomcat are used together, with Apache serving as the front-end web server and handling static content, while Tomcat serves dynamic content generated by Java web applications.
Recommended Online Training: Apache Tomcat Server from Beginners to Advanced
Environment Specification
We are using a minimal installed Rocky Linux 9 virtual machine with following specifications.
- CPU – 3.4 Ghz (2 cores)
- Memory – 2 GB
- Storage – 20 GB
- Operating System – Rocky Linux release 9.1 (Blue Onyx)
- Hostname – tomcat-01.centlinux.com
- IP Address – 192.168.116.131/24
Read Also: How to install Apache Tomcat on CentOS 7
Update your Rocky Linux Server
By using a ssh client, login to your Linux server as root user.
Set a FQDN (Fully Qualified Domain Name) for your Rocky Linux server.
# hostnamectl set-hostname tomcat-01.centlinux.com
Refresh your yum cache by using dnf command.
# dnf makecache Rocky Linux 9 - BaseOS 280 kB/s | 1.7 MB 00:06 Rocky Linux 9 - AppStream 653 kB/s | 6.4 MB 00:09 Rocky Linux 9 - Extras 3.0 kB/s | 7.0 kB 00:02 Metadata cache created.
Execute following command to upgrade software packages in your Linux operating system.
# dnf update -y
Sometimes, the above command also upgrades your Linux Kernel.
In such situation, you should reboot your Linux operating system with newly installed Kernel.
# reboot
After reboot, check the Linux Kernel and operating system versions.
# cat /etc/rocky-release Rocky Linux release 9.1 (Blue Onyx) # uname -r 5.14.0-162.6.1.el9_1.0.1.x86_64
Install Apache Tomcat Prerequisites
By using dnf command, install software packages that are required to download and install Apache Tomcat software.
# dnf install -y wget tar gzip
Apache Tomcat 10.0.x requires Java 8 or later JRE (Java Runtime Environment).
Therefore, you can install OpenJDK 11 (an open source implementation of Java platform) on Linux machine.
# dnf install -y java-11-openjdk
After installation, check the version of your active Java runtime environment.
# java --version openjdk 11.0.17 2022-10-18 LTS OpenJDK Runtime Environment (Red_Hat-11.0.17.0.8-2.el9_0) (build 11.0.17+8-LTS) OpenJDK 64-Bit Server VM (Red_Hat-11.0.17.0.8-2.el9_0) (build 11.0.17+8-LTS, mixed mode, sharing)
Create a Linux user to own Tomcat software and processes.
# useradd -r -d /opt/tomcat/ -s /sbin/nologin -c "Tomcat User" tomcat
Install Apache Tomcat on Rocky Linux 9
Apache Tomcat website provides the latest version of Java application server for various platforms.
You can use wget command to download Tomcat software directly from Linux CLI.
# cd /tmp # wget https://dlcdn.apache.org/tomcat/tomcat-10/v10.0.27/bin/apache-tomcat-10.0.27.tar.gz --2022-12-13 08:48:03-- https://dlcdn.apache.org/tomcat/tomcat-10/v10.0.27/bin/apache-tomcat-10.0.27.tar.gz Resolving dlcdn.apache.org (dlcdn.apache.org)... 151.101.2.132, 2a04:4e42::644 Connecting to dlcdn.apache.org (dlcdn.apache.org)|151.101.2.132|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 11984522 (11M) [application/x-gzip] Saving to: ‘apache-tomcat-10.0.27.tar.gz’ apache-tomcat-10.0. 100%[===================>] 11.43M 1.14MB/s in 10s 2022-12-13 08:48:13 (1.14 MB/s) - ‘apache-tomcat-10.0.27.tar.gz’ saved [11984522/11984522]
Create a directory and extract downloaded tarball therein.
# mkdir /opt/tomcat # tar xf apache-tomcat-10.0.27.tar.gz -C /opt/tomcat --strip-components=1
Grant ownership of /opt/tomcat directory to tomcat user.
# chown -R tomcat:tomcat /opt/tomcat/
You need to create one or more admin users to manage your Tomcat server via Application Manager.
Edit tomcat-users.xml file by using vim text editor.
# vi /opt/tomcat/conf/tomcat-users.xml
Add following lines just before </tomcat-users> tag.
<role rolename="admin-gui"/> <role rolename="manager-gui"/> <user username="admin" password="admin" roles="admin-gui,manager-gui"/> <user username="ahmer" password="123" roles="admin-gui,manager-gui"/>
By default Application Manager is allowed to be accessed from localhost only.
Therefore, you must edit the following file to make your application manager accessible from other machines within the same network.
# vi /opt/tomcat/webapps/manager/META-INF/context.xml
Search for following lines in this file.
<Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="127.d+.d+.d+|::1|0:0:0:0:0:0:0:1" />
Comment out these lines as follows.
<!-- <Valve className="org.apache.catalina.valves.RemoteAddrValve" allow="127.d+.d+.d+|::1|0:0:0:0:0:0:0:1" />-->
Similarly, repeat the above step for following file.
# vi /opt/tomcat/webapps/host-manager/META-INF/context.xml
Create Systemd Service
Create a systemd unit to define a service for your Java application server.
# vi /usr/lib/systemd/system/tomcat.service
Add following directives in this file.
[Unit] Description=Apache Tomcat Server After=syslog.target network.target [Service] Type=forking User=tomcat Group=tomcat Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid Environment=CATALINA_HOME=/opt/tomcat Environment=CATALINA_BASE=/opt/tomcat ExecStart=/opt/tomcat/bin/catalina.sh start ExecStop=/opt/tomcat/bin/catalina.sh stop RestartSec=10 Restart=always [Install] WantedBy=multi-user.target
Execute following command to notify systemd that you have created a new unit file.
# systemctl daemon-reload
Enable and start Tomcat service.
# systemctl enable --now tomcat.service Created symlink /etc/systemd/system/multi-user.target.wants/tomcat.service → /usr/lib/systemd/system/tomcat.service.
Verify the status of Tomcat service.
# systemctl status tomcat.service
● tomcat.service - Apache Tomcat Server
Loaded: loaded (/usr/lib/systemd/system/tomcat.service; enabled; vendor pr>
Active: active (running) since Tue 2022-12-13 09:17:12 CST; 15s ago
Process: 15601 ExecStart=/opt/tomcat/bin/catalina.sh start (code=exited, st>
Main PID: 15612 (java)
Tasks: 28 (limit: 4463)
Memory: 85.0M
CPU: 10.879s
CGroup: /system.slice/tomcat.service
└─15612 /usr/bin/java -Djava.util.logging.config.file=/opt/tomcat/>
Dec 13 09:17:12 tomcat-01.centlinux.com systemd[1]: Starting Apache Tomcat Serv>
Dec 13 09:17:12 tomcat-01.centlinux.com catalina.sh[15601]: Tomcat started.
Dec 13 09:17:12 tomcat-01.centlinux.com systemd[1]: Started Apache Tomcat Serve>
Configure Linux Firewall
Allow the Tomcat default service port 8080/tcp in Linux firewall, to make it accessible across the network.
# firewall-cmd --permanent --add-port=8080/tcp success # firewall-cmd --reload success
Accessing Application Manager
Open URL http://tomcat-01.centlinux.com:8080/ in a web browser.
Click on “Server Status” button to check you Java application server status.
Go back and click on “Application Manager”. You may need to login by using the credentials that you have defined in tomcat-users.xml file.
Final Thoughts
Unlock the potential of your web applications by mastering the installation of Apache Tomcat on Rocky Linux 9. With our comprehensive guide, you’ll seamlessly integrate this powerful servlet container into your development and deployment workflow. Let this tutorial be your gateway to building and hosting dynamic, Java-based web applications with ease and efficiency. To start using your Java application server, we recommend that you should have Tomcat: The Definitive Guide (PAID LINK) by O’Reilly Media as a quick reference.