Articles

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



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


    ServerAdmin [email protected]
    ServerName tomcat.example.com
    DefaultType text/html
    ProxyRequests off
    ProxyPreserveHost On
    ProxyPass / http://localhost:8080/
    ProxyPassReverse / http://localhost:8080/

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

For AJP connector, it will be configuration like this:


  ServerName example.com

  ProxyRequests Off
  ProxyPass / ajp://localhost:8009/
  ProxyPassReverse / ajp://localhost:8009/

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.

Images mentioned above related to CentOS are either copyright property of respective image owners.

Rabins Sharma Lamichhane

Rabins Sharma Lamichhane is the owner of RabinsXP who is constantly working for increasing the Internet of Things (IoT) in Nepal. He also builds android apps and crafts beautiful websites. He is also working with various social services. The main aim of Lamichhane is to digitally empower the citizens of Nepal and make the world spiritually sound better both in terms of technology and personal development. Rabins is also the first initiator of Digital Nepal.

Related Articles

Leave a Reply

Your email address will not be published.

Back to top button