How to Install and Configure Apache Tomcat 9 in CentOS

Apache Tomcat (earlier known as Jakarta Tomcat) is an open-source web server developed by Apache Foundation to provide a pure Java HTTP server, which will enable you to run Java files easily, which means that Tomcat is not a normal server like Apache or Nginx, because its main goal is to provide a good web environment to run Java applications only unlike other normal web servers.

Recently, on December 06, 2018, Apache Tomcat reached to version 9 (i.e. 9.0.14), which is the first stable release of the series 9.x.0. This Apache Tomcat 9 version comes with numerous improvements as compared to previous Tomcat 8 version.

Some of noticeable changes included in this release are: support for Java Servlet 3.1, JavaServer Pages 2.3, Java Websocket 1.0, etc. and other numerous fixes for issues, as well as number of other enhancements and changes.

This article will walk you throughout the installation of Apache Tomcat 9 on RHEL/CentOS 7.0/6.x. For Ubuntu, follow How to Install Apache Tomcat in Ubuntu.

Step 1: Installing and Configuring Java 8

Before heading up for the Tomcat installation, make sure you must have JAVA installed on your Linux box to run Tomcat. If not, install the latest version of JAVA 9 or use the following yum command to install available Java (i.e version 8) from the default repositories.

# yum install java-1.8.0

Once Java installed, you can verify the newly installed JAVA version running the following command on your system.

# java -version
Sample Output
openjdk version "1.8.0_181"
OpenJDK Runtime Environment (build 1.8.0_181-b13)
OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode)

Step 2: Installing Apache Tomcat 9

After installing JAVA on the system, now it’s time to download latest version of Apache Tomcat (i.e. 9.0.14) is the most recent stable version at the time of writing this article. If you want to make a cross check, head over to following Apache download page and check if there is a newer version available.

  1. hhttps://tomcat.apache.org/download-90.cgi

Now download the latest version of Apache Tomcat 9, using following wget command and setup it as shown.

# cd /usr/local
# wget http://www-us.apache.org/dist/tomcat/tomcat-9/v9.0.14/bin/apache-tomcat-9.0.14.tar.gz
# tar -xvf apache-tomcat-9.0.14.tar.gz
# mv apache-tomcat-9.0.14 tomcat9

Note: Replace the version number above with the latest version available if it was different.

Before starting the Tomcat Service, configure CATALINA_HOME environment variable in your system using following command.

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

Now we all set to start the tomcat web server using the scripts provided by the tomcat package.

# cd /usr/local/tomcat9/bin
# ./startup.sh 
Sample Output
Using CATALINA_BASE:   /usr/local/tomcat9
Using CATALINA_HOME:   /usr/local/tomcat9
Using CATALINA_TMPDIR: /usr/local/tomcat9/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tomcat9/bin/bootstrap.jar:/usr/local/tomcat9/bin/tomcat-juli.jar
Tomcat started.

Now to open Tomcat from your browser, go to your IP or domain with the 8080 port (because Tomcat will always run on the 8080 port) as an example: mydomain.com:8080, replace mydomain.com with your IP or domain.

http://Your-IP-Address:8080
OR
http://Your-Domain.com:8080
Verify Apache Tomcat

Verify Apache Tomcat

The default directory for Tomcat files will be in /usr/local/tomcat9, you can view the configuration files inside the conf folder, the main page that you seen above, when you open your website on the 8080 port is in /usr/local/tomcat9/webapps/ROOT/.

Step 3: Configuring Apache Tomcat 9

By default you only able to access default Tomcat page, to access admin and other sections like Server Status, Manager App and Host Manager. You need to configure user accounts for admins and managers.

To do so, you need to edit ‘tomcat-users.xml‘ file located under /usr/local/tomcat9/conf directory.

Setup Tomcat User Accounts

For example, to assign the manager-gui role to a user named ‘tecmint‘ with a password ‘t$cm1n1‘, add the following line of code to the config file inside the section.

# vi /usr/local/tomcat9/conf/tomcat-users.xml 
<role rolename="manager-gui"/>
<user username="tecmint" password="t$cm1n1" roles="manager-gui"/>

Similarly, you can also add ‘admin-gui‘ role to a admin user named ‘admin‘ with a password ‘adm!n‘ as shown below.

<role rolename="admin-gui"/>
<user username="admin" password="adm!n" roles="admin-gui"/>
Configure Apache Tomcat User Roles

Configure Apache Tomcat User Roles

After setting up the admin and managers roles, restart the Tomcat and then try to access the admin section.

./shutdown.sh 
./startup.sh

Now click on ‘Server Status‘ tab, it will prompt you to enter user credentials, enter username and password that you’ve added above in config file.

Apache Tomcat User Login

Apache Tomcat User Login

Once, you enter user credentials, you will find a page similar to below.

Monitor Apache Tomcat Server Status

Monitor Apache Tomcat Server Status

Changing Apache Tomcat Port

If you want to run Tomcat on different port say 80 port. You will have to edit the ‘server.xml‘ file in ‘/usr/local/tomcat9/conf/‘. Before changing, port, make sure to stop the Tomcat server using.

# /usr/local/tomcat9/bin/shutdown.sh

Now open the server.xml file using the Vi editor.

# vi /usr/local/tomcat9/conf/server.xml

Now search for “Connector port” and change its value from 8080 to 80 or any other port you want as it follows.

Change Apache Tomcat Port

Change Apache Tomcat Port

To save the file and restart the Apache Tomcat server again, using below command.

