Install Apache Tomcat on Rocky Linux 9 with Best Steps

Posted on

Install Apache Tomcat on Rocky Linux 9 with Best Steps

Install Apache Tomcat on Rocky Linux 9 with Best Steps

This guide will walk you through How To Install Apache Tomcat on Rocky Linux 9. Apache Tomcat is a robust open-source Java servlet and Java Server Page container that empowers developers to deploy a wide array of enterprise Java applications. Tomcat serves as a vital HTTP web server environment where Java code can execute seamlessly. Let’s delve into the steps required for Install Apache Tomcat on Rocky Linux 9.

You can now proceed to the guide steps below to complete Apache Tomcat Setup on Rocky Linux 9.

Comprehensive Steps To Install Apache Tomcat on Rocky Linux 9

To complete Tomcat Setup, ensure you are logged into your server as a non-root user with sudo privileges and have a basic firewall set up. If you haven’t already, you can refer to our guide on Initial Server Setup with Rocky Linux 9. This will make your experience with Install Apache Tomcat on Rocky Linux 9 a lot smoother.

1. Install Java for Tomcat Setup

Tomcat requires Java to be installed on the server.

First, update your local package index using the following command:

sudo dnf update -y

Then, install Java on Rocky Linux 9 using the command below:

sudo dnf install java -y

Verify your Java installation by checking the version:

java --version

You should see output similar to this:

**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 10 on Rocky Linux 9

To install the latest Tomcat on your server, visit the Tomcat downloads page.

Download Tomcat

Find the latest release and download it to your server using the wget command. Note that the version number may differ depending on when you’re performing the installation:

sudo wget https://archive.apache.org/dist/tomcat/tomcat-10/v10.1.1/bin/apache-tomcat-10.1.1.tar.gz

Once the download completes, extract the archive using the command:

sudo tar -xvf apache-tomcat-10*.tar.gz

Next, move the extracted directory to the /usr/local/tomcat directory:

sudo mv apache-tomcat-10.1.1 /usr/local/tomcat

Create Tomcat User

It’s best practice to run the Tomcat service as a dedicated user. Create a Tomcat user and group using the following commands:

# sudo groupadd tomcat
# sudo useradd -d /usr/local/tomcat -r -s /bin/false -g tomcat tomcat

Set the correct ownership of the /usr/local/tomcat directory to the Tomcat user:

sudo chown -R tomcat:tomcat /usr/local/tomcat/

You can verify the change of ownership with 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 Rocky Linux 9

Now, configure the CATALINA_HOME environment variable required to run the Tomcat server.

Execute the following commands:

# echo "export CATALINA_HOME="/usr/local/tomcat"" >> ~/.bashrc
# source ~/.bashrc

To access the Tomcat Web Management Interface, create a user to access the manager-gui and admin-gui.

Edit Tomcat Users File

Open the Tomcat user file using your preferred text editor (e.g., vi):

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 replace "admin-pass" with a strong, secure password.

Save and exit the file.

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:

sudo vi /usr/local/tomcat/webapps/host-manager/META-INF/context.xml

Comment out 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" /> -->

Save and close the file.

Edit Tomcat Manager App

Open the Tomcat manager app configuration:

sudo vi /usr/local/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" /> -->

Save and close the file.

4. Run Tomcat as a service on Rocky Linux 9

You can start the Tomcat service using the following commands:

# cd /usr/local/tomcat
# sudo ./bin/startup.sh

You should see output similar to:

**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

Assuming you have enabled firewalld, 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 Manager App

In your web browser, type your server’s IP address followed by port 8080:

http://<server-IP-adress>:8080

You should see the Tomcat default page.

[Image of Tomcat default page]

To access the Manager GUI, click on Manager App. You will be prompted for a username and password. Enter the credentials you configured earlier in tomcat-users.xml.

[Image of Tomcat Manager App]

For the Host manager GUI, follow the same process.

[Image of Tomcat Host Manager]

That’s it, you are done! You’ve successfully completed the Install Apache Tomcat on Rocky Linux 9 procedure.

Conclusion

At this point, you have learned to Install Apache Tomcat on Rocky Linux 9. Apache Tomcat on Rocky Linux 9 is used for deploying and running Java-based web applications, providing a lightweight and reliable Java servlet and JSP container.

Hope you enjoy it. Please follow us on social media.

You may be like these articles:

Alternative Solutions for Installing Tomcat on Rocky Linux 9

While the above method details manual installation, there are other approaches you can take, which can often simplify the process. Here are two alternatives for installing Tomcat on Rocky Linux 9:

