Install Apache Tomcat on AlmaLinux 9: Best Servlet Container
In this guide, we want to teach you How To Install Apache Tomcat on AlmaLinux 9. Apache Tomcat is a long-lived, open-source Java servlet container that implements core Java enterprise (now Jakarta EE) specifications, including the Jakarta Servlet, Jakarta Server Pages, and Jakarta WebSocket specs.
Today, it remains the most widely used Java application server, boasting a well-tested and proven core engine with good extensibility. You can now proceed to the guide steps below on the Orcacore website to complete the Install Apache Tomcat on AlmaLinux 9 setup.
Steps To Install and Configure Apache Tomcat on AlmaLinux 9
To complete this guide, you must log in to your server as a non-root user with sudo privileges and set up a basic firewall. To do this, you can follow our guide on Initial Server Setup with AlmaLinux 9.
1. Install Java For Setting Up Tomcat
To install Tomcat, you need to have Java installed on your server. First, update your local package index with the following command:
sudo dnf update -y
Then, use the command below to install Java on AlmaLinux 9:
sudo dnf install java -y
You can verify your Java installation by checking its version:
java --version
In your output you will see:
**Output**
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)
2. Install Tomcat on AlmaLinux 9
At this point, to install the latest Tomcat on your server, you need to visit the Tomcat downloads page. We will Install Apache Tomcat on AlmaLinux 9 version 10 in this guide steps.
Download Tomcat 10
Now check for the latest release and download it to your server by using the wget command:
sudo wget https://archive.apache.org/dist/tomcat/tomcat-10/v10.1.1/bin/apache-tomcat-10.1.1.tar.gz
When your download is completed, extract it by using the command below:
sudo tar -xvf apache-tomcat-10*.tar.gz
Next, move your file to the /usr/local/tomcat
directory:
sudo mv apache-tomcat-10.1.1 /usr/local/tomcat
Create Tomcat User
At this point, you need to run the Tomcat service as a normal user. To do this, create a Tomcat user and group by using the commands below:
# sudo groupadd tomcat
# sudo useradd -d /usr/local/tomcat -r -s /bin/false -g tomcat tomcat
Then, set the correct ownership of the directory to the Tomcat user by using the following command:
sudo chown -R tomcat:tomcat /usr/local/tomcat/
You can verify the change of ownership using the following command:
sudo ls -l /usr/local/tomcat
**Output**
drwxr-x--- 2 tomcat tomcat 4096 Nov 3 04:38 bin
-rw-r----- 1 tomcat tomcat 20123 Oct 3 08:42 BUILDING.txt
drwx------ 2 tomcat tomcat 4096 Oct 3 08:42 conf
-rw-r----- 1 tomcat tomcat 6210 Oct 3 08:42 CONTRIBUTING.md
drwxr-x--- 2 tomcat tomcat 4096 Nov 3 04:38 lib
-rw-r----- 1 tomcat tomcat 60393 Oct 3 08:42 LICENSE
drwxr-x--- 2 tomcat tomcat 4096 Oct 3 08:42 logs
-rw-r----- 1 tomcat tomcat 2333 Oct 3 08:42 NOTICE
-rw-r----- 1 tomcat tomcat 3398 Oct 3 08:42 README.md
-rw-r----- 1 tomcat tomcat 6775 Oct 3 08:42 RELEASE-NOTES
-rw-r----- 1 tomcat tomcat 16073 Oct 3 08:42 RUNNING.txt
drwxr-x--- 2 tomcat tomcat 4096 Nov 3 04:38 temp
drwxr-x--- 7 tomcat tomcat 4096 Oct 3 08:42 webapps
drwxr-x--- 2 tomcat tomcat 4096 Oct 3 08:42 work
3. Configure Apache Tomcat on AlmaLinux 9
At this point, you need to configure the CATALINA_HOME
Environment variable required to run the Tomcat server. To do this, run the following commands:
# echo "export CATALINA_HOME="/usr/local/tomcat"" >> ~/.bashrc
# source ~/.bashrc
In order to access the Tomcat Web Management Interface, you need to create a user to access the manager-gui
and admin-gui
.
Edit Tomcat Users File
Here you need to open the tomcat user file on AlmaLinux 9 by using your favorite text editor like Vi Editor or Nano Editor:
sudo vi /usr/local/tomcat/conf/tomcat-users.xml
Add the following lines just above the </tomcat-users>
tag at the end of the file.
<role rolename="manager-gui"/>
<role rolename="admin-gui"/>
<user username="admin" password="admin-pass" roles="manager-gui,admin-gui"/>
Remember to update the ‘password’ field to match your preferred one. Save and exit the file, when you are done.
At this point, you need to allow the tomcat manager and host manager to be accessible.
Edit Tomcat Host Manager File
Open the host manager file with your favorite text editor, we use vi:
sudo vi /usr/local/tomcat/webapps/host-manager/META-INF/context.xml
Comment on the following line as shown below:
<!-- <Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127.d+.d+.d+|::1|0:0:0:0:0:0:0:1" /> -->
When you are done, save and close the file.
Edit Tomcat Manager App
Open the Tomcat Manager app:
sudo vi /usr/local/tomcat/webapps/manager/META-INF/context.xml
Comment on the following line as shown below:
<!-- <Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127.d+.d+.d+|::1|0:0:0:0:0:0:0:1" /> -->
When you are done, save and close the file.
4. Run Tomcat as a service on AlmaLinux 9
At this point, you can use the following command to start your Tomcat service:
# cd /usr/local/tomcat
# sudo ./bin/startup.sh
You will get the following output:
**Output**
Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.
5. Configure Firewall for Tomcat
At this point, we assume that you have enabled firewalls. So you need to allow port 8080 for Tomcat through the firewall:
sudo firewall-cmd --zone=public --add-port=8080/tcp --permanent
Reload the firewall to apply the new rules:
sudo firewall-cmd --reload
6. Access Apache Tomcat Web GUI
In your web browser type your server’s IP address followed by 8080:
http://server-IP-adress:8080
You will see:
[Image of Tomcat default page]
To access the Manager GUI click on Manager App. Then you will be presented will a login prompt. Enter the user and password that you have provided before.
Then, you will see your Manager App screen:
[Image of Tomcat manager app]
For the Host Manager GUI, you follow the same process and the page is as shown below.
[Image of Tomcat host manager]
Conclusion
At this point, you have learned to Install Apache Tomcat on AlmaLinux 9. Apache Tomcat in AlmaLinux 9 is used as a web server and servlet container to run Java-based web applications. It supports Java Servlet, JavaServer Pages (JSP), and WebSocket technologies, making it ideal for deploying dynamic Java applications.
Hope you enjoy it. Please subscribe to us on Facebook, Instagram, and YouTube.
You may also like these articles:
Set up Apache Tomcat on AlmaLinux 8
How To Set up Apache Tomcat on Debian 11
Install and Configure Apache Tomcat on Ubuntu 22.04
Alternative Solutions for Installing Apache Tomcat on AlmaLinux 9
While the method described above works perfectly well for installing and configuring Apache Tomcat on AlmaLinux 9, let’s explore two alternative approaches: using a package manager and using Docker.
1. Installing Apache Tomcat using a Package Manager (Less Recommended)
While not the preferred method for the latest Tomcat versions, some repositories might offer older versions of Tomcat through package managers like dnf
. This approach is generally not recommended for production environments as it often provides outdated releases. It’s best suited for quick testing or development where the specific Tomcat version isn’t critical.
Explanation:
AlmaLinux’s default repositories might contain a Tomcat package. Installing through dnf
simplifies the process, handling dependencies automatically. However, the version available is often older than the latest stable release from the Apache Tomcat website. Using the latest version allows for the most up-to-date security and feature implementations.
Procedure:
-
Search for available Tomcat packages:
sudo dnf search tomcat
This command lists any Tomcat-related packages available in the configured repositories. You might find packages like
tomcat
ortomcat8
. -
Install the desired Tomcat package (if available):
sudo dnf install tomcat
Replace
tomcat
with the actual package name found in the search results. -
Start and enable the Tomcat service:
sudo systemctl start tomcat sudo systemctl enable tomcat
-
Configure Tomcat (if needed):
Configuration files are usually located in
/etc/tomcat
. You may need to adjust settings like ports, user accounts, and virtual hosts depending on your requirements.
Limitations:
- Outdated versions: The biggest drawback is the potential for installing an older, unsupported Tomcat version.
- Limited control: You have less control over the installation directory and configuration compared to manual installation.
2. Installing Apache Tomcat using Docker
A more modern and flexible approach is to use Docker to run Apache Tomcat. Docker containers provide isolated environments, ensuring consistency across different systems and simplifying deployment. This is a great option for developers looking for a portable and repeatable Tomcat instance.
Explanation:
Docker allows you to package applications and their dependencies into containers. This ensures that Tomcat runs in a consistent environment, regardless of the underlying operating system. It also simplifies managing multiple Tomcat instances with different configurations.
Procedure:
-
Install Docker: If you don’t have Docker installed, follow the instructions on the official Docker website for AlmaLinux.
sudo dnf install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin sudo systemctl start docker sudo systemctl enable docker
-
Pull the official Tomcat Docker image:
docker pull tomcat
This command downloads the latest official Tomcat image from Docker Hub. You can specify a specific version by using a tag, for example,
docker pull tomcat:10.1
. -
Run the Tomcat container:
docker run -d -p 8080:8080 --name mytomcat tomcat
-d
: Runs the container in detached mode (in the background).-p 8080:8080
: Maps port 8080 on the host machine to port 8080 inside the container. This allows you to access Tomcat from your browser.--name mytomcat
: Assigns the name "mytomcat" to the container.tomcat
: Specifies the image to use (the Tomcat image we pulled earlier).
-
Access Tomcat: Open your web browser and navigate to
http://your_server_ip:8080
. You should see the default Tomcat welcome page. -
Managing the Tomcat Container:
- Stopping the container
docker stop mytomcat
- Starting the container
docker start mytomcat
- Restarting the container
docker restart mytomcat
- Removing the container
docker rm mytomcat
- Stopping the container
Advanced Configuration (Mounting a Volume):
To deploy your own web applications or modify Tomcat’s configuration, you can mount a volume from your host machine into the container. This allows you to share files between your host and the container. For example:
docker run -d -p 8080:8080 -v /path/to/your/webapps:/usr/local/tomcat/webapps --name mytomcat tomcat
In this example, the /path/to/your/webapps
directory on your host machine is mounted to the /usr/local/tomcat/webapps
directory inside the container. Any web applications you place in /path/to/your/webapps
will be automatically deployed to Tomcat.
Benefits:
- Isolation: Docker containers provide isolated environments, preventing conflicts with other applications on your system.
- Consistency: Ensures Tomcat runs consistently across different environments.
- Portability: Easy to move Tomcat instances between different machines.
- Simplified Deployment: Docker simplifies the deployment process, making it easier to manage multiple Tomcat instances.
Both alternative methods offer distinct advantages and disadvantages. Using dnf
for Install Apache Tomcat on AlmaLinux 9 is quick but may result in an outdated Tomcat version. Docker provides a more robust, flexible, and portable solution, making it ideal for development and production environments. Choose the method that best suits your specific needs and technical expertise.