{"id":9796,"date":"2021-07-26T18:41:01","date_gmt":"2021-07-26T15:41:01","guid":{"rendered":"https:\/\/kifarunix.com\/?p=9796"},"modified":"2024-03-18T19:32:18","modified_gmt":"2024-03-18T16:32:18","slug":"install-apache-tomcat-on-rocky-linux-8","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/install-apache-tomcat-on-rocky-linux-8\/","title":{"rendered":"Install Apache Tomcat on Rocky Linux 8"},"content":{"rendered":"\n

Follow through this tutorial to learn how to install Apache Tomcat on Rocky Linux 8. Apache Tomcat<\/a> is an opensource java based HTTP web server that implements  the Java Servlet, JavaServer Pages, Java Expression Language and Java WebSocket technologies. <\/p>\n\n\n\n

Installing Apache Tomcat on Rocky Linux 8<\/h2>\n\n\n\n

Install OpenJDK on Rocky Linux 8<\/h3>\n\n\n\n

Apache Tomcat requires a Java Standard Edition Runtime Environment (JRE) version 8 or later. Since OpenJDK 11 Runtime Environment is available on the default Rocky Linux repos, we will install it. We install other packages including wget and tar that will be used later on in the guide.<\/p>\n\n\n\n

dnf install java-11-openjdk wget tar<\/code><\/pre>\n\n\n\n

Once the installation completes, you can run the command below to verify the version.<\/p>\n\n\n\n

java -version<\/code><\/pre>\n\n\n\n
openjdk version \"11.0.12\" 2021-07-20 LTS\nOpenJDK Runtime Environment 18.9 (build 11.0.12+7-LTS)\nOpenJDK 64-Bit Server VM 18.9 (build 11.0.12+7-LTS, mixed mode, sharing)<\/code><\/pre>\n\n\n\n

Download Apache Tomcat Binary Distribution<\/h3>\n\n\n\n

Navigate to  Apache Tomcat<\/a> download’s page and grab Apache Tomcat binary distribution tarball. As of this writing, Apache Tomcat 10.0.8 is the current stable release.<\/p>\n\n\n\n

wget https:\/\/downloads.apache.org\/tomcat\/tomcat-10\/v10.0.8\/bin\/apache-tomcat-10.0.8.tar.gz<\/code><\/pre>\n\n\n\n

Install Apache Tomcat<\/h3>\n\n\n\n

Once the download of the binary distribution is complete, installation of Apache Tomcat using the binary distribution is as easy as extracting it to some specific directory, which in this case, we used \/opt\/tomcat<\/code> directory.<\/p>\n\n\n\n

mkdir \/opt\/tomcat<\/code><\/pre>\n\n\n\n
tar xzf apache-tomcat-10.0.8.tar.gz -C \/opt\/tomcat --strip-components=1<\/code><\/pre>\n\n\n\n

The command above extracts the contents of apache-tomcat-9.0.36.tar.gz tarball to the installation directory, \/opt\/tomcat<\/code>.<\/p>\n\n\n\n

ls -1 \/opt\/tomcat\/<\/code><\/pre>\n\n\n\n
bin\nBUILDING.txt\nconf\nCONTRIBUTING.md\nlib\nLICENSE\nlogs\nNOTICE\nREADME.md\nRELEASE-NOTES\nRUNNING.txt\ntemp\nwebapps\nwork\n<\/code><\/pre>\n\n\n\n

Set Apache Tomcat Environment Variables<\/h3>\n\n\n\n

Various environment variables are used by the Tomcat startup scripts to prepare the command that runs Tomcat daemon.<\/p>\n\n\n\n

Set CATALINA_HOME<\/code>  environment variable to point to the base path of the Tomcat installation, which in this case is, \/opt\/tomcat<\/code>.<\/p>\n\n\n\n

echo 'export CATALINA_HOME=\"\/opt\/tomcat\"' > \/etc\/profile.d\/tomcat.sh<\/code><\/pre>\n\n\n\n

Depending on the Java package installed, set JRE_HOME<\/code><\/strong> (if you installed JRE<\/em>) or JAVA_HOME<\/strong><\/code> (if you installed JDK<\/em>) environment variable for the Java version you have installed.<\/p>\n\n\n\n

Since we installed JDK, create the JAVA_HOME environment as follows.<\/p>\n\n\n\n

Locate the path to the installed Java version using alternatives<\/code> command.<\/p>\n\n\n\n

alternatives --list | grep java<\/code><\/pre>\n\n\n\n
java                \tauto  \t\/usr\/lib\/jvm\/java-11-openjdk-11.0.12.0.7-0.el8_4.x86_64<\/strong>\/bin\/java\njre_openjdk         \tauto  \t\/usr\/lib\/jvm\/java-11-openjdk-11.0.12.0.7-0.el8_4.x86_64\njre_11              \tauto  \t\/usr\/lib\/jvm\/java-11-openjdk-11.0.12.0.7-0.el8_4.x86_64\n<\/code><\/pre>\n\n\n\n

From our output above, the path is \/usr\/lib\/jvm\/java-11-openjdk-11.0.12.0.7-0.el8_4.x86_64<\/code><\/strong>.<\/p>\n\n\n\n

Once you have the path, create the environment variable.<\/p>\n\n\n\n

echo 'export JAVA_HOME=\"\/usr\/lib\/jvm\/java-11-openjdk-11.0.12.0.7-0.el8_4.x86_64<\/strong>\"' >> \/etc\/profile.d\/tomcat.sh<\/code><\/pre>\n\n\n\n

Reload the environment variables set above.<\/p>\n\n\n\n

source \/etc\/profile.d\/tomcat.sh<\/code><\/pre>\n\n\n\n

Create Apache Tomcat System User<\/h3>\n\n\n\n

Just like any other web server, Apache Tomcat should not be run with a privileged user. Hence, create a system user for Apache Tomcat as follows;<\/p>\n\n\n\n

useradd -r -d \/opt\/tomcat\/ -s \/bin\/false -c \"Apach Tomcat User\" tomcat<\/code><\/pre>\n\n\n\n

Next, you need to set the user and group ownership of Tomcat directory to tomcat<\/code><\/strong> user.<\/p>\n\n\n\n

chown -R tomcat: \/opt\/tomcat\/<\/code><\/pre>\n\n\n\n

Configure Tomcat Web Management Accounts<\/h3>\n\n\n\n

Create Tomcat Web Management User<\/h4>\n\n\n\n

Define a user for the web management of Tomcat Admin\/Manager User interfaces.<\/p>\n\n\n\n

This can be done by editing the \/opt\/tomcat\/conf\/tomcat-users.xml<\/code><\/strong> file and adding the following highlighted lines below between the <\/tomcat-users><\/code><\/strong> tag.<\/p>\n\n\n\n

vim \/opt\/tomcat\/conf\/tomcat-users.xml<\/code><\/pre>\n\n\n\n
<tomcat-users\n...\n<!--\n  <role rolename=\"tomcat\"\/>\n  <role rolename=\"role1\"\/>\n  <user username=\"tomcat\" password=\"must-be-changed\" roles=\"tomcat\"\/>\n  <user username=\"both\" password=\"must-be-changed\" roles=\"tomcat,role1\"\/>\n  <user username=\"role1\" password=\"must-be-changed\" roles=\"role1\"\/>\n-->\n  <role rolename=\"admin-gui\"\/>\n  <role rolename=\"manager-gui\"\/>\n  <user username=\"admin\" password=\"StrongP@SS\" roles=\"admin-gui,manager-gui\"\/><\/strong>\n<\/tomcat-users>\n<\/code><\/pre>\n\n\n\n

Replace the password and usernames accordingly.<\/p>\n\n\n\n

Configure Tomcat to allow remote connection to Manager and Host Manager apps.<\/h4>\n\n\n\n

Edit the configuration files below for Manager and Host Manager respectively and enter the IP addresses of the remote server you are accessing the Tomcat from. The IPs are separated by a pipe, |<\/strong>.<\/p>\n\n\n\n

In this case, 192.168.60.1<\/code>, is the IP address of the server to allow access of Tomcat from.<\/p>\n\n\n\n

Update for Manager;<\/p>\n\n\n\n

vim \/opt\/tomcat\/webapps\/manager\/META-INF\/context.xml<\/code><\/pre>\n\n\n\n
...\n<Context antiResourceLocking=\"false\" privileged=\"true\" >\n  <Valve className=\"org.apache.catalina.valves.RemoteAddrValve\"\n          allow=\"127\\.\\d+\\.\\d+\\.\\d+|::1|0:0:0:0:0:0:0:1|192.168.60.1\" \/>\n...\n<\/Context><\/code><\/pre>\n\n\n\n

Update for Host Manager;<\/p>\n\n\n\n

vim \/opt\/tomcat\/webapps\/host-manager\/META-INF\/context.xml<\/code><\/pre>\n\n\n\n
...\n<Context antiResourceLocking=\"false\" privileged=\"true\" >\n  <Valve className=\"org.apache.catalina.valves.RemoteAddrValve\"\n         allow=\"127\\.\\d+\\.\\d+\\.\\d+|::1|0:0:0:0:0:0:0:1|192.168.60.1\" \/>\n...\n<\/Context><\/code><\/pre>\n\n\n\n

Save and exit the configuration file after changes are made.<\/p>\n\n\n\n

Allow Tomcat Through Firewall<\/h4>\n\n\n\n

To allow external access to Tomcat, you need to open TCP port 8080 on Firewalld, if it is running;<\/p>\n\n\n\n

firewall-cmd --add-port=8080\/tcp --permanent\nfirewall-cmd --reload<\/code><\/pre>\n\n\n\n

Running Tomcat on Rocky Linux 8<\/h3>\n\n\n\n

To run Apache Tomcat in standalone mode, execute the \/opt\/tomcat\/bin\/startup.sh<\/code>. script.<\/p>\n\n\n\n

\/opt\/tomcat\/bin\/startup.sh<\/code><\/pre>\n\n\n\n

Sample output;<\/p>\n\n\n\n

Using CATALINA_BASE:   \/opt\/tomcat\nUsing CATALINA_HOME:   \/opt\/tomcat\nUsing CATALINA_TMPDIR: \/opt\/tomcat\/temp\nUsing JRE_HOME:        \/usr\/lib\/jvm\/java-11-openjdk-11.0.12.0.7-0.el8_4.x86_64\nUsing CLASSPATH:       \/opt\/tomcat\/bin\/bootstrap.jar:\/opt\/tomcat\/bin\/tomcat-juli.jar\nUsing CATALINA_OPTS:   \nTomcat started.\n<\/code><\/pre>\n\n\n\n

Check catalina.out log file or any other log file under \/opt\/tomcat\/logs\/*<\/strong><\/code>.<\/p>\n\n\n\n

tail \/opt\/tomcat\/logs\/catalina.out<\/p>\n\n\n\n

26-Jul-2021 18:16:57.225 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [\/opt\/tomcat\/webapps\/docs]\n26-Jul-2021 18:16:57.259 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [\/opt\/tomcat\/webapps\/docs] has finished in [34] ms\n26-Jul-2021 18:16:57.259 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [\/opt\/tomcat\/webapps\/examples]\n26-Jul-2021 18:16:57.992 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [\/opt\/tomcat\/webapps\/examples] has finished in [733] ms\n26-Jul-2021 18:16:57.993 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [\/opt\/tomcat\/webapps\/host-manager]\n26-Jul-2021 18:16:58.042 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [\/opt\/tomcat\/webapps\/host-manager] has finished in [49] ms\n26-Jul-2021 18:16:58.043 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [\/opt\/tomcat\/webapps\/manager]\n26-Jul-2021 18:16:58.063 INFO [main] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [\/opt\/tomcat\/webapps\/manager] has finished in [21] ms\n26-Jul-2021 18:16:58.084 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler [\"http-nio-8080\"]\n26-Jul-2021 18:16:58.129 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in [1865] milliseconds\n<\/code><\/pre>\n\n\n\n

Accessing Tomcat Web Interface<\/h3>\n\n\n\n

You can now access your Apache tomcat from the browser using the address, http:\/\/server-hostname-or-ip:8080<\/strong><\/code>.<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n

Click Manager App<\/strong> to access the Tomcat Web Application Manager. When prompted for credentials, use the ones you set above;<\/p>\n\n\n\n

\"Install<\/figure>\n\n\n\n

Click Host Manager<\/strong>, to access Tomcat virtual host manager.<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n

Running Apache Tomcat as a service<\/h3>\n\n\n\n

To be able to run Apache tomcat as a service and ensure it runs on system reboots, you need to create a systemd service unit file it as follows.<\/p>\n\n\n\n

\ncat > \/etc\/systemd\/system\/tomcat.service << 'EOL'\n[Unit]\nDescription=Apache Tomcat Server\nAfter=syslog.target network.target\n\n[Service]\nType=forking\nUser=tomcat\nGroup=tomcat\n\nEnvironment=CATALINA_PID=\/opt\/tomcat\/temp\/tomcat.pid\nEnvironment=CATALINA_HOME=\/opt\/tomcat\nEnvironment=CATALINA_BASE=\/opt\/tomcat\n\nExecStart=\/opt\/tomcat\/bin\/catalina.sh start\nExecStop=\/opt\/tomcat\/bin\/catalina.sh stop\n\nRestartSec=10\nRestart=always\n[Install]\nWantedBy=multi-user.target\nEOL\n<\/code><\/pre>\n\n\n\n

Replace the paths accordingly as per your installation. Save and exit the file.<\/p>\n\n\n\n

Reload systemd configurations<\/p>\n\n\n\n

systemctl daemon-reload<\/code><\/pre>\n\n\n\n

Stop the initial tomcat started with the script above, \/opt\/tomcat\/bin\/startup.sh<\/code>.<\/p>\n\n\n\n

ps aux | grep tomcat | grep -v grep | awk '{print $2}' | xargs -I {} kill -9 {}<\/code><\/pre>\n\n\n\n

Reset the ownership of the logs directory;<\/p>\n\n\n\n

chown -R tomcat: \/opt\/tomcat\/logs\/<\/code><\/pre>\n\n\n\n

Start and enable Tomcat systemd service;<\/p>\n\n\n\n

systemctl enable --now tomcat<\/code><\/pre>\n\n\n\n

Check the status;<\/p>\n\n\n\n

systemctl status tomcat<\/code><\/pre>\n\n\n\n
\u25cf tomcat.service - Apache Tomcat Server\n   Loaded: loaded (\/etc\/systemd\/system\/tomcat.service; enabled; vendor preset: disabled)\n   Active: active (running) since Mon 2021-07-26 18:37:15 EAT; 1s ago\n  Process: 4812 ExecStart=\/opt\/tomcat\/bin\/catalina.sh start (code=exited, status=0\/SUCCESS)\n Main PID: 4823 (java)\n    Tasks: 14 (limit: 4938)\n   Memory: 54.6M\n   CGroup: \/system.slice\/tomcat.service\n           \u2514\u25004823 \/usr\/bin\/java -Djava.util.logging.config.file=\/opt\/tomcat\/conf\/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk>\n\nJul 26 18:37:15 rocky8.kifarunix-demo.com systemd[1]: Starting Apache Tomcat Server...\nJul 26 18:37:15 rocky8.kifarunix-demo.com systemd[1]: Started Apache Tomcat Server.\n\n<\/code><\/pre>\n\n\n\n

And that marks the end of our guide on how to install Apache Tomcat.<\/p>\n\n\n\n

Reference<\/h3>\n\n\n\n

Apache Tomcat.10 Doc -RUNNING.txt<\/a><\/p>\n\n\n\n

Related Tutorials<\/h3>\n\n\n\n

Install Apache Guacamole on Rocky Linux 8<\/a><\/p>\n\n\n\n

Install Apache Tomcat 9 on Ubuntu 20.04<\/a><\/p>\n\n\n\n

Install Apache Tomcat 9 on Debian 10\/Debian 9<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"

Follow through this tutorial to learn how to install Apache Tomcat on Rocky Linux 8. Apache Tomcat is an opensource java based HTTP web server that<\/p>\n","protected":false},"author":3,"featured_media":9831,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[121,795,334,253],"tags":[1012,3894,3897,3895,3587,3896,3898],"class_list":["post-9796","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-howtos","category-java","category-tomcat","category-web-servers","tag-apache-tomcat","tag-install-apache-tomcat-on-rocky-linux-8","tag-install-tomcat-10-rocky-linux","tag-install-tomcat-on-rocky-linux-8","tag-rocky-linux-8","tag-rocky-linux-8-tomcat-10","tag-tomcat-10","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50","resize-featured-image"],"_links":{"self":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/9796"}],"collection":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/comments?post=9796"}],"version-history":[{"count":13,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/9796\/revisions"}],"predecessor-version":[{"id":21716,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/9796\/revisions\/21716"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media\/9831"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=9796"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=9796"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=9796"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}