{"id":944,"date":"2018-01-25T06:17:12","date_gmt":"2018-01-25T06:17:12","guid":{"rendered":"https:\/\/www.newdelhihosting.co.in\/blog\/?p=944"},"modified":"2024-10-22T10:53:12","modified_gmt":"2024-10-22T10:53:12","slug":"apache-tomcat-8-5-23-installation-on-centos","status":"publish","type":"post","link":"https:\/\/www.newdelhihosting.co.in\/blog\/apache-tomcat-8-5-23-installation-on-centos\/","title":{"rendered":"Apache Tomcat 8.5.23 Installation on CentOS"},"content":{"rendered":"<p><strong>Tomcat:-<\/strong><\/p>\n<p style=\"text-align: left;\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Tomcat is an open source Java Servlet implementation developed by the <strong>Apache Software Foundation.<\/strong> Beside Java Servlets, Tomcat implements several Java server technologies including JavaServer Pages (JSP), Java Expression Language, and Java WebSocket. Tomcat provides an HTTP Web Server for Java applications with support for HTTP\/2, OpenSSL for JSSE and TLS virtual hosting.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Step1-Install Java<\/strong><\/p>\n<p>As a first thing, we need to install Java\u00a0JRE and JDK from the CentOS repository. We will install Java 1.8.13 on the server.<\/p>\n<pre><em># yum -y install java-1.8.0-openjdk.x86_64 java-1.8.0-openjdk-devel.x86_64\r\n<\/em><\/pre>\n<p>It will take some time, wait until the installation finished. After completing the installation check the Java version.<\/p>\n<p># java -version<\/p>\n<pre><em>[root@newdelhihosting ~]# java -version\r\nopenjdk version \"1.8.0_151\"\r\nOpenJDK Runtime Environment (build 1.8.0_151-b12)\r\nOpenJDK 64-Bit Server VM (build 25.151-b12, mixed mode)\r\n[root@newdelhihosting ~]#\r\n<\/em><\/pre>\n<p><strong>Step2-Configuring the Java Environment<\/strong><br \/>\nIn the first step, we have installed Java, and now we need to configure the JAVA_HOME environment variable on the CentOS server so that Java applications can find the right Java version and also the Tomcat requires the JAVA_HOME environment to be setup properly.<\/p>\n<p>Before, configuring the JAVA_HOME environment, we need to know where the Java directory is.<\/p>\n<p>To check the Java directory<\/p>\n<pre><em># sudo update-alternatives --config java<\/em><\/pre>\n<p>Java Directory=java-1.8.0-openjdk.x86_64 (\/usr\/lib\/jvm\/java-1.8.0-openjdk-1.8.0.151-1.b12.el7_4.x86_64\/jre\/bin\/java)<\/p>\n<p>Now, we can proceed with editing the environment file.<\/p>\n<pre><em>JAVA_HOME=\"\/usr\/lib\/jvm\/java-1.8.0-openjdk-1.8.0.151-1.b12.el7_4.x86_64\/jre\"\r\n<\/em><\/pre>\n<p>Save the file and exit.<\/p>\n<p>Next, edit the .bash_profile file and add the JAVA_HOME variable as well<\/p>\n<pre><em>export JAVA_HOME=\/usr\/lib\/jvm\/java-1.8.0-openjdk-1.8.0.151-1.b12.el7_4.x86_64\/jre\r\nexport PATH=$JAVA_HOME\/bin:$PATH\r\n<\/em><\/pre>\n<p>Save the file and exit.<\/p>\n<p>Now, we need to reload the bash profile file.<\/p>\n<pre><em># source ~\/.bash_profile<\/em><\/pre>\n<p>Make sure there is no error while reloading the bash file, Finally check the JAVA_HOME environment variable.<\/p>\n<pre><em># echo $JAVA_HOME<\/em><\/pre>\n<pre><em>[root@newdelhihosting ~]# echo $JAVA_HOME\r\n\/usr\/lib\/jvm\/java-1.8.0-openjdk-1.8.0.151-1.b12.el7_4.x86_64\/jre\r\n[root@newdelhihosting ~]#<\/em><\/pre>\n<p><strong>Step3-Install Apache Tomcat 8.5.23<\/strong><\/p>\n<p>As a important thing we to know here is, installing Apache Tomcat under the user tomcat.<\/p>\n<p>Let we create a user and group named tomcat.<\/p>\n<p><em># groupadd tomcat<\/em><br \/>\n<em> # useradd -s \/bin\/false -g tomcat -d \/opt\/tomcat tomcat<\/em><br \/>\nNote that,<\/p>\n<p>-s \/bin\/false \u2013 disable shell access<br \/>\n-g tomcat \u2013 assign new user to the group tomcat<br \/>\n-d \/opt\/tomcat \u2013 define the home directory for the user<br \/>\nNow, go to the \/opt directory and download tomcat.<\/p>\n<pre><em>#cd \/opt\/<\/em><\/pre>\n<p># wget http:\/\/www-us.apache.org\/dist\/tomcat\/tomcat-8\/v8.5.23\/bin\/apache-tomcat-8.5.23.tar.gz<br \/>\nExtract Tomcat and move all the files and directories that are in the \u2018apache-tomcat-8.5.6\u2019 directory to the \u2018tomcat\u2019 directory.<\/p>\n<pre><em># tar -xzvf apache-tomcat-8.5.23.tar.gz\r\n# mv apache-tomcat-8.5.23\/* tomcat\/<\/em><\/pre>\n<p>Next, change the owner of the tomcat directory to the \u201ctomcat\u201d user and group.<\/p>\n<pre><em># chown -hR tomcat:tomcat tomcat<\/em><\/pre>\n<p>Finally you have installed Apache Tomcat.<\/p>\n<p><strong>Step4-Test Apache Tomcat<\/strong><br \/>\nNow, we just want to run a short test to make sure there are no errors in our installation.<\/p>\n<p>Go to, \/opt\/tomcat\/bin\/ directory and run the command \u2018startup.sh\u2019 to test Apache Tomcat.<\/p>\n<pre><em># cd \/opt\/tomcat\/bin\/\r\n# .\/startup.sh<\/em><\/pre>\n<p>you will get the out put as follows:-<\/p>\n<pre><em>[root@newdelhihosting bin]# .\/startup.sh\r\nUsing CATALINA_BASE:   \/opt\/tomcat\r\nUsing CATALINA_HOME:   \/opt\/tomcat\r\nUsing CATALINA_TMPDIR: \/opt\/tomcat\/temp\r\nUsing JRE_HOME:        \/usr\/lib\/jvm\/java-1.8.0-openjdk-1.8.0.151-1.b12.el7_4.x86_64\/jre\r\nUsing CLASSPATH:       \/opt\/tomcat\/bin\/bootstrap.jar:\/opt\/tomcat\/bin\/tomcat-juli.jar\r\nTomcat started.\r\n[root@newdelhihosting bin]#<\/em><\/pre>\n<p>Tomcat is using port 8080 now, check the open port on the server with the netstat command.<\/p>\n<pre><em># netstat -plntu<\/em><\/pre>\n<p>Note : If netstat command is not working, you have to install net-tools using,<\/p>\n<pre><em># yum install net-tools<\/em><\/pre>\n<p>You will get the output for netstat -plntu as<\/p>\n<pre><em>[root@newdelhihosting bin]# netstat -plntu\r\nActive Internet connections (only servers)\r\nProto Recv-Q Send-Q Local Address           Foreign Address         State       PID\/Program name\r\ntcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      707\/sshd\r\ntcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      819\/master\r\ntcp6       0      0 :::8080                 :::*                    LISTEN      15865\/java\r\ntcp6       0      0 :::22                   :::*                    LISTEN      707\/sshd\r\ntcp6       0      0 127.0.0.1:8005          :::*                    LISTEN      15865\/java\r\ntcp6       0      0 :::8009                 :::*                    LISTEN      15865\/java\r\nudp        0      0 103.14.120.207:123      0.0.0.0:*                           476\/ntpd\r\nudp        0      0 127.0.0.1:123           0.0.0.0:*                           476\/ntpd\r\nudp        0      0 0.0.0.0:123             0.0.0.0:*                           476\/ntpd\r\nudp6       0      0 fe80::216:3eff:fe70:123 :::*                                476\/ntpd\r\nudp6       0      0 ::1:123                 :::*                                476\/ntpd\r\nudp6       0      0 :::123                  :::*                                476\/ntpd\r\n[root@newdelhihosting bin]#\r\n<\/em><\/pre>\n<p>Now, visit the server IP address with port 8080, with a web browser(your_server_ip:8080). You will see the Apache Tomcat default page.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-945\" src=\"https:\/\/www.newdelhihosting.co.in\/blog\/wp-content\/uploads\/2017\/12\/Tomcat-1024x524.jpg\" alt=\"\" width=\"780\" height=\"399\" srcset=\"https:\/\/www.newdelhihosting.co.in\/blog\/wp-content\/uploads\/2017\/12\/Tomcat-1024x524.jpg 1024w, https:\/\/www.newdelhihosting.co.in\/blog\/wp-content\/uploads\/2017\/12\/Tomcat-300x154.jpg 300w, https:\/\/www.newdelhihosting.co.in\/blog\/wp-content\/uploads\/2017\/12\/Tomcat-768x393.jpg 768w, https:\/\/www.newdelhihosting.co.in\/blog\/wp-content\/uploads\/2017\/12\/Tomcat-250x128.jpg 250w, https:\/\/www.newdelhihosting.co.in\/blog\/wp-content\/uploads\/2017\/12\/Tomcat.jpg 1340w\" sizes=\"(max-width: 780px) 100vw, 780px\" \/><br \/>\nNow, stop Apache Tomcat , because we need to run Tomcat with a systemd service file in the final configuration. Also make sure the tomcat directory is owned by the \u201ctomcat\u201d user and group.<\/p>\n<pre><em># cd \/opt\/tomcat\/bin\/\r\n# .\/shutdown.sh \r\n# chown -hR tomcat:tomcat \/opt\/tomcat\/<\/em><\/pre>\n<pre><em>[root@newdelhihosting bin]# .\/shutdown.sh\r\nUsing CATALINA_BASE:   \/opt\/tomcat\r\nUsing CATALINA_HOME:   \/opt\/tomcat\r\nUsing CATALINA_TMPDIR: \/opt\/tomcat\/temp\r\nUsing JRE_HOME:        \/usr\/lib\/jvm\/java-1.8.0-openjdk-1.8.0.151-1.b12.el7_4.x86_64\/jre\r\nUsing CLASSPATH:       \/opt\/tomcat\/bin\/bootstrap.jar:\/opt\/tomcat\/bin\/tomcat-juli.jar\r\n[root@newdelhihosting bin]#\r\n<\/em><\/pre>\n<p><strong>Step 5 \u2013 Setup Apache Tomcat Service<\/strong><br \/>\nNow,we need to run Apache Tomcat as tomcat user with a systemd service file for easy starting and stopping of the service. So the next step is to create a \u2018tomcat.service\u2019 file.<\/p>\n<p>Go to the systemd system directory and create a new file \u2018tomcat.service\u2019.<\/p>\n<pre><em># cd \/etc\/systemd\/system\/\r\n# touch tomcat.service\r\n# nano tomcat.service\r\n<\/em><\/pre>\n<p>Paste the configuration below:<\/p>\n<pre><em>[Unit]\r\nDescription=Apache Tomcat 8 Servlet Container\r\nAfter=syslog.target network.target\r\n\r\n[Service]\r\nUser=tomcat\r\nGroup=tomcat\r\nType=forking\r\nEnvironment=CATALINA_PID=\/opt\/tomcat\/tomcat.pid\r\nEnvironment=CATALINA_HOME=\/opt\/tomcat\r\nEnvironment=CATALINA_BASE=\/opt\/tomcat\r\nExecStart=\/opt\/tomcat\/bin\/startup.sh\r\nExecStop=\/opt\/tomcat\/bin\/shutdown.sh\r\nRestart=on-failure\r\n\r\n[Install]\r\nWantedBy=multi-user.target\r\n<\/em><\/pre>\n<p>Save the file and exit from tomcat.service file.<\/p>\n<p>Now, we have to reload the systemd daemon, then start and add the Apache Tomcat service at boot time.<\/p>\n<pre><em># systemctl daemon-reload\r\n# systemctl start tomcat\r\n# systemctl enable tomcat<\/em><\/pre>\n<p>Now check that tomcat is running by checking the open port 8080. and check the status.<\/p>\n<pre><em># netstat -plntu\r\n# systemctl status tomcat<\/em><\/pre>\n<p><strong>Step 6 \u2013 Configure Apache Tomcat Users<\/strong><br \/>\nHere, we need to configure the users for Apache Tomcat. Tomcat is installed, and it\u2019s running by default on port 8080, we can access it with a web browser, but we can not access the site-manager dashboard yet. To enable and configure Tomcat users, edit the file \u2018tomcat-users.xml\u2019.<\/p>\n<p>Go to the tomcat configuration directory and edit the tomcat-users.xml file.<\/p>\n<pre><em># cd \/opt\/tomcat\/conf\/\r\n# nano tomcat-users.xml\r\n<\/em><\/pre>\n<p>Create a new line under line 43 and paste below configuration.<\/p>\n<p><em>&lt;role rolename=\u201dmanager-gui\u201d\/&gt;<\/em><br \/>\n<em> &lt;user username=\u201dadmin\u201d password=\u201dpassword\u201d roles=\u201dmanager-gui,admin-gui\u201d\/&gt;<\/em><\/p>\n<p>Save and exit from tomcat-users.xml.<\/p>\n<p>NOTE: Don\u2019t forget to remove the commented marks ( &lt;!\u2013 \u2026 \u2013&gt; )<\/p>\n<p>Your user configurations should be as follows<\/p>\n<pre><em>&lt;role rolename=\"tomcat\"\/&gt;\r\n&lt;role rolename=\"manager-script\"\/&gt;\r\n&lt;role rolename=\"manager-jmx\"\/&gt;\r\n&lt;role rolename=\"manager-status\"\/&gt;\r\n&lt;role rolename=\"manager-gui\"\/&gt;\r\n&lt;role rolename=\"admin-gui\"\/&gt;\r\n&lt;role rolename=\"manager\"\/&gt;\r\n&lt;user username=\"admin\" password=\"EQcJhHW4Ud\" roles=\"manager-gui,manager-status,admin-gui,manager\"\/&gt;\r\n&lt;\/tomcat-users&gt;\r\n<\/em><\/pre>\n<p>Next, go to the manager directory and edit the context.xml file.<\/p>\n<pre><em># cd \/opt\/tomcat\/webapps\/manager\/META-INF\/\r\n# nano context.xml\r\n<\/em><\/pre>\n<p>Comment or remove the below lines from this file.<\/p>\n<pre><em>&lt;Valve className=\u201dorg.apache.catalina.valves.RemoteAddrValve\u201d\r\n# allow=\u201d127\\.\\d+\\.\\d+\\.\\d+|::1|0:0:0:0:0:0:0:1\u2033 \/&gt;<\/em><\/pre>\n<p>Now, Save the file and exit.<\/p>\n<p>Go to the host-manager directory and edit the context.xml file again.<\/p>\n<pre><em># cd \/opt\/tomcat\/webapps\/host-manager\/META-INF\/\r\n# nano context.xml\r\n<\/em><\/pre>\n<p>Comment or remove the below lines from this file.<\/p>\n<pre><em>&lt;Valve className=\u201dorg.apache.catalina.valves.RemoteAddrValve\u201d\r\n# allow=\u201d127\\.\\d+\\.\\d+\\.\\d+|::1|0:0:0:0:0:0:0:1\u2033 \/&gt;\r\n<\/em><\/pre>\n<p>Save the file and exit, then restart tomcat.<\/p>\n<pre><em># systemctl restart tomcat\r\n<\/em><\/pre>\n<p><strong>Step 7 \u2013 Configure Firewalld<\/strong><br \/>\nIn CentOS 7, we have a default firewall tool named firewalld. It replaces the iptables interface and connects to the Netfilter kernel code.<\/p>\n<p>Here, we will start the firewalld service and open port 8080 so we can access the Apache Tomcat server from the outside of the network.<\/p>\n<p>Start the firewalld service and add it to start at boot time.<\/p>\n<pre><em># systemctl start firewalld\r\n# systemctl enable firewalld\r\n<\/em><\/pre>\n<p>Next, add the apache tomcat port 8080 to the firewall, and reload the firewalld service.<\/p>\n<pre><em># firewall-cmd --zone=public --permanent --add-port=8080\/tcp\r\n# firewall-cmd --reload\r\n<\/em><\/pre>\n<p>Check that all the services are available in the firewall and check that the Apache Tomcat port 8080 is open.<\/p>\n<pre><em># firewall-cmd --list-ports\r\n# firewall-cmd --list-services\r\n<\/em><\/pre>\n<p>Now, Apache Tomcat port 8080 is accessible from outside of the network, and the ssh port is open by default as well.<\/p>\n<p><strong>Step 8 \u2013 Testing Apache Tomcat<\/strong><br \/>\nNow, open your web browser and type in your server IP with port 8080. You will see the Apache Tomcat default page.<\/p>\n<p>http:\/\/your_server_ip:8080\/<\/p>\n<p>Go to the manager dashboard with below URL ,<\/p>\n<p>http:\/\/your_server_ip:8080\/manager\/html<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-946\" src=\"https:\/\/www.newdelhihosting.co.in\/blog\/wp-content\/uploads\/2017\/12\/Tomcat_Manager-1024x495.jpg\" alt=\"\" width=\"780\" height=\"377\" srcset=\"https:\/\/www.newdelhihosting.co.in\/blog\/wp-content\/uploads\/2017\/12\/Tomcat_Manager-1024x495.jpg 1024w, https:\/\/www.newdelhihosting.co.in\/blog\/wp-content\/uploads\/2017\/12\/Tomcat_Manager-300x145.jpg 300w, https:\/\/www.newdelhihosting.co.in\/blog\/wp-content\/uploads\/2017\/12\/Tomcat_Manager-768x371.jpg 768w, https:\/\/www.newdelhihosting.co.in\/blog\/wp-content\/uploads\/2017\/12\/Tomcat_Manager-250x121.jpg 250w, https:\/\/www.newdelhihosting.co.in\/blog\/wp-content\/uploads\/2017\/12\/Tomcat_Manager.jpg 1320w\" sizes=\"(max-width: 780px) 100vw, 780px\" \/><\/p>\n<p>Now, you have installed Apache Tomcat 8.5.23 on your centOS 7.3 Server.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Tomcat:- \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Tomcat is an open source Java Servlet implementation developed by the Apache Software Foundation. Beside Java Servlets, Tomcat implements several Java server technologies including JavaServer Pages (JSP), Java Expression Language, and Java WebSocket. Tomcat provides an HTTP Web Server for Java applications with support for HTTP\/2, OpenSSL for JSSE and TLS virtual hosting&#8230;.<\/p>\n","protected":false},"author":1,"featured_media":952,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"footnotes":""},"categories":[14],"tags":[],"class_list":["post-944","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-vps-2"],"jetpack_featured_media_url":"https:\/\/www.newdelhihosting.co.in\/blog\/wp-content\/uploads\/2017\/12\/tom_fe.png","_links":{"self":[{"href":"https:\/\/www.newdelhihosting.co.in\/blog\/wp-json\/wp\/v2\/posts\/944"}],"collection":[{"href":"https:\/\/www.newdelhihosting.co.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.newdelhihosting.co.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.newdelhihosting.co.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.newdelhihosting.co.in\/blog\/wp-json\/wp\/v2\/comments?post=944"}],"version-history":[{"count":5,"href":"https:\/\/www.newdelhihosting.co.in\/blog\/wp-json\/wp\/v2\/posts\/944\/revisions"}],"predecessor-version":[{"id":953,"href":"https:\/\/www.newdelhihosting.co.in\/blog\/wp-json\/wp\/v2\/posts\/944\/revisions\/953"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.newdelhihosting.co.in\/blog\/wp-json\/wp\/v2\/media\/952"}],"wp:attachment":[{"href":"https:\/\/www.newdelhihosting.co.in\/blog\/wp-json\/wp\/v2\/media?parent=944"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.newdelhihosting.co.in\/blog\/wp-json\/wp\/v2\/categories?post=944"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.newdelhihosting.co.in\/blog\/wp-json\/wp\/v2\/tags?post=944"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}