How to Install Apache Tomcat on CentOS 7

Apache Tomcat (sometimes simply referred to as Tomcat) is a popular open-source web server software used to set up Java-based server environments. The technologies used in Tomcat include Java Servlet, Java Server Pages, Java Expression Language, and WebSocket and provides an HTTP server environment ideal for running Java-based applications. In this article, we will outline how to install Tomcat on your CentOS 7 server. Please note, in order to perform the instructions on this guide, you will need root SSH access to your VPS or Dedicated server.

Before you begin, make sure you have installed Java.

Topics Include:

Install Apache Tomcat on your Dedicated Hosting server and start running your Java applications today!

Installing Apache Tomcat

  1. First, ensure your machine and all related packages are up-to-date with the following commands:

    sudo yum install epel-release

    sudo yum update -y && sudo reboot
  2. Next, create a tomcat user and add it to the tomcat group with the following commands:

    sudo groupadd tomcat

    sudo mkdir /opt/tomcat

    sudo useradd -s /bin/nologin -g tomcat -d /opt/tomcat tomcat
  3. Next, change back to the home directory and download the Tomcat .tar file using the following commands:

    cd ~

    wget https://www-us.apache.org/dist/tomcat/tomcat-8/v8.0.33/bin/apache-tomcat-8.0.33.tar.gz
  4. Once it has finished downloading, extract the Tomcat archive using the following command:

    sudo tar -zxvf apache-tomcat-8.0.33.tar.gz -C /opt/tomcat --strip-components=1
  5. Once the extraction finishes, update the file permissions with the following commands:

    cd /opt/tomcat

    sudo chgrp -R tomcat conf

    sudo chmod g+rwx conf

    sudo chmod g+r conf/*

    sudo chown -R tomcat logs/ temp/ webapps/ work/

    sudo chgrp -R tomcat bin

    sudo chgrp -R tomcat lib

    sudo chmod g+rwx bin

    sudo chmod g+r bin/*
  6. Next, you will need to set up a systemd unit file using the following command to open a text editor:

    sudo nano /etc/systemd/system/tomcat.service
  7. With the text editor open, add the following code:

    [Unit]Description=Apache Tomcat Web Application Container
    After=syslog.target network.target

    [Service]Type=forking

    Environment=JAVA_HOME=/usr/lib/jvm/jre
    Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid
    Environment=CATALINA_HOME=/opt/tomcat
    Environment=CATALINA_BASE=/opt/tomcat
    Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'
    Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom'

    ExecStart=/opt/tomcat/bin/startup.sh
    ExecStop=/bin/kill -15 $MAINPID

    User=tomcat
    Group=tomcat

    [Install]WantedBy=multi-user.target
  8. Press Crtl + X to save and quit the text editor.
  9. Next, you will need to install and enable the haveged utility for security using the following commands:

    sudo yum install haveged

    sudo systemctl start haveged.service

    sudo systemctl enable haveged.service
  10. Once that is done, you can start Tomcat and enable it to start on reboot with the following commands:

    sudo systemctl start tomcat.service

    sudo systemctl enable tomcat.service

Configuring Apache Tomcat

  1. Now that Tomcat has been installed, you will need to configure the firewall to allow browser access using the following commands:

    sudo firewall-cmd --zone=public --permanent --add-port=8080/tcp

    sudo firewall-cmd --reload
  2. In order to start using the Tomcat Graphical User Interface (GUI), you will need to configure the users file with a new username and password. In this example, replace newusername and newpassword with the username and password you want to use to log in to Tomcat.

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

    Within:
    </tomcat-users ...>...</tomcat-users>
    Insert:
    <user username="newusername" password="newpassword" roles="manager-gui,admin-gui"/>
  3. Restart Tomcat 

    sudo systemctl restart tomcat.service

Testing Apache Tomcat

To test your Tomcat installation, you can simply visit your server IP followed by :8080. If the installation was successful, you should immediately see the login page.

Was this article helpful? Let us know!