How to install Tomcat 9 on Linux RHEL

Hey everyone!

Hope you are safe and doing great!

In this short guide, we will go through the setup from the scratch, using a Linux RHEL machine, and will install Tomcat 9 into /opt directory.

Installing Tomcat 9

Log in to your Linux machine and do the commands below:

  1. Install Java 17.
sudo dnf install java-17-openjdk-devel

2. Create tomcat user.

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

3. Download Tomcat archive.

wget -c https://downloads.apache.org/tomcat/tomcat-9/v9.0.73/bin/apache-tomcat-9.0.73.tar.gz

4. Extract the archive to /opt directory.

sudo tar xf apache-tomcat-9.0.34.tar.gz -C /opt/tomcat

5. Create the symbolic link for the future Tomcat upgrades (each update the link should be changed).

sudo ln -s /opt/tomcat/apache-tomcat-9.0.34 /opt/tomcat/updated

6. Grant tomcat user rights to the directory.

sudo chown -R tomcat: /opt/tomcat/

7. Make scripts executable in the directory.

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

8. Get the path to JAVA_HOME

dirname $(dirname $(readlink -f $(which javac)))

9. Create tomcat service.

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

9.1. Put the service configuration. Replace path after JAVA_HOME within your path.

[Unit]
Description=Apache Tomcat Web Application Container
After=network.target

[Service]
Type=forking

Environment="JAVA_HOME=/usr/lib/jvm/java-17-openjdk-17.0.5.0.8-2.el8_6.x86_64"
Environment="CATALINA_PID=/opt/tomcat/updated/temp/tomcat.pid"
Environment="CATALINA_HOME=/opt/tomcat/updated/"
Environment="CATALINA_BASE=/opt/tomcat/updated/"
Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"
Environment="JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom"

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

User=tomcat
Group=tomcat
UMask=0007
RestartSec=10
Restart=always

[Install]
WantedBy=multi-user.target

10. Reload the system daemon.

sudo systemctl daemon-reload

11. Start tomcat service.

sudo systemctl start tomcat

12. Enable tomcat service.

sudo systemctl enable tomcat

13. Open port 8080 within the firewall.

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

14. Open port 8443 within the firewall.

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

15. Reload the firewall.

sudo firewall-cmd --reload

16. Create Tomcat manager and admin users.

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

16.1.  Add lines before the ending tag. Replace user_password with a new password.

<role rolename="admin-gui,manager-gui"/>
<user username="admin" password="user_password" roles="admin-gui,manager-gui"/>

17. Remove the restriction to use manager role from remote machines.

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

17.1. Add |.* after the last address range before the ending tag.

allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|.*" />

18. Remove the restriction to use host-manager role from remote machines.

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

18.1. Add |.* after the last address range before the ending tag.

allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1|.*" />

19. Restart tomcat service.

sudo systemctl restart tomcat.service

20. Get the instance IP address.

hostname -i

21. Open the link via a browser on your machine.

You are awesome!

That is it. Hope this short guide helped you and saved your time for the best.

Thank you for reading and see you soon.