{"id":6207,"date":"2020-06-20T00:40:44","date_gmt":"2020-06-19T21:40:44","guid":{"rendered":"https:\/\/kifarunix.com\/?p=6207"},"modified":"2024-03-14T21:16:15","modified_gmt":"2024-03-14T18:16:15","slug":"install-apache-tomcat-9-on-ubuntu-20-04","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/install-apache-tomcat-9-on-ubuntu-20-04\/","title":{"rendered":"Install Apache Tomcat 9 on Ubuntu 20.04"},"content":{"rendered":"\n<p>In this tutorial, you will learn how to install Apache Tomcat 9 on Ubuntu 20.04.\u00a0<a href=\"https:\/\/tomcat.apache.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">Apache Tomcat<\/a>\u00a0is an opensource java based HTTP web server that implements \u00a0the Java Servlet, JavaServer Pages, Java Expression Language and Java WebSocket technologies.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Installing Apache Tomcat 9 on Ubuntu 20.04<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Run system update<\/h3>\n\n\n\n<p>Ensure that your system package cache is up-to-date;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>apt update<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"installopenjdk14ubuntu20.04\"><a href=\"#installopenjdk14ubuntu20.04\">Install OpenJDK 14 on Ubuntu 20.04<\/a><\/h3>\n\n\n\n<p>Apache Tomcat 9 requires a Java Standard Edition Runtime Environment (JRE) version 8 or later.  Therefore, install OpenJRE as follows;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>apt install openjdk-14-jre<\/code><\/pre>\n\n\n\n<p>Once the installation completes, you can run the command below to verify the version.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>java -version<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>openjdk version \"14.0.1\" 2020-04-14\nOpenJDK Runtime Environment (build 14.0.1+7-Ubuntu-1ubuntu1)\nOpenJDK 64-Bit Server VM (build 14.0.1+7-Ubuntu-1ubuntu1, mixed mode, sharing)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Download Apache Tomcat Binary Distribution<\/h3>\n\n\n\n<p>Navigate to &nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/tomcat.apache.org\/download-90.cgi\" target=\"_blank\">Apache Tomcat 9 downloads page<\/a>&nbsp;and grab Apache Tomcat 9 binary distribution tarball. As of this writing, Apache Tomcat 9.0.36 is the current stable release. <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>wget https:\/\/downloads.apache.org\/tomcat\/tomcat-9\/v9.0.36\/bin\/apache-tomcat-9.0.36.tar.gz<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"apachetomcat9ubuntu20.04\"><a href=\"#apachetomcat9ubuntu20.04\">Installing Apache Tomcat 9<\/a><\/h3>\n\n\n\n<p>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 <code>\/opt\/tomcat9<\/code> directory.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>mkdir \/opt\/tomcat9<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>tar xzf apache-tomcat-9.0.36.tar.gz -C \/opt\/tomcat9 --strip-components=1<\/code><\/pre>\n\n\n\n<p>The command above extracts the contents of apache-tomcat-9.0.36.tar.gz tarball to the installation directory, <code>\/opt\/tomcat9<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>ls \/opt\/tomcat9\/<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code><strong>bin<\/strong> BUILDING.txt <strong>conf<\/strong> CONTRIBUTING.md <strong>lib<\/strong> LICENSE <strong>logs<\/strong> NOTICE README.md RELEASE-NOTES RUNNING.txt <strong>temp webapps work<\/strong><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Set Apache Tomcat 9 Environment Variables<\/h3>\n\n\n\n<p>Various environment variables are used by the Tomcat startup scripts to prepare the command that runs Tomcat daemon.<\/p>\n\n\n\n<p>Set <code>CATALINA_HOME<\/code>&nbsp; environment variable to point to the base path of the Tomcat installation, which in this case is, <code>\/opt\/tomcat9<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>echo 'export CATALINA_HOME=\"\/opt\/tomcat9\"' &gt; \/etc\/profile.d\/tomcat9.sh<\/code><\/pre>\n\n\n\n<p>Depending on the Java package installed, set <strong><code>JRE_HOME<\/code><\/strong> (if you installed <em>JRE<\/em>) or <code><strong>JAVA_HOME<\/strong><\/code> (if you installed <em>JDK<\/em>) environment variable for the Java version you have installed. Since we installed JRE, create the JRE_HOME environment as follows.<\/p>\n\n\n\n<p>Locate the path to the installed Java version using <code>update-java-alternatives<\/code>&nbsp;command.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>update-java-alternatives -l<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>java-1.14.0-openjdk-amd64      1411       \/usr\/lib\/jvm\/java-1.14.0-openjdk-amd64<\/code><\/pre>\n\n\n\n<p>Once you have the path, create the environment variable.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>echo 'export JRE_HOME=\"\/usr\/lib\/jvm\/java-1.14.0-openjdk-amd64\"' &gt;&gt; \/etc\/profile.d\/tomcat9.sh<\/code><\/pre>\n\n\n\n<p>Reload the environment variables set above.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>source \/etc\/profile.d\/tomcat9.sh<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Create Tomcat System User<\/h3>\n\n\n\n<p>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<pre class=\"wp-block-preformatted\"><code>useradd -r -d \/opt\/tomcat9\/ -s \/bin\/false tomcat<\/code><\/pre>\n\n\n\n<p>Next, you need to set the user and group ownership of Tomcat 9 directory to&nbsp;<strong><code>tomcat<\/code><\/strong> user.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>chown -R tomcat: \/opt\/tomcat9\/<\/code><\/pre>\n\n\n\n<p>Assign&nbsp;<strong><code>tomcat<\/code><\/strong> the read and execution permissions on the Tomcat 9 configuration files directory.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>chmod -R u+x,g+rx \/opt\/tomcat9\/<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Configure Tomcat Web Management Accounts<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Create Tomcat Web Management User<\/h4>\n\n\n\n<p>Define a user for the web management of Tomcat 9 Admin\/Manager User interfaces.<\/p>\n\n\n\n<p>This can be done by editing the&nbsp;<strong><code>\/opt\/tomcat9\/conf\/tomcat-users.xml<\/code><\/strong>&nbsp;file and adding the following highlighted lines below between the&nbsp;<strong><code>&lt;\/tomcat-users&gt;<\/code><\/strong>&nbsp;tag.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>vim \/opt\/tomcat9\/conf\/tomcat-users.xml<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>&lt;tomcat-users\n...\n&lt;!--\n  &lt;role rolename=\"tomcat\"\/&gt;\n  &lt;role rolename=\"role1\"\/&gt;\n  &lt;user username=\"tomcat\" password=\"must-be-changed\" roles=\"tomcat\"\/&gt;\n  &lt;user username=\"both\" password=\"must-be-changed\" roles=\"tomcat,role1\"\/&gt;\n  &lt;user username=\"role1\" password=\"must-be-changed\" roles=\"role1\"\/&gt;\n--&gt;\n  <strong>&lt;role rolename=\"admin-gui\"\/&gt;\n  &lt;role rolename=\"manager-gui\"\/&gt;\n  &lt;user username=\"admin\" password=\"StrongP@SS\" roles=\"admin-gui,manager-gui\"\/&gt;<\/strong>\n&lt;\/tomcat-users&gt;\n<\/code><\/pre>\n\n\n\n<p>Replace the password and usernames accordingly.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Configure Tomcat to allow remote connection to Manager and Host Manager apps.<\/h4>\n\n\n\n<p>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,&nbsp;<strong>|<\/strong>.<\/p>\n\n\n\n<p>In this case, <code>192.168.56.1<\/code>, is the IP address of the server to allow access of Tomcat from.<\/p>\n\n\n\n<p>Update for Manager;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>vim \/opt\/tomcat9\/webapps\/manager\/META-INF\/context.xml<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>...\n&lt;Context antiResourceLocking=\"false\" privileged=\"true\" &gt;\n  &lt;Valve className=\"org.apache.catalina.valves.RemoteAddrValve\"\n          allow=\"127\\.\\d+\\.\\d+\\.\\d+|::1|0:0:0:0:0:0:0:1|192.168.56.1\" \/&gt;\n...\n&lt;\/Context&gt;<\/code><\/pre>\n\n\n\n<p>Update for host manager;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>vim \/opt\/tomcat9\/webapps\/host-manager\/META-INF\/context.xml<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>...\n&lt;Context antiResourceLocking=\"false\" privileged=\"true\" &gt;\n  &lt;Valve className=\"org.apache.catalina.valves.RemoteAddrValve\"\n         allow=\"127\\.\\d+\\.\\d+\\.\\d+|::1|0:0:0:0:0:0:0:1|192.168.56.1\" \/&gt;\n...\n&lt;\/Context&gt;<\/code><\/pre>\n\n\n\n<p>Save and exit the configuration file after changes are made.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Allow Tomcat Through Firewall<\/h4>\n\n\n\n<p>To allow external access to Tomcat, you need to open TCP port 8080 on UFW, if it is running;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>ufw allow 8080\/tcp<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Running Tomcat 9 on Ubuntu 20.04<\/h3>\n\n\n\n<p>To run Apache Tomcat in standalone mode, execute the <code>\/opt\/tomcat9\/bin\/startup.sh<\/code>. script.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>\/opt\/tomcat9\/bin\/startup.sh<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Using CATALINA_BASE:   \/opt\/tomcat9\nUsing CATALINA_HOME:   \/opt\/tomcat9\nUsing CATALINA_TMPDIR: \/opt\/tomcat9\/temp\nUsing JRE_HOME:        \/usr\/lib\/jvm\/java-1.14.0-openjdk-amd64\nUsing CLASSPATH:       \/opt\/tomcat9\/bin\/bootstrap.jar:\/opt\/tomcat9\/bin\/tomcat-juli.jar\nTomcat started.<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"accesstomcatonbrowser\"><a href=\"#accesstomcatonbrowser\">Accessing Tomcat 9 Web Interface<\/a><\/h3>\n\n\n\n<p>You can now access your Apache Tomcat9 from the browser using the address, <code><strong>http:\/\/server-hostname-or-ip:8080<\/strong><\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1246\" height=\"992\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2020\/06\/apache-tomcat9-dashboard.png\" alt=\"Install Apache Tomcat 9 on Ubuntu 20.04\" class=\"wp-image-6212\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2020\/06\/apache-tomcat9-dashboard.png?v=1592602454 1246w, https:\/\/kifarunix.com\/wp-content\/uploads\/2020\/06\/apache-tomcat9-dashboard-768x611.png?v=1592602454 768w\" sizes=\"(max-width: 1246px) 100vw, 1246px\" \/><\/figure>\n\n\n\n<p>Click&nbsp;<strong>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<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1904\" height=\"940\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2020\/06\/apache-tomcat-manager-app.png\" alt=\"\" class=\"wp-image-6214\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2020\/06\/apache-tomcat-manager-app.png?v=1592602518 1904w, https:\/\/kifarunix.com\/wp-content\/uploads\/2020\/06\/apache-tomcat-manager-app-768x379.png?v=1592602518 768w, https:\/\/kifarunix.com\/wp-content\/uploads\/2020\/06\/apache-tomcat-manager-app-1536x758.png?v=1592602518 1536w\" sizes=\"(max-width: 1904px) 100vw, 1904px\" \/><\/figure>\n\n\n\n<p>Click&nbsp;<strong>Host Manager<\/strong>, to access Tomcat virtual host manager.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1898\" height=\"881\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2020\/06\/apache-tomcat-host-manager.png\" alt=\"\" class=\"wp-image-6213\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2020\/06\/apache-tomcat-host-manager.png?v=1592602486 1898w, https:\/\/kifarunix.com\/wp-content\/uploads\/2020\/06\/apache-tomcat-host-manager-768x356.png?v=1592602486 768w, https:\/\/kifarunix.com\/wp-content\/uploads\/2020\/06\/apache-tomcat-host-manager-1536x713.png?v=1592602486 1536w\" sizes=\"(max-width: 1898px) 100vw, 1898px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"apachetomcatsystemdserviceunit\"><a href=\"#apachetomcatsystemdserviceunit\">Running Apache Tomcat 9 as a service<\/a><\/h3>\n\n\n\n<p>To be able to run Apache Tomcat9 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<pre class=\"scroll-box\"><code>\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\/tomcat9\/temp\/tomcat.pid\nEnvironment=CATALINA_HOME=\/opt\/tomcat9\nEnvironment=CATALINA_BASE=\/opt\/tomcat9\n\nExecStart=\/opt\/tomcat9\/bin\/catalina.sh start\nExecStop=\/opt\/tomcat9\/bin\/catalina.sh stop\n\nRestartSec=10\nRestart=always\n[Install]\nWantedBy=multi-user.target\nEOL\n<\/code><\/pre>\n\n\n\n<p>Replace the paths accordingly as per your installation. Save and exit the file.<\/p>\n\n\n\n<p>Reload systemd configurations<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>systemctl daemon-reload<\/code><\/pre>\n\n\n\n<p>Stop the initial Tomcat9 started with the script above, <code>\/opt\/tomcat9\/bin\/startup.sh<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>ps aux | grep tomcat | grep -v grep | awk '{print $2}' | xargs -I {} kill -9 {}<\/code><\/pre>\n\n\n\n<p>Reset the ownership of the logs directory;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>chown -R tomcat: \/opt\/tomcat\/logs\/<\/code><\/pre>\n\n\n\n<p>Start and enable Tomca9 systemd service;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>systemctl enable --now tomcat<\/code><\/pre>\n\n\n\n<p>Check the status;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>systemctl status tomcat<\/code><\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>\u25cf tomcat.service - Apache Tomcat Server\n     Loaded: loaded (\/etc\/systemd\/system\/tomcat.service; enabled; vendor preset: enabled)\n     Active: active (running) since Fri 2020-06-19 21:26:19 UTC; 2s ago\n    Process: 6834 ExecStart=\/opt\/tomcat9\/bin\/catalina.sh start (code=exited, status=0\/SUCCESS)\n   Main PID: 6855 (java)\n      Tasks: 37 (limit: 2282)\n     Memory: 88.3M\n     CGroup: \/system.slice\/tomcat.service\n             \u2514\u25006855 \/usr\/bin\/java -Djava.util.logging.config.file=\/opt\/tomcat9\/conf\/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -D&gt;\n\nJun 19 21:26:19 ubuntu20 systemd[1]: Starting Apache Tomcat Server...\nJun 19 21:26:19 ubuntu20 catalina.sh[6834]: Tomcat started.\nJun 19 21:26:19 ubuntu20 systemd[1]: Started Apache Tomcat Server.\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Reference<\/h3>\n\n\n\n<p><a rel=\"noreferrer noopener\" href=\"http:\/\/tomcat.apache.org\/tomcat-9.0-doc\/RUNNING.txt\" target=\"_blank\">Apache Tomcat 9.0 Doc -RUNNING.txt<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Related Tutorials<\/h3>\n\n\n\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/kifarunix.com\/install-apache-tomcat-9-on-debian-10-debian-9\/\" target=\"_blank\">Install Apache Tomcat 9 on Debian 10\/Debian 9<\/a><\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/kifarunix.com\/install-oracle-java-openjdk-13-on-centos-8\/\" target=\"_blank\">Install Oracle Java (OpenJDK) 13 on CentOS 8<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/install-oracle-java-12-on-debian-10\/\" target=\"_blank\" rel=\"noreferrer noopener\">Install Oracle Java 12 on Debian 10<\/a><\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/kifarunix.com\/install-oracle-java-12-on-ubuntu-18-04-debian-9-8\/\" target=\"_blank\">Install Oracle Java 12 on Ubuntu 18.04\/Debian 9.8<\/a><\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" href=\"https:\/\/kifarunix.com\/install-java-11-on-debian-9-8-ubuntu-18-04\/\" target=\"_blank\">Install Java 11 on Debian 9.8\/Ubuntu 18.04<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to install Apache Tomcat 9 on Ubuntu 20.04.\u00a0Apache Tomcat\u00a0is an opensource java based HTTP web server that implements<\/p>\n","protected":false},"author":1,"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":[1706,1708,1705,1704,797,1707,1200],"class_list":["post-6207","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-howtos","category-java","category-tomcat","category-web-servers","tag-apache-tomcat-9","tag-apache-tomcat-9-webserver","tag-install-apache-tomcat-on-ubuntu-20-04","tag-install-tomcat-9-on-ubuntu-20-04","tag-java","tag-openjdk-14-ubuntu-20-04","tag-ubuntu-20-04","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\/6207"}],"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\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/comments?post=6207"}],"version-history":[{"count":12,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/6207\/revisions"}],"predecessor-version":[{"id":21421,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/6207\/revisions\/21421"}],"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=6207"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=6207"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=6207"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}