{"id":720,"date":"2018-03-28T10:01:02","date_gmt":"2018-03-28T02:01:02","guid":{"rendered":"https:\/\/blog.jsjs.org\/?p=720"},"modified":"2018-03-28T10:01:02","modified_gmt":"2018-03-28T02:01:02","slug":"how-to-install-apache-tomcat-8-on-centos-7","status":"publish","type":"post","link":"https:\/\/blog.jsjs.org\/?p=720","title":{"rendered":"How to Install Apache Tomcat 8 on CentOS 7"},"content":{"rendered":"<p>Apache Tomcat is an open-source web server that is designed to serve Java web pages. It is widely deployed and powers various mission-critical web applications around the world.<\/p>\n<p>As a starter guide, this article explains how to install Apache Tomcat 8, the latest stable version of Apache Tomcat, onto a Vultr CentOS 7 server instance.<\/p>\n<h3>Prerequisites<\/h3>\n<p>Before further reading, you need to:<\/p>\n<ul>\n<li>Deploy a fresh Vultr CentOS 7 server instance.<\/li>\n<li>Log into this machine from your SSH terminal as a non-root sudo user.<\/li>\n<\/ul>\n<h3>Step 1: Update your CentOS system<\/h3>\n<p>First things first, you need to update the system to the latest stable status:<\/p>\n<pre><code>sudo yum install epel-release\nsudo yum update -y &amp;&amp; sudo reboot\n<\/code><\/pre>\n<p>Use the same sudo user to log into the system after the reboot finishes.<\/p>\n<h3>Step 2: Install Java<\/h3>\n<p>You need to install Java SE 7.0 or later before Apache Tomcat can run properly. Here, I will install OpenJDK Runtime Environment 1.8.0 using YUM:<\/p>\n<pre><code>sudo yum install java-1.8.0-openjdk.x86_64\n<\/code><\/pre>\n<p>Now, you can confirm your installation with:<\/p>\n<pre><code>java -version\n<\/code><\/pre>\n<p>The output will resemble the following:<\/p>\n<pre><code>openjdk version \"1.8.0_91\"\nOpenJDK Runtime Environment (build 1.8.0_91-b14)\nOpenJDK 64-Bit Server VM (build 25.91-b14, mixed mode)\n<\/code><\/pre>\n<h3>Step 3: Create a dedicated user for Apache Tomcat<\/h3>\n<p>For security purposes, you need to create a dedicated non-root user &#8220;tomcat&#8221; who belongs to the &#8220;tomcat&#8221; group:<\/p>\n<pre><code>sudo groupadd tomcat\nsudo mkdir \/opt\/tomcat\nsudo useradd -s \/bin\/nologin -g tomcat -d \/opt\/tomcat tomcat\n<\/code><\/pre>\n<p>In this fashion, you created a user &#8220;tomcat&#8221; who belongs to the group &#8220;tomcat&#8221;. You cannot use this user account to log into the system. The home directory is\u00a0<code>\/opt\/tomcat<\/code>, which is where the Apache Tomcat program will reside.<\/p>\n<h3>Step 4: Download and install the latest Apache Tomcat<\/h3>\n<p>You can always find the latest stable version of Apache Tomcat 8 from its\u00a0<a href=\"http:\/\/tomcat.apache.org\/tomcat-8.0-doc\/index.html\">official download page<\/a>, which is 8.0.33 as of writing.<\/p>\n<p>Under the &#8220;Binary Distributions&#8221; section and then the &#8220;Core&#8221; list, use the link pointing to the &#8220;tar.gz&#8221; archive to compose a wget command:<\/p>\n<pre><code>cd ~\nwget http:\/\/www-us.apache.org\/dist\/tomcat\/tomcat-8\/v8.0.33\/bin\/apache-tomcat-8.0.33.tar.gz\nsudo tar -zxvf apache-tomcat-8.0.33.tar.gz -C \/opt\/tomcat --strip-components=1\n<\/code><\/pre>\n<h3>Step 5: Setup proper permissions<\/h3>\n<p>Before you can run Apache Tomcat, you need to setup proper permissions for several directories:<\/p>\n<pre><code>cd \/opt\/tomcat\nsudo chgrp -R tomcat conf\nsudo chmod g+rwx conf\nsudo chmod g+r conf\/*\nsudo chown -R tomcat logs\/ temp\/ webapps\/ work\/\n\nsudo chgrp -R tomcat bin\nsudo chgrp -R tomcat lib\nsudo chmod g+rwx bin\nsudo chmod g+r bin\/*\n<\/code><\/pre>\n<h3>Step 6: Setup a Systemd unit file for Apache Tomcat<\/h3>\n<p>As a matter of convenience, you should setup a Systemd unit file for Apache Tomcat:<\/p>\n<pre><code>sudo vi \/etc\/systemd\/system\/tomcat.service\n<\/code><\/pre>\n<p>Populate the file with:<\/p>\n<pre><code>[Unit]\nDescription=Apache Tomcat Web Application Container\nAfter=syslog.target network.target\n\n[Service]\nType=forking\n\nEnvironment=JAVA_HOME=\/usr\/lib\/jvm\/jre\nEnvironment=CATALINA_PID=\/opt\/tomcat\/temp\/tomcat.pid\nEnvironment=CATALINA_HOME=\/opt\/tomcat\nEnvironment=CATALINA_BASE=\/opt\/tomcat\nEnvironment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'\nEnvironment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:\/dev\/.\/urandom'\n\nExecStart=\/opt\/tomcat\/bin\/startup.sh\nExecStop=\/bin\/kill -15 $MAINPID\n\nUser=tomcat\nGroup=tomcat\n\n[Install]\nWantedBy=multi-user.target\n<\/code><\/pre>\n<p>Save and quit:<\/p>\n<pre><code>:wq\n<\/code><\/pre>\n<h3>Step 7: Install haveged, a security-related program<\/h3>\n<p>For security purposes, you should install haveged as well:<\/p>\n<pre><code>sudo yum install haveged\nsudo systemctl start haveged.service\nsudo systemctl enable haveged.service\n<\/code><\/pre>\n<h3>Step 8: Start and test Apache Tomcat<\/h3>\n<p>Now, start the Apache Tomcat service and set it run on system boot:<\/p>\n<pre><code>sudo systemctl start tomcat.service\nsudo systemctl enable tomcat.service\n<\/code><\/pre>\n<p>In order to test Apache Tomcat in a web browser, you need to modify the firewall rules:<\/p>\n<pre><code>sudo firewall-cmd --zone=public --permanent --add-port=8080\/tcp\nsudo firewall-cmd --reload\n<\/code><\/pre>\n<p>Then, you can test your installation of Apache Tomcat by visiting the following URL from a web browser:<\/p>\n<pre><code>http:\/\/[your-Vultr-server-IP]:8080\n<\/code><\/pre>\n<p>If nothing goes wrong, you will see the default Apache Tomcat front page.<\/p>\n<h3>Step 9: Configure the Apache Tomcat web management interface<\/h3>\n<p>In order to use the &#8220;Manager App&#8221; and the &#8220;Host manager&#8221; in the Apache Tomcat web interface, you need to create an admin user for your Apache Tomcat server:<\/p>\n<pre><code>sudo vi \/opt\/tomcat\/conf\/tomcat-users.xml\n<\/code><\/pre>\n<p>Within the\u00a0<code>&lt;\/tomcat-users ...&gt;...&lt;\/tomcat-users&gt;<\/code>\u00a0segment, insert a line to define a admin user:<\/p>\n<pre><code>&lt;user username=\"yourusername\" password=\"yourpassword\" roles=\"manager-gui,admin-gui\"\/&gt;\n<\/code><\/pre>\n<p>Remember to replace &#8220;yourusername&#8221; and &#8220;yourpassword&#8221; with your own ones, the less common the better.<\/p>\n<p>Save and quit:<\/p>\n<pre><code>:wq\n<\/code><\/pre>\n<p>Restart Apache Tomcat to put your modifications into effect:<\/p>\n<pre><code>sudo systemctl restart tomcat.service\n<\/code><\/pre>\n<p>Refresh the Apache Tomcat front page from your web browser. Log in the &#8220;Manager App&#8221; and the &#8220;Host manager&#8221; using the credentials you had setup earlier.<\/p>\n<p>The Apache Tomcat setup is complete. You can now use it to deploy your own applications.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Apache Tomcat is an open-source web server that is designed to serve Java web pages. It is widely deployed and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[1],"tags":[],"class_list":["post-720","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/posts\/720","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=720"}],"version-history":[{"count":0,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=\/wp\/v2\/posts\/720\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=720"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=720"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jsjs.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=720"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}