How to Install Tomcat 9 on CentOS 8

How to Install Tomcat 9 on CentOS 8

Apache Tomcat 9 is the latest version available for the installation of the Tomcat web server. Tomcat is an open-source web server for Java-based applications developed by the Apache Foundation. We use Tomcat for deploying Java Servlet and JSP applications. To know more about the Apache Tomcat visit apache official site http://tomcat.apache.org/.

Install tomcat 9 on centOS 8; Through this tutorial, we will learn how to install and configure tomcat on centOS 8.

How to Install Tomcat 9 on CentOS 8

Just follow the following steps and install and configure tomcat 9 on centOS 8:

  • Step 1 – Install Java on CentOS 8
  • Step 2 – Create tomcat user and group
  • Step 3 – Install Tomcat 9 on Linux CentOS 8
  • Step 4 – Configure Tomcat 9 Systemd service
  • Step 5 – Configure Firewall
  • Step 6 – Configure Tomcat Authentication
  • Step 7 – Configure Tomcat Proxy
  • Step 8 – Access Tomcat Web interface

Step 1 – Install Java on CentOS 8

First of all, open terminal or command line and execute the following command into it to install java on centOS 8:

sudo dnf install openjdk 

Once the java installation is completed, execute the following command on command line to verify installation:

java -version

Step 2 – Create tomcat user and group

Then execute the following command on command line to create tomcate user and group:

sudo groupadd --system tomcat
sudo useradd -d /usr/share/tomcat -r -s /bin/false -g tomcat tomcat

Step 3 – Install Tomcat 9 on Linux CentOS 8

Now, install tomcat 9 by executing the following command on command line or terminal:

sudo yum -y install wget
export VER="9.0.64"
wget https://archive.apache.org/dist/tomcat/tomcat-9/v${VER}/bin/apache-tomcat-${VER}.tar.gz

Execute the following command on command line or terminal to extract downloaded file with tar.

sudo tar xvf apache-tomcat-${VER}.tar.gz -C /usr/share/

And also execute the following command on command line or terminal to create symlink to extracted tomcat data.

sudo ln -s /usr/share/apache-tomcat-$VER/ /usr/share/tomcat

Set proper directory permissions:

sudo chown -R tomcat:tomcat /usr/share/tomcat
sudo chown -R tomcat:tomcat /usr/share/apache-tomcat-$VER/ 

Step 4 – Configure Tomcat 9 Systemd service

Execute the following command on command line to create a new systemd service to Tomcat:

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

Configuration:

[Unit]
Description=Tomcat
After=syslog.target network.target

[Service]
Type=forking
User=tomcat
Group=tomcat

Environment=JAVA_HOME=/usr/lib/jvm/jre-openjdk
Environment='JAVA_OPTS=-Djava.awt.headless=true'

Environment=CATALINA_HOME=/usr/share/tomcat
Environment=CATALINA_BASE=/usr/share/tomcat
Environment=CATALINA_PID=/usr/share/tomcat/temp/tomcat.pid

ExecStart=/usr/share/tomcat/bin/catalina.sh start
ExecStop=/usr/share/tomcat/bin/catalina.sh stop

[Install]
WantedBy=multi-user.target

Update CATALINA_OPTS values with your memory limits for Tomcat service.

Start and enable service.

sudo systemctl daemon-reload
sudo systemctl restart tomcat
sudo systemctl enable tomcat

Check service status with the following command:

systemctl status tomcat

Step 5 – Configure Firewall

Execute the following command on command line or terminal to allow Port used by tomcat on the firewall – TCP port 8080.

sudo firewall-cmd --permanent --add-port=8080/tcp
sudo firewall-cmd --reload

Step 6 – Configure Tomcat Authentication

Execute the following command on command line or terminal to edit the users configuration file:

sudo vi /usr/share/tomcat/conf/tomcat-users.xml

Add below line before </tomcat-users>

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

Step 7 – Configure Tomcat Proxy

Execute the following command on command line or termianl to Apache httpd as a proxy to an Apache Tomcat application container:

sudo yum -y install httpd 

Create VirtualHost for accessing Tomcat Admin web interface – /etc/httpd/conf.d/tomcat_manager.conf

<VirtualHost *:80>
    ServerAdmin root@localhost
    ServerName tomcat.example.com
    DefaultType text/html
    ProxyRequests off
    ProxyPreserveHost On
    ProxyPass / http://localhost:8080/
    ProxyPassReverse / http://localhost:8080/
</VirtualHost>

tomcat.example.com should be value of your tomcat server name.

For AJP connector, it will be configuration like this:

<VirtualHost *:80>
  ServerName example.com

  ProxyRequests Off
  ProxyPass / ajp://localhost:8009/
  ProxyPassReverse / ajp://localhost:8009/
</VirtualHost>

Configure SELinux for Apache to access Tomcat.

sudo setsebool -P httpd_can_network_connect 1
sudo setsebool -P httpd_can_network_relay 1
sudo setsebool -P httpd_graceful_shutdown 1
sudo setsebool -P nis_enabled 1

Restart httpd service

sudo systemctl restart httpd && sudo systemctl enable httpd

Step 8 – Access Tomcat Web interface

Use domain name configured on VirtualHost to access Tomcat management interface.

Conclusion

Through this tutorial, we have learned how to install and configure tomcat on centOS 8.

Recommended CentOS Tutorials

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

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