How To Install Apache Tomcat in CentOS

We show you in detail how you can install the Apache Tomcat in Cent0S Linux.

Step 1: Install Java (JRE & JDK)

The first step is to install Java JRE and JDK from the CentOS repository, in this case, we will install Java 1.8.11 on the server using the yum command like this:

yum -y install java-1.8.0-openjdk.x86_64 java-1.8.0-openjdk-devel.x86_64

Once this installation process is finished, we will check the Java version that we have installed by executing the following command:

java -version

Step 2: Configure Java Environment

The next step is to configure the environment variable JAVA_HOME on the computer with CentOS so that Java applications can find the correct Java version and this is ideal since Tomcat requires that the JAVA_HOME environment be configured correctly and optimize its use.

Before configuring the JAVA_HOME environment, we need to know where the Java directory is located, to do this, we will verify the Java directory with the following command:

update-alternatives --config java

There we can see that initially, we have only one directory, so we press the Enter key. Now, we will edit the environment with some text editor executing the following:

nano etc/environment
JAVA_HOME="/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.161-1.b14.el7_4.x86_64/jre/bin/java"

Save the changes using the following key combination Ctrl + O and exit the editor using Ctrl + X.

Note: There we must enter the right path where the Java directory is located.

Next, we will edit the variable .bash_profile, and there we will add variable JAVA_HOME:

nano /.bash_profile

In the final part of this file we will add the following:

export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.111-1.b15.el7_2.x86_64/jre
export PATH=$JAVA_HOME/bin:$PATH

We save the changes and proceed to reload the file by executing the following:

source ~/.bash_profile

We must ensure that there is no error there, then we will verify the environment variable JAVA_HOME using the following command:

echo $JAVA_HOME

Step 3: Install Apache Tomcat

Now, we are going to create the user and group of Apache, for that we are going to execute the following:

groupadd tomcat
useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat

Once these steps are configured, we will download and install Apache Tomcat 8.5 in the /opt directory and then download Apache, for this, we can go to the official Apache link below:

http://tomcat.apache.org/download-80.cgi

There we have the opportunity to select different download mirrors, in this case, we will use a local one, and for its download, we will execute the following:

cd /opt/
wget http://www-eu.apache.org/dist/tomcat/tomcat-8/v8.5.28/bin/apache-tomcat-8.5.28.tar.gz

Once this file is downloaded, we will extract Tomcat and move all the files and directories that are in the ‘apache-tomcat-8.5.28' directory to the ‘tomcat' directory like this:

tar -xzvf apache-tomcat-8.5.28.tar.gz

Then we will move the files by executing:

mv apache-tomcat-8.5.28/* tomcat/

Now, let's change the owner of the tomcat directory to the user and tomcat group:

chown -hR tomcat:tomcat tomcat

Step 4: Verify Apache Tomcat

Before validating Apache in CentOS, we will execute a test to make sure that everything is working the way we want, for this we will execute the following:

cd /opt/tomcat/bin/
./startup.sh

We can see that everything is correct in the “Tomcat started” line. Tomcat runs by default on port 8080.

Now, we can access Apache Tomcat and validate its installation by running the next line:

http://IP_Centos:8080

There, we can see that there is correct access to Apache Tomcat 8.5.

Now, we are going to stop Apache Tomcat because it has been executed with a systemd service file in the final configuration, we must make sure that the Tomcat directory is owned by the user and tomcat group, for this we execute the following:

cd /opt/tomcat/bin/
./shutdown.sh
chown -hR tomcat:tomcat /opt/tomcat/

Step 5: Configure Apache Service

We are going to run Apache Tomcat as a tomcat user with a systemd service file with which to facilitate the start and stop of the service, for this we will create a file called tomcat.service:

cd /etc/systemd/system/
nano tomcat.service

In this file we will paste the following:

[Unit]
Description=Apache Tomcat 8 Servlet Container
After=syslog.target network.target

[Service]
User=tomcat
Group=tomcat
Type=forking
Environment=CATALINA_PID=/opt/tomcat/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINA_BASE=/opt/tomcat
ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh
Restart=on-failure

[Install]
WantedBy=multi-user.target

Save the changes using the following key combination Ctrl + O and exit the editor using Ctrl + X.

Now, let's load the systemd daemon, then start it and add this Apache Tomcat service at the time of CentOS startup.

systemctl daemon-reload
systemctl start tomcat
systemctl enable tomcat

We will check the status of Apache Tomcat by running the following line. There we see that his state is active.

systemctl status tomcat

Step 6: Configure Apache Tomcat Users

The time has come to configure the Apache Tomcat users.

Tomcat is installed and runs by default on port 8080, and we can access it with a web browser, but we still do not have access to the administrator panel of the site.

To enable and configure Tomcat users, we must edit the tomcat-users.xml file like this:

cd /opt/tomcat/conf/
nano tomcat-users.xml

There we will add these lines just below line 43:

<role rolename="manager-gui"/>
<user username="admin" password="password" roles="manager-gui,admin-gui"/>

We save the changes. Now, we will go to the administrator's directory and edit the context.xml file:

cd /opt/tomcat/webapps/manager/META-INF/
nano context.xml

There we will comment lines 19 and 20:

<Context antiResourceLocking="false" privileged="true" >
<!-- <Valve className="org.apache.catalina.valves.RemoteAddrValve"
    allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->
</Context>

We will go to the host-manager directory and modify the context.xml file:

cd /opt/tomcat/webapps/host-manager/META-INF/
nano context.xml

There, we will comment lines 19 and 20:

<Context antiResourceLocking="false" privileged="true" >
<!-- <Valve className="org.apache.catalina.valves.RemoteAddrValve"
    allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->
</Context>

Save the changes. Now, we can restart the Tomcat service by running:

systemctl restart tomcat

Step 7: Configure Firewalld

In CentOS , Firewalld is the firewall utility that manages the access of packets and connections through the system ports.

First, we will start the firewalld service and open port 8080 so that we can access the Apache Tomcat server from an external browser:

systemctl start firewalld
systemctl enable firewalld
firewall-cmd --zone=public --permanent --add-port=8080/tcp
firewall-cmd --reload

We can verify the port and the services by executing the following:

firewall-cmd --list-ports
firewall-cmd --list-services

To confirm that everything works correctly, we go to an external browser and enter again using the following syntax:

http://IP_CentOS:8080


Now, we can click on the “Manager App” button.

We will enter the user admin with the password that we have previously assigned, and we will see the following:

And to access the host administrator, we execute the following:

http://IP_CentOS:8080/host-manager/html

Thus, we have learned to install Apache Tomcat 8 in CentOS and take full advantage of all its benefits and benefits at the level of administration, control, and development.

Similar Posts

One Comment

  1. just a note
    on my centos 7 with cpanel doest start with

    # systemctl start tomcat

    we need to change this permissions

    # chown -R tomcat tomcat/

Leave a Reply

Your email address will not be published. Required fields are marked *