How to Install Apache Tomcat on Rocky Linux 8

Install Apache Tomcat on Rocky Linux 8

In this article, we will have explained the necessary steps to install Apache Tomcat on Rocky Linux 8. Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges. All the commands in this tutorial should be run as a non-root user.

Apache Tomcat is an open-source web server used for deploying Java-based web applications. Java servlets are small programs defining how a server handles requests and responses. Tomcat acts as an open-source implementation of the Java Servlet, JavaServer Pages, Java Expression Language, and Java WebSocket technologies.

Install Apache Tomcat on Rocky Linux 8

Step 1. First, before you start installing any package on your Rocky Linux server, we always recommend making sure that all system packages are updated.

sudo dnf install epel-release
sudo dnf update
sudo dnf upgrade

Step 2. Install Java on Rocky Linux.

Run the following command below to install OpenJDK:

sudo dnf install java-11-openjdk-devel

Once the installation completes, you can run the command below to verify the version:

java -version

Step 3. Create Tomcat User and Group.

We create a new system user to minimize any security risk by running Tomcat as a root user:

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

Step 4. Install Apache Tomcat on Rocky Linux.

Installing Tomcat on your Rocky Linux system is straightforward, All you need to do is open a terminal and run the following command:

wget https://downloads.apache.org/tomcat/tomcat-10/v10.0.5/bin/apache-tomcat-10.0.5.tar.gz

Next, extract the file to the /opt/tomcat directory:

sudo tar -xf apache-tomcat-${VERSION}.tar.gz -C /opt/tomcat/

Then, create a symbolic link to the latest version of Tomcat that points to Tomcat’s installation directory:

sudo ln -s /opt/tomcat/apache-tomcat-${VERSION} /opt/tomcat/latest

Change the ownership of the directory to the tomcat user:

sudo chown -R tomcat:tomcat /opt/tomcat

Step 5. Create Systemd Unit File.

By default, we won’t have a Systemd unit file for Tomcat like the Apache server to stop, start and enable its services. Now we create a service file for the Tomcat server so that it can be started automatically:

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

Add the following file:

[Unit]
Description=Apache Tomcat 10 Servlet container
Wants=network.target
After=network.target

[Service]
Type=forking

User=tomcat
Group=tomcat

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

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

ExecStart=/opt/tomcat/latest/bin/startup.sh
ExecStop=/opt/tomcat/latest/bin/shutdown.sh
Restart=always

[Install]
WantedBy=multi-user.target

Save and close the file when you are finished. After successfully creating systemd unit file for tomcat, start its service using the below-given commands:

sudo systemctl daemon-reload
sudo systemctl enable tomcat --now

Step 6. Configure Firewall.

To allow external access to Tomcat, you need to open TCP port 8080 on Firewalld, if it is running:

sudo firewall-cmd --zone=public --permanent --add-port=8080/tcp
sudo firewall-cmd --reload

Step 7. Access Apache Tomcat Web UI.

Now access Tomcat using the URL http://your-server-ip-address:8080. You should see the following screen:

Install Apache Tomcat on Rocky Linux 8

That’s all you need to do to install Tomcat on Rocky Linux 8. I hope you find this quick tip helpful. For further reading on Tomcat, please refer to their official knowledge base. If you have questions or suggestions, feel free to leave a comment below.