{"id":185,"date":"2018-06-18T15:25:14","date_gmt":"2018-06-18T18:25:14","guid":{"rendered":"http:\/\/xaxowareti.com.br\/?p=185"},"modified":"2018-06-18T15:25:14","modified_gmt":"2018-06-18T18:25:14","slug":"how-to-install-apache-tomcat-8-on-centos-7","status":"publish","type":"post","link":"https:\/\/xaxowareti.com.br\/?p=185","title":{"rendered":"How To Install Apache Tomcat 8 on CentOS 7"},"content":{"rendered":"<h3 id=\"introduction\">Introduction<\/h3>\n<p>Apache Tomcat is a web server and servlet container that is used to serve Java applications. Tomcat is an open source implementation of the Java Servlet and JavaServer Pages technologies, released by the Apache Software Foundation. This tutorial covers the basic installation and some configuration of the latest release of Tomcat 8 on your CentOS 7 server.<\/p>\n<div data-unique=\"prerequisites\"><\/div>\n<h2 id=\"prerequisites\">Prerequisites<\/h2>\n<p>Before you begin with this guide, you should have a separate, non-root user account set up on your server. You can learn how to do this by completing steps 1-3 in the\u00a0<a href=\"https:\/\/www.digitalocean.com\/community\/articles\/initial-server-setup-with-centos-7\">initial server setup<\/a>\u00a0for CentOS 7. We will be using the\u00a0<code>demo<\/code>\u00a0user created here for the rest of this tutorial.<\/p>\n<div data-unique=\"install-java\"><\/div>\n<h2 id=\"install-java\">Install Java<\/h2>\n<p>Tomcat requires that Java is installed on the server, so any Java web application code can be executed. Let&#8217;s satisfy that requirement by installing OpenJDK 7 with yum.<\/p>\n<p>To install OpenJDK 7 JDK using yum, run this command:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">sudo yum install java-1.7.0-openjdk-devel<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<p>Answer\u00a0<code>y<\/code>\u00a0at the prompt to continue installing OpenJDK 7.<\/p>\n<p>Note that a shortcut to the JAVA_HOME directory, which we will need to configure Tomcat later, can be found at\u00a0<code>\/usr\/lib\/jvm\/jre<\/code>.<\/p>\n<p>Now that Java is installed, let&#8217;s create a\u00a0<code>tomcat<\/code>\u00a0user, which will be used to run the Tomcat service.<\/p>\n<div data-unique=\"create-tomcat-user\"><\/div>\n<h2 id=\"create-tomcat-user\">Create Tomcat User<\/h2>\n<p>For security purposes, Tomcat should be run as an unprivileged user (i.e. not root). We will create a new user and group that will run the Tomcat service.<\/p>\n<p>First, create a new\u00a0<code>tomcat<\/code>\u00a0group:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">sudo groupadd tomcat<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<p>Then create a new\u00a0<code>tomcat<\/code>\u00a0user. We&#8217;ll make this user a member of the\u00a0<code>tomcat<\/code>\u00a0group, with a home directory of\u00a0<code>\/opt\/tomcat<\/code>\u00a0(where we will install Tomcat), and with a shell of\u00a0<code>\/bin\/false<\/code>\u00a0(so nobody can log into the account):<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">sudo useradd -M -s \/bin\/nologin -g tomcat -d \/opt\/tomcat tomcat<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<p>Now that our\u00a0<code>tomcat<\/code>\u00a0user is set up, let&#8217;s download and install Tomcat.<\/p>\n<div data-unique=\"install-tomcat\"><\/div>\n<h2 id=\"install-tomcat\">Install Tomcat<\/h2>\n<p>The easiest way to install Tomcat 8 at this time is to download the latest binary release then configure it manually.<\/p>\n<h3 id=\"download-tomcat-binary\">Download Tomcat Binary<\/h3>\n<p>Find the latest version of Tomcat 8 at the\u00a0<a href=\"http:\/\/tomcat.apache.org\/download-80.cgi\">Tomcat 8 Downloads page<\/a>. At the time of writing, the latest version is\u00a0<strong>8.5.9<\/strong>. Under the\u00a0<strong>Binary Distributions<\/strong>\u00a0section, then under the\u00a0<strong>Core<\/strong>\u00a0list, copy the link to the &#8220;tar.gz&#8221;.<\/p>\n<p>Let&#8217;s download the latest binary distribution to our home directory.<\/p>\n<p>First, change to your home directory:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">cd ~<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<p>Then use\u00a0<code>wget<\/code>\u00a0and paste in the link to download the Tomcat 8 archive, like this (your mirror link will probably differ from the example):<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">wget <span class=\"highlight\">http:\/\/apache.mirrors.ionfish.org\/tomcat\/tomcat-8\/v8.5.9\/bin\/apache-tomcat-8.5.9.tar.gz<\/span><\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<p>We&#8217;re going to install Tomcat to the\u00a0<code>\/opt\/tomcat<\/code>\u00a0directory. Create the directory, then extract the the archive to it with these commands:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">sudo mkdir \/opt\/tomcat<\/li>\n<li class=\"line\">sudo tar xvf apache-tomcat-8*tar.gz -C \/opt\/tomcat &#8211;strip-components=1<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<p>Now we&#8217;re ready to set up the proper user permissions.<\/p>\n<h3 id=\"update-permissions\">Update Permissions<\/h3>\n<p>The\u00a0<code>tomcat<\/code>\u00a0user that we set up needs to have the proper access to the Tomcat installation. We&#8217;ll set that up now.<\/p>\n<p>Change to the Tomcat installation path:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">cd \/opt\/tomcat<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<p>Give the\u00a0<code>tomcat<\/code>\u00a0group ownership over the entire installation directory:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">sudo chgrp -R tomcat \/opt\/tomcat<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<p>Next, give the\u00a0<code>tomcat<\/code>\u00a0group read access to the\u00a0<code>conf<\/code>\u00a0directory and all of its contents, and execute access to the directory itself:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">sudo chmod -R g+r conf<\/li>\n<li class=\"line\">sudo chmod g+x conf<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<p>Then make the\u00a0<code>tomcat<\/code>\u00a0user the owner of the\u00a0<code>webapps<\/code>,\u00a0<code>work<\/code>,\u00a0<code>temp<\/code>, and\u00a0<code>logs<\/code>\u00a0directories:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">sudo chown -R tomcat webapps\/ work\/ temp\/ logs\/<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<p>Now that the proper permissions are set up, let&#8217;s set up a Systemd unit file.<\/p>\n<h3 id=\"install-systemd-unit-file\">Install Systemd Unit File<\/h3>\n<p>Because we want to be able to run Tomcat as a service, we will set up a Tomcat Systemd unit file .<\/p>\n<p>Create and open the unit file by running this command:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">sudo vi \/etc\/systemd\/system\/tomcat.service<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<p>Paste in the following script. You may also want to modify the memory allocation settings that are specified in\u00a0<code>CATALINA_OPTS<\/code>:<\/p>\n<div class=\"code-label \" title=\"\/etc\/systemd\/system\/tomcat.service\">\/etc\/systemd\/system\/tomcat.service<\/div>\n<pre class=\"code-pre \"><code># Systemd unit file for tomcat\r\n[Unit]\r\nDescription=Apache Tomcat Web Application Container\r\nAfter=syslog.target network.target\r\n\r\n[Service]\r\nType=forking\r\n\r\nEnvironment=JAVA_HOME=\/usr\/lib\/jvm\/jre\r\nEnvironment=CATALINA_PID=\/opt\/tomcat\/temp\/tomcat.pid\r\nEnvironment=CATALINA_HOME=\/opt\/tomcat\r\nEnvironment=CATALINA_BASE=\/opt\/tomcat\r\nEnvironment='CATALINA_OPTS=-Xms<span class=\"highlight\">512M<\/span> -Xmx<span class=\"highlight\">1024M<\/span> -server -XX:+UseParallelGC'\r\nEnvironment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:\/dev\/.\/urandom'\r\n\r\nExecStart=\/opt\/tomcat\/bin\/startup.sh\r\nExecStop=\/bin\/kill -15 $MAINPID\r\n\r\nUser=tomcat\r\nGroup=tomcat\r\nUMask=0007\r\nRestartSec=10\r\nRestart=always\r\n\r\n[Install]\r\nWantedBy=multi-user.target\r\n<\/code><\/pre>\n<p>Save and exit. This script tells the server to run the Tomcat service as the\u00a0<code>tomcat<\/code>\u00a0user, with the settings specified.<\/p>\n<p>Now reload Systemd to load the Tomcat unit file:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">sudo systemctl daemon-reload<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<p>Now you can start the Tomcat service with this\u00a0<code>systemctl<\/code>\u00a0command:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">sudo systemctl start tomcat<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<p>Check that the service successfully started by typing:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">sudo systemctl status tomcat<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<p>If you want to enable the Tomcat service, so it starts on server boot, run this command:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">sudo systemctl enable tomcat<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<p>Tomcat is not completely set up yet, but you can access the default splash page by going to your domain or IP address followed by\u00a0<code>:8080<\/code>\u00a0in a web browser:<\/p>\n<pre class=\"code-pre \"><code><\/code><\/pre>\n<div class=\"secondary-code-label \" title=\"Open in web browser:\">Open in web browser:<\/div>\n<pre class=\"code-pre \"><code>http:\/\/<span class=\"highlight\">server_IP_address<\/span>:8080\r\n<\/code><\/pre>\n<p>You will see the default Tomcat splash page, in addition to other information. Now we will go deeper into the installation of Tomcat.<\/p>\n<div data-unique=\"configure-tomcat-web-management-interface\"><\/div>\n<h2 id=\"configure-tomcat-web-management-interface\">Configure Tomcat Web Management Interface<\/h2>\n<p>In order to use the manager webapp that comes with Tomcat, we must add a login to our Tomcat server. We will do this by editing the\u00a0<code>tomcat-users.xml<\/code>\u00a0file:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">sudo vi \/opt\/tomcat\/conf\/tomcat-users.xml<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<p>This file is filled with comments which describe how to configure the file. You may want to delete all the comments between the following two lines, or you may leave them if you want to reference the examples:<\/p>\n<div class=\"code-label \" title=\"tomcat-users.xml excerpt\">tomcat-users.xml excerpt<\/div>\n<pre class=\"code-pre \"><code>&lt;tomcat-users&gt;\r\n...\r\n&lt;\/tomcat-users&gt;\r\n<\/code><\/pre>\n<p>You will want to add a user who can access the\u00a0<code>manager-gui<\/code>\u00a0and\u00a0<code>admin-gui<\/code>\u00a0(webapps that come with Tomcat). You can do so by defining a user similar to the example below. Be sure to change the username and password to something secure:<\/p>\n<div class=\"code-label \" title=\"tomcat-users.xml \u2014 Admin User\">tomcat-users.xml \u2014 Admin User<\/div>\n<pre class=\"code-pre \"><code>&lt;tomcat-users&gt;\r\n    &lt;user username=\"<span class=\"highlight\">admin<\/span>\" password=\"<span class=\"highlight\">password<\/span>\" roles=\"manager-gui,admin-gui\"\/&gt;\r\n&lt;\/tomcat-users&gt;\r\n<\/code><\/pre>\n<p>Save and quit the tomcat-users.xml file.<\/p>\n<p>By default, newer versions of Tomcat restrict access to the Manager and Host Manager apps to connections coming from the server itself. Since we are installing on a remote machine, you will probably want to remove or alter this restriction. To change the IP address restrictions on these, open the appropriate\u00a0<code>context.xml<\/code>\u00a0files.<\/p>\n<p>For the Manager app, type:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">sudo vi \/opt\/tomcat\/webapps\/manager\/META-INF\/context.xml<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<p>For the Host Manager app, type:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">sudo vi \/opt\/tomcat\/webapps\/host-manager\/META-INF\/context.xml<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<p>Inside, comment out the IP address restriction to allow connections from anywhere. Alternatively, if you would like to allow access only to connections coming from your own IP address, you can add your public IP address to the list:<\/p>\n<div class=\"code-label \" title=\"context.xml files for Tomcat webapps\">context.xml files for Tomcat webapps<\/div>\n<pre class=\"code-pre \"><code>&lt;Context antiResourceLocking=\"false\" privileged=\"true\" &gt;\r\n  <span class=\"highlight\">&lt;!--<\/span>&lt;Valve className=\"org.apache.catalina.valves.RemoteAddrValve\"\r\n         allow=\"127\\.\\d+\\.\\d+\\.\\d+|::1|0:0:0:0:0:0:0:1\" \/&gt;<span class=\"highlight\">--&gt;<\/span>\r\n&lt;\/Context&gt;\r\n<\/code><\/pre>\n<p>Save and close the files when you are finished.<\/p>\n<p>To put our changes into effect, restart the Tomcat service:<\/p>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<ul class=\"prefixed\">\n<li class=\"line\">sudo systemctl restart tomcat<\/li>\n<\/ul>\n<pre class=\"code-pre command\"><code><\/code><\/pre>\n<div data-unique=\"access-the-web-interface\"><\/div>\n<h2 id=\"access-the-web-interface\">Access the Web Interface<\/h2>\n<p>Now that Tomcat is up and running, let&#8217;s access the web management interface in a web browser. You can do this by accessing the public IP address of the server, on port 8080:<\/p>\n<pre class=\"code-pre \"><code><\/code><\/pre>\n<div class=\"secondary-code-label \" title=\"Open in web browser:\">Open in web browser:<\/div>\n<pre class=\"code-pre \"><code>http:\/\/<span class=\"highlight\">server_IP_address<\/span>:8080\r\n<\/code><\/pre>\n<p>You will see something like the following image:<\/p>\n<p class=\"growable\"><img decoding=\"async\" src=\"https:\/\/assets.digitalocean.com\/articles\/tomcat8_centos\/splashscreen.png\" alt=\"Tomcat root\" \/><\/p>\n<p>As you can see, there are links to the admin webapps that we configured an admin user for.<\/p>\n<p>Let&#8217;s take a look at the Manager App, accessible via the link or\u00a0<code>http:\/\/<span class=\"highlight\">server_IP_address<\/span>:8080\/manager\/html<\/code>:<\/p>\n<p class=\"growable\"><img decoding=\"async\" src=\"https:\/\/assets.digitalocean.com\/articles\/tomcat8_centos\/manager.png\" alt=\"Tomcat Web Application Manager\" \/><\/p>\n<p>The Web Application Manager is used to manage your Java applications. You can Start, Stop, Reload, Deploy, and Undeploy here. You can also run some diagnostics on your apps (i.e. find memory leaks). Lastly, information about your server is available at the very bottom of this page.<\/p>\n<p>Now let&#8217;s take a look at the Host Manager, accessible via the link or\u00a0<code>http:\/\/<span class=\"highlight\">server_IP_address<\/span>:8080\/host-manager\/html\/<\/code>:<\/p>\n<p class=\"growable\"><img decoding=\"async\" src=\"https:\/\/assets.digitalocean.com\/articles\/tomcat8_centos\/host-manager.png\" alt=\"Tomcat Virtual Host Manager\" \/><\/p>\n<p>From the Virtual Host Manager page, you can add virtual hosts to serve your applications from.<\/p>\n<div data-unique=\"conclusion\"><\/div>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>Your installation of Tomcat is complete! Your are now free to deploy your own Java web applications!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Apache Tomcat is a web server and servlet container that is used to serve Java applications. Tomcat is an open source implementation of the Java Servlet and JavaServer Pages technologies, released by the Apache Software Foundation. This tutorial covers the basic installation and some configuration of the latest release of Tomcat 8 on your [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-185","post","type-post","status-publish","format-standard","hentry","category-dicassolucoes"],"_links":{"self":[{"href":"https:\/\/xaxowareti.com.br\/index.php?rest_route=\/wp\/v2\/posts\/185","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xaxowareti.com.br\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xaxowareti.com.br\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xaxowareti.com.br\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xaxowareti.com.br\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=185"}],"version-history":[{"count":1,"href":"https:\/\/xaxowareti.com.br\/index.php?rest_route=\/wp\/v2\/posts\/185\/revisions"}],"predecessor-version":[{"id":186,"href":"https:\/\/xaxowareti.com.br\/index.php?rest_route=\/wp\/v2\/posts\/185\/revisions\/186"}],"wp:attachment":[{"href":"https:\/\/xaxowareti.com.br\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xaxowareti.com.br\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=185"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xaxowareti.com.br\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}