1. Using a Tomcat Docker Container

Docker provides a way to containerize applications, which isolates them from the host system and simplifies deployment. Using a Tomcat Docker container can be a convenient way to run Tomcat without needing to manually install and configure it on your Rocky Linux 9 system.

Explanation:

  • Containerization: Docker containers encapsulate the Tomcat application and all its dependencies into a single package.
  • Isolation: This isolation ensures that Tomcat runs in a consistent environment, regardless of the underlying host system.
  • Simplified Deployment: Docker simplifies deployment as you only need to pull the Tomcat image and run the container.

Steps:

  1. Install Docker: If Docker isn’t already installed, install it using the following commands:

    sudo dnf install docker -y
    sudo systemctl start docker
    sudo systemctl enable docker
  2. Pull the Tomcat Image: Pull the official Tomcat image from Docker Hub:

    sudo docker pull tomcat
  3. Run the Tomcat Container: Run the Tomcat container, mapping port 8080 on the host to port 8080 in the container:

    sudo docker run -d -p 8080:8080 --name mytomcat tomcat

    This command runs the Tomcat container in detached mode (-d), maps port 8080 (-p 8080:8080), names the container mytomcat, and uses the tomcat image.

  4. Access Tomcat: Access Tomcat in your web browser using your server’s IP address and port 8080: http://<server-IP-address>:8080

  5. Managing the Container: You can stop, start, and remove the container using the following Docker commands:

    • Stop: sudo docker stop mytomcat
    • Start: sudo docker start mytomcat
    • Remove: sudo docker rm mytomcat

Code Example (Dockerfile – Optional, for Custom Images):

If you want to create a custom Tomcat image with specific configurations or applications, you can use a Dockerfile. Here’s a basic example:

FROM tomcat:latest

# Copy your WAR file to the Tomcat webapps directory
COPY myapp.war /usr/local/tomcat/webapps/

# Expose port 8080
EXPOSE 8080

# Start Tomcat (optional, usually the base image handles this)
CMD ["catalina.sh", "run"]

To build this image, save it as Dockerfile in a directory containing your myapp.war file, and run:

sudo docker build -t mycustomtomcat .

Then run the custom image:

sudo docker run -d -p 8080:8080 --name mycustomtomcat mycustomtomcat

2. Using SDKMAN! to Manage Java and Tomcat (Less Common, but Flexible)

SDKMAN! (Software Development Kit Manager) is a tool for managing parallel versions of multiple Software Development Kits on most Unix-based systems. While less direct for Tomcat installation, it provides fine-grained control over Java versions and could be coupled with manual Tomcat installation for a very customized environment.

Explanation:

  • Java Version Management: SDKMAN! allows you to easily switch between different versions of the JDK, ensuring compatibility with your Tomcat installation.
  • Environment Control: It helps manage environment variables and dependencies for Java-based applications.
  • Flexibility: This method offers the most control over your Java and Tomcat environment.

Steps:

  1. Install SDKMAN!: Install SDKMAN! by running the following command in your terminal:

    curl -s "https://get.sdkman.io" | bash
    source "$HOME/.sdkman/bin/sdkman-init.sh"
  2. Install a Java Version: Use SDKMAN! to install a specific version of Java (e.g., OpenJDK 11):

    sdk install java 11.0.17-open

    Replace 11.0.17-open with the actual version available in SDKMAN!’s list. You can list available versions with sdk list java.

  3. Set the Java Version: Set the installed Java version as the default:

    sdk use java 11.0.17-open
  4. Install Tomcat Manually: Follow the manual installation steps from the original guide, ensuring that the CATALINA_HOME and JAVA_HOME environment variables are correctly configured to point to the SDKMAN!-managed Java installation.

  5. Verify: Double-check that the JAVA_HOME environment variable is correctly set using echo $JAVA_HOME. It should point to the SDKMAN! Java directory.

Code Example (Setting JAVA_HOME):

After installing Java with SDKMAN!, you might need to explicitly set JAVA_HOME in your ~/.bashrc or ~/.bash_profile file:

export JAVA_HOME="$HOME/.sdkman/candidates/java/current"
export PATH="$JAVA_HOME/bin:$PATH"

Remember to source ~/.bashrc or source ~/.bash_profile after making these changes.

While SDKMAN! doesn’t directly install Tomcat, it provides a powerful way to manage Java versions, which is essential for a successful Tomcat installation, especially when dealing with different Java application requirements. This is particularly useful if you need multiple Java versions on your system for different applications.