# /usr/local/tomcat9/bin/startup.sh

That’s it, you server will be running on the 80 port.

Of course, you have to run all the above commands as a root, if you don’t they won’t work because we are working in the ‘/usr/local‘ directory which is a folder owned by the root user only, if you want you can run the server as a normal user but you will have to use your HOME folder as a working area to download, extract and run the Apache Tomcat server.

To get some information about your running Tomcat server and your computer, run.

/usr/local/tomcat9/bin/version.sh
Sample Output
Using CATALINA_BASE:   /usr/local/tomcat9
Using CATALINA_HOME:   /usr/local/tomcat9
Using CATALINA_TMPDIR: /usr/local/tomcat9/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tomcat9/bin/bootstrap.jar:/usr/local/tomcat9/bin/tomcat-juli.jar
Tomcat started.
[[email protected] tomcat9]# /usr/local/tomcat9/bin/version.sh
Using CATALINA_BASE:   /usr/local/tomcat9
Using CATALINA_HOME:   /usr/local/tomcat9
Using CATALINA_TMPDIR: /usr/local/tomcat9/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tomcat9/bin/bootstrap.jar:/usr/local/tomcat9/bin/tomcat-juli.jar
Server version: Apache Tomcat/9.0.12
Server built:   Sep 4 2018 22:13:41 UTC
Server number:  9.0.12.0
OS Name:        Linux
OS Version:     3.10.0-693.el7.x86_64
Architecture:   amd64
JVM Version:    1.8.0_181-b13
JVM Vendor:     Oracle Corporation

That’s it! Now you can start deploying JAVA based applications under Apache Tomcat 9. For more about on how to deploy applications and create virtual hosts, check out the official Tomcat documentation.

Best Affordable Linux and WordPress Services For Your Business
Outsource Your Linux and WordPress Project and Get it Promptly Completed Remotely and Delivered Online.

If You Appreciate What We Do Here On TecMint, You Should Consider:

  1. Stay Connected to: Twitter | Facebook | Google Plus
  2. Subscribe to our email updates: Sign Up Now
  3. Get your own self-hosted blog with a Free Domain at ($3.45/month).
  4. Become a Supporter - Make a contribution via PayPal
  5. Support us by purchasing our premium books in PDF format.
  6. Support us by taking our online Linux courses

We are thankful for your never ending support.

Ravi Saive

I am Ravi Saive, creator of TecMint. A Computer Geek and Linux Guru who loves to share tricks and tips on Internet. Most Of My Servers runs on Open Source Platform called Linux. Follow Me: Twitter, Facebook and Google+

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide

You may also like...

33 Responses

  1. jayaram says:

    I have installed tomcat, can you explain how to configure the web servers?

  2. Sarah says:

    Hi! I have made up to the point of opening tomcat with http://myipaddress:8080 and both the IP and domain options are not opening tomcat from putty.

    • Ravi Saive says:

      @Sarah,

      Please open the port 8080 of Tomcat on the firewall to access from the public network. Also let me know how you accessing the URL’s? and what error you getting?

      • Sarah says:

        Ravi,

        I have tried in Microsoft Edge, which is telling me “Hmmm…can’t reach this page” and on Firefox which is telling me “the server is taking too long to respond

        I’m very new to this, can you briefly tell me how to open the port on the firewall?

        Thank you.

        • Ravi Saive says:

          @Sarah,

          First try to access the Tomcat page from the server terminal itself at:

          # yum install links
          # links http://localhost:8080
          

          If it’s accessible from the localhost, you should open a port on firewall and allow Tomcat access from public IP.

  3. Martin Schaible says:

    I removed MC, but it wasn’t the reason.
    It is really strange, that no errors where generated, only a zero byte length log file “catalina.out” was generated.

    • Ravi Saive says:

      @Martin,

      Thanks for your efforts, let’s give a final try by creating a systemd unit file, with this you can start, stop and enable the tomcat service..

      Create and open the unit file by running this command:

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

      Paste in the following script. Make sure to modify the memory allocation and path to tomcat installation settings that are specified in CATALINA_OPTS, CATALINA_HOME and CATALINA_PID:

      # Systemd unit file for tomcat
      [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
      UMask=0007
      RestartSec=10
      Restart=always
      
      [Install]
      WantedBy=multi-user.target
      

      Save and exit. This script tells the server to run the Tomcat service as the tomcat user, with the settings specified.

      Now reload Systemd to load the Tomcat unit file and start & enable the service.

      # systemctl daemon-reload
      # systemctl start tomcat
      # systemctl enable tomcat
      
  4. Martin Schaible says:

    Thanks for this helpful tutorial. I used this article with other sources of information to get a clean installation.

    In my case Tomcat does not start, if i execute “./startup.sh” which goes to “catalina.sh start“. Only an empty log file “catalina.out” was generated.
    If i run “./catalina.sh run“, tomcat starts and works. In this case, Tomcat runs in the current window.

    Now something weird: If the “Midnight Commander” is active and i run “./startup.sh“, it works!

    Looks kind of a shell problem.

    I’m really stuck, any idea where to look?

    Cheers!

    • Ravi Saive says:

      @Martin,

      This is something really weird thing, I came across, have you tried on different terminal shell?

      • Martin Schaible says:

        I have a standard CentOS 7 installation like others have. i’m not really sure, if it a good idea to change the shell systemwide. Keep in mind, that tomcat behaves the same, if i try to start the service instead on the command prompt.

Got something to say? Join the discussion.

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.