How to Install Tomcat server on a CentOS 7 ?

Apache Tomcat is an open-source web server and servlet container developed by the Apache Software Foundation (ASF). It simplifies the task of configuring and managing the server. It also supports integration with development tools for easy debugging and management.It can manage a substantial number of simultaneous requests and can be used in a load-balanced setup with multiple instances to handle even more traffic.

Here is step-by-step guide of Tomcat installation:

  1. Install java OpenJDK :
sudo yum install java-1.8.0-openjdk-devel
  1. Create Tomcat user and group
sudo useradd -m -U -d /opt/tomcat -s /bin/false tomcat
  1. Download Tomcat through official page
cd /tmp
wget https://downloads.apache.org/tomcat/tomcat-9/v9.0.89/bin/apache-tomcat-9.0.89.tar.gz
  1. Extract tar file
mkdir /opt/tomcat
sudo tar xzvf apache-tomcat-9*tar.gz -C /opt/tomcat --strip-components=1

Modify Tomcat permission

sudo chown -R tomcat:tomcat /opt/tomcat
cd /opt/tomcat
chmod +x /bin/*.sh

Configure Tomcat:

  1. Find the Java location with the following command and copy the location
readlink -f $(which java)

  1. create a tomcat.service file
sudo vi /etc/systemd/system/tomcat.service
  1. Add systemd file
[Unit]
Description=Apache Tomcat Web Application Container
After=network.target
[Service]
Type=oneshot
RemainAfterExit=yes
User=tomcat
Group=tomcat
Environment="JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.412.b08-1.el7_9.x86_64/"
Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom -Djava.awt.headless=true"
Environment="CATALINA_BASE=/opt/tomcat"
Environment="CATALINA_HOME=/opt/tomcat"
Environment="CATALINA_PID=/opt/tomcat/temp/tomcat.pid"
Environment="CATALINA_OPTS=-Xms512M -It can manage a substantial number of simultaneous requests and can be used in a load-balanced setup with multiple instances to handle even more traffic.It can manage a substantial number of simultaneous requests and can be used in a load-balanced setup with multiple instances to handle even more traffic.Xmx1024M -server -XX:+UseParallelGC"
ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh
[Install]
WantedBy=multi-user.target
  1. Paste the path from the previous step in the Environment="JAVA_HOME=<path>" line.

  2. Save and close the file.

  3. Refresh the system:

sudo systemctl daemon-reload
  1. Set the Tomcat service to start on boot:
sudo systemctl enable tomcat
  1. Start the Tomcat service:
sudo systemctl start tomcat
  1. Verify the status of Tomcat service:
sudo systemctl status tomcat

sometimes we may need to manually start tomcat by :

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

Also ,fix firewall setting if tomcat is not running in port 8080