Set up Apache Tomcat on AlmaLinux 8

In this article, we want to teach you How To Set up Apache Tomcat on AlmaLinux 8.

Apache Tomcat is a long-lived, open-source Java servlet container that implements several core Java enterprise specs, namely the Java Servlet, JavaServer Pages (JSP), and WebSockets APIs.

How To Set up Apache Tomcat on AlmaLinux 8

Before you start to install Apache Tomcat on AlmaLinux 8, you need to 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 article the Initial Server Setup with AlmaLinux 8.

Now you can follow the steps below to set up Apache Tomcat on your server.

Install Java on AlmaLinux 8

To install Apache Tomcat, you need to have Java installed on your AlmaLinux 8.

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

sudo dnf update

Then, install OpenJDK 11 on your server with the following command:

sudo dnf install java-11-openjdk.x86_64

You can verify your Java installation by checking its version:

java --version

In your output you will see:

Output
openjdk 11.0.13 2021-10-19 LTS
OpenJDK Runtime Environment 18.9 (build 11.0.13+8-LTS)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.13+8-LTS, mixed mode, sharing)

Here you need to create a Tomcat group and user that will have access to Tomcat only.

You can use the following command to create a Tomcat group:

sudo groupadd tomcat

Now create a directory to save the Tomcat files with the following command:

sudo mkdir /opt/tomcat

Add the user in the above directory and disable its login rights with the following command:

sudo useradd -s /bin/nologin -g tomcat -d /opt/tomcat tomcat

Install Tomcat on AlmaLinux 8

First, you need to download the latest Apache Tomcat version. You can check for the latest version on the Apache Tomcat Downloads page and copy the tar.gz binary distributions link address.

Then, use the wget command to download the Tomcat 10, paste your copied link in the below command:

wget https://dlcdn.apache.org/tomcat/tomcat-10/v10.0.13/bin/apache-tomcat-10.0.13.tar.gz

When your download is completed, extract your file and copy it into the /opt/tomcat directory with the following command:

sudo tar -zxvf apache-tomcat-*.tar.gz -C /opt/tomcat --strip-components=1

Now you need to set the correct permissions for the Tomcat user with the following command:

sudo chown -R tomcat: /opt/tomcat

Also, you need to allow the script available inside the folder to execute with the command below:

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

Here you should create a systemd unit file for Tomcat. Create the file with your favorite text editor, here we use vi:

sudo vi /etc/systemd/system/tomcat.service

Then, paste the following content to the file:

[Unit]
Description=Tomcat webs servlet container
After=network.target

[Service]
Type=forking

User=tomcat
Group=tomcat

Environment="JAVA_HOME=/usr/lib/jvm/jre"
Environment="JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom"

Environment="CATALINA_BASE=/opt/tomcat"
Environment="CATALINA_HOME=/opt/tomcat"
Environment="CATALINA_PID=/opt/tomcat/temp/tomcat.pid"
Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"

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

[Install]
WantedBy=multi-user.target

When you are done, save and close the file.

At this point, you can start and enable your Tomcat service on AlmaLinux 8 with the following commands:

$ sudo systemctl start tomcat
$ sudo systemctl enable tomcat

Verify that your Tomcat service is active and running on your server with the command below:

systemctl status tomcat

In your output you should see:

Output
tomcat.service - Tomcat webs servlet container
   Loaded: loaded (/etc/systemd/system/tomcat.service; enabled; vendor preset: >
   Active: active (running) since Tue 2021-12-07 07:36:33 EST; 13s ago
 Main PID: 89736 (java)
    Tasks: 19 (limit: 11409)
   Memory: 126.5M
   CGroup: /system.slice/tomcat.service
           └─89736 /usr/lib/jvm/jre/bin/java -Djava.util.logging.config.file=/o>
To access the Apache Tomcat web interface, you need to open port 8080 through the AlmaLinux firewall.
We assumed that you have enabled firewalld from the requirements. To allow traffic on port 8080 you can use the following command:
sudo firewall-cmd --zone=public --permanent --add-port=8080/tcp

Reload the firewall to apply the new rules:

sudo firewall-cmd --reload

Now you can access the Apache Tomcat web interface on AlmaLinux 8.

Access Apache Tomcat web Interface

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

http://server-IP-adress:8080

You will see:

Apache Tomcat web interface

If you want to access the “Server Status“, “Manager App” and “Host Manager”, you should follow the steps below.

First, you need to add a username and password to the Tomcat User XML file on AlmaLinux 8. Open the file with your favorite text editor, here we use vi:

sudo vi /opt/tomcat/conf/tomcat-users.xml

At the end of the file before </tomcat-users> paste the following contents:

<role rolename="admin"/>
<role rolename="admin-gui"/>
<role rolename="manager"/>
<role rolename="manager-gui"/>

<user username="user" password="pwd" roles="admin,admin-gui,manager,manager-gui"/>

Remember to replace the username and password with your own.

When you are done, save and close the file.

Second, edit the manager context file:

sudo vi /opt/tomcat/webapps/manager/META-INF/context.xml

Comment 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 finished, save and close the file.

Next, you need to edit the host manager context file:

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

Again, comment on the following line as above:

<!-- <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.

To apply the changes, restart Tomcat on AlmaLinux 8:

sudo systemctl restart tomcat

Now you can refresh your Tomcat web interface and click on the Manager app. You need to enter the username and password that you have defined in the XML file.

Tomcat web app manager

Conclusion

At this point, you learn to install and configure Apache Tomcat on AlmaLinux 8.

Hope you enjoy it.

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!