How can I install Apache Tomcat 8 on CentOS 7
Today, if you are using a Java application on your server then using Apache Tomcat, is one of the best solutions because it acts as a web server and also a servlet container. Moreover, Tomcat is an open source implementation of Java Server Pages and Java Servlet technologies which is released by the Apache software foundation. So today if you are using website and web hosting services for your business and now if you are looking for information on how to install Apache Tomcat 8 on CentOS 7 and configuration of the latest release of Tomcat 8 on CentOS in a Dedicated server then here is the complete information on it.
Before installing Apache and Tomcat you need to install Java because tomcat requires that Java has to be installed on the server so that you can easily execute the Java application. For that you need to install OpenJDK 7 with yum.
--->>> To install OpenJDK 7 using yum you need to run the below command
$ sudo yum install java-1.7.0-openjdk-devel
--->>> In the next output you need to answer y at the prompt to install OpenJDK 7
Note: Once after installing a shortcut JAVA_HOME directory, will need to configure via Tomcat and you can see at /usr/lib/jvm/jre.
Tomcat should run as an unprivileged user for some security purpose. For this reason you need to ceate a new user and group where it will run the Tomcat service.
--->>> But first you need to create a new Tomcat group so use the below command
$ sudo groupadd tomcat
--->>> Once group is created you need to create a new tomcat user and this user home directory of /opt/tomcat and with a shell of /bin/false. So you need to use the below command:
$ sudo useradd -M -s /bin/nologin -g tomcat -d /opt/tomcat tomcat
So install the Tomcat 8 is by downloading the latest binary release and configuring it manually.
For the lastest version of Tomcat 8 will be found on their official website. Now under the Binary Configuration section under the Core list, you need to copy the link to the "tar.gz".
--->>> Before downloading you need to change your home directory by using the below command
$ cd ~
--->>> Once changing your home directory you need to use wget and paste the link to download the latest Tomcat. Use the below command-
$ wget http://apache.mirrors.ionfish.org/tomcat/tomcat-8/v8.5.9/bin/apache-tomcat-8.5.9.tar.gz
--->>> Once everything is done you need to install Tomcat to the /opt/tomcat directory. And also need to create the directory and then extract the archive using below command-
$ sudo mkdir /opt/tomcat
$ sudo tar xvf apache-tomcat-8*tar.gz -C /opt/tomcat --strip-components=1
Once after creating the tomcat-users, you need to set up proper and right access to install tomcat.
--->>> For that you need to change the Tomcat installation path
$ cd /opt/tomcat
--->>> Next you need to give your those group ownership over the entire installation directory using this command-
$ sudo chgrp -R tomcat /opt/tomcat
--->>> Now you need to give tomcat group to read access the conf directory and all of its contents where it will execute access to the directory itself. So use the below command-
$ sudo chmod -R g+r conf
$ sudo chmod g+x conf
--->>> Now make the tomcat user as the owner of the web apps, temp, work and logs directories, so use the below command
$ sudo chown -R tomcat webapps/ work/ temp/ logs/
If you want to run Tomcat as a service then you need to set up a Tomcat Systemd unit file.
--->>> First you need to create and open the unit file by running the below command
$ sudo vi /etc/systemd/system/tomcat.service
--->>> Next you need to paste the below script. But sometimes you may also need to modify the memory allocation which is specified in CATALINA_OPTS so use the below script-
# Systemd unit file for tomcat
Description=Apache Tomcat Web Application Container
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'
ExecStop=/bin/kill -15 $MAINPID
--->>> You need to save and exit the about script. And the above script tells the server to run the Tomcat service as its user.
--->>> Next you need to reload systemd to load the Tomcat unit file, so use the below command-
$ sudo systemctl daemon-reload
--->>> Once after reloading of tomcat you need to start the Tomcat service by using below systemctl command-
$ sudo systemctl start tomcat
--->>> Next you need to check that the service is successfully started by using the below command-
$ sudo systemctl status tomcat
--->>> Now if you want to enable the Tomcat service so that its starts on server boot you need to run the below command:
$ sudo systemctl enable tomcat
--->>> At this stage Tomcat is not completely set up but still you can access the default splash page by going to your domain or by using your IP address using :8080 in the web browser
Open in web browser:
To use the manager webapp which comes with Tomcat you need to add a login to our Tomcat server and we will do this by editing the tomcat-user.xml file
$ sudo vi /opt/tomcat/conf/tomcat-users.xml
---->>> The next file is filled with comments where it describes how to configure the file. If you want to you can also delete all the comments between two lines
--->>> Sometimes you need to add a user who can access the manager-gui and admin-gui, so you can do it by setting a user similar to the example below. But you need to make sure to change the username and password as per your requirements.
tomcat-users.xml — Admin User
<user username="admin1" password="creative" roles="manager-gui,admin-gui"/>
Save and quit the tomcat-users.xml file.
--->>> Sometimes by default new versions of Tomcat may restrict the access to the manager and Host manager apps to connections which is coming from the server. Since your using on a remote machine to install you need to remove or alter this restriction. So to change the IP address restrictions on these, open the appropriate context.xml files.
--->>> For the Manager app, type:
$ sudo vi /opt/tomcat/webapps/manager/META-INF/context.xml
--->>> For the Host Manager app, type:
$ sudo vi /opt/tomcat/webapps/host-manager/META-INF/context.xml
--->>> Inside, remark out the IP deliver confinement to permit connections from anyplace. Then again, on the off chance that you might want to permit get to just to connections originating from your own IP address, you can add your open IP address to the list by using the below command
context.xml files for Tomcat webapps
<Context antiResourceLocking="false" privileged="true" >
--->>> Once done you need to save and close the files
--->>> Now to put your changes into effect you need to restart the Tomcat service so use the below command-
$ sudo systemctl restart tomcat