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:
- Install Java 17.
sudo dnf install java-17-openjdk-devel2. Create tomcat user.
sudo useradd -r -m -U -d /opt/tomcat -s /bin/false tomcat3. Download Tomcat archive.
wget -c https://downloads.apache.org/tomcat/tomcat-9/v9.0.73/bin/apache-tomcat-9.0.73.tar.gz4. Extract the archive to /opt directory.
sudo tar xf apache-tomcat-9.0.34.tar.gz -C /opt/tomcat5. 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/updated6. 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.service9.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.target10. Reload the system daemon.
sudo systemctl daemon-reload11. Start tomcat service.
sudo systemctl start tomcat12. Enable tomcat service.
sudo systemctl enable tomcat13. Open port 8080 within the firewall.
sudo firewall-cmd --zone=public --permanent --add-port=8080/tcp14. Open port 8443 within the firewall.
sudo firewall-cmd --zone=public --permanent --add-port=8443/tcp15. Reload the firewall.
sudo firewall-cmd --reload16. Create Tomcat manager and admin users.
sudo nano /opt/tomcat/conf/tomcat-users.xml16.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.xml17.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.xml18.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.service20. Get the instance IP address.
hostname -i21. 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.