How to Install Apache Tomcat 9 on CentOS 85 min read

Introduction

Apache Tomcat (short for “Tomcat”) is a Java Servlet, Java Server Pages, Java Expression Language, and WebSocket implementation that is open-source. Tomcat is an HTTP web server that runs Java code in a “pure Java” environment. This tutorial describes how to install and configure Apache Tomcat 9 on CentOS 8.

Note: If you are using Ubuntu, then refer our article How to install Tomcat 9 on Ubuntu 20.04.

Install Java in CentOS 8

1. First we will make sure our system is up-to-date by executing the below commands,

sudo su
yum install epel-release -y
yum update -y

2. We need to verify if java is installed,

java -version
java -version

3. If we do not have Java installed, we will receive the output shown above. We can install java with the below command,

yum install java-1.8.0-openjdk-devel -y
java -version
java -version

Install Apache Tomcat 9 on CentOS 8

Note: At the time of this writing, the latest version of Tomcat is 9.0.41. If you would like to download the latest version, visit this download link for Tomcat to check for the newest version. Copy the tar.gz file link from the Binary Distributions and replace it with the link in Step 2 of this tutorial.

1. First, we need to create a user for Tomcat.

useradd -r tomcat

2. Create a directory for tomcat and download the tomcat tar.gz file in it

mkdir /usr/local/tomcat9
cd /usr/local/tomcat9
curl -o tomcat9.tar.gz https://downloads.apache.org/tomcat/tomcat-9/v9.0.41/bin/apache-tomcat-9.0.41.tar.gz
downloading tomcat 9 tar.gz on centos 8

3. Unpack the tomcat9.tar.gz file with the below command

tar -xvf tomcat9.tar.gz

4. Now, we need to modify the ownership of the /usr/local/tomcat9/ directory.

chown -R tomcat:tomcat /usr/local/tomcat9

By default, your files will be located in this folder: /usr/local/tomcat9 and the configuration files will be stored in: /usr/local/tomcat9/conf

5. Set Environment Variables

echo "export CATALINA_HOME="/usr/local/tomcat9"" >> ~/.bashrc
source ~/.bashrc

6. Edit the tomcat.service file

sudo vi /etc/systemd/system/tomcat.service

vi editor will be opened. Press i and paste the below lines in the file,

[Unit]
Description=Apache Tomcat Server
After=syslog.target network.target
[Service]
Type=forking
User=tomcat
Group=tomcat
Environment=CATALINA_PID=/usr/local/tomcat9/apache-tomcat-9.0.41/temp/tomcat.pid
Environment=CATALINA_HOME=/usr/local/tomcat9/apache-tomcat-9.0.41
Environment=CATALINA_BASE=/usr/local/tomcat9/apache-tomcat-9.0.41
ExecStart=/usr/local/tomcat9/apache-tomcat-9.0.41/bin/catalina.sh start
ExecStop=/usr/local/tomcat9/apache-tomcat-9.0.41/bin/catalina.sh stop
RestartSec=10
Restart=always
[Install]
WantedBy=multi-user.target

Note: If you are installing a version other than 9.0.41, then change the version accordingly in the above file.

Press ‘esc’, type ‘:wq‘ and hit ‘Enter’ key to save and exit

7. Execute the below commands to start tomcat service,

systemctl daemon-reload
systemctl start tomcat.service
systemctl enable tomcat.service

8. Check tomcat status with the below command,

systemctl status tomcat.service
apache tomcat 9 status on centos 8

Note: Press q to come out of the status screen.

Now you can access the tomcat service on your browser. If you have installed tomcat in your local system, then enter the URL: http://localhost:8080. If you have installed it on a cloud instance, then enter the URL: http://<public_ip>:8080

installed apache tomcat 9 on centos 8

Done. We have successfully installed Apache Tomcat 9 on CentOS 8.


Changing Apache Tomcat port number

If there is another service that is running on your system on port 8080, then you won’t be able to access the tomcat webpage. In this case, you will either have to stop the other service or else change the tomcat port number. You can refer to the section: Change Tomcat port to change the port number of tomcat service in your system.


Share:

Leave a Reply