How to Install and Configure Apache Tomcat Application Server to Deploy Java Web Applications

Surya Raj Ghimire
5 min readJun 20, 2023

--

Apache Tomcat
  1. Configure IP and host name
[root@tomcatsrv ~]# hostnamectl set-hostname tomcatsrv.srg.com
[root@tomcatsrv ~]# exec bash
[root@tomcatsrv ~]# vi /etc/hosts
---------------------------------
192.168.13.135 tomcatsrv.srg.com
---------------------------------
[root@tomcatsrv ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens33
---------------------------------
BOOTPROTO=static
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.13.135
NETMASK=255.255.255.0
GATEWAY=192.168.13.2
DNS1=192.168.13.2
---------------------------------
[root@tomcatsrv ~]# systemctl restart network
[root@tomcatsrv ~]# systemctl restart network
[root@tomcatsrv ~]# hostname
tomcatsrv.srg.com
[root@tomcatsrv ~]# hostname -I
192.168.13.135 192.168.122.1

2. Install java (pre-requisite)

[root@tomcatsrv ~]# yum -y install java-11-openjdk
[root@tomcatsrv ~]# rpm -q java-11-openjdk
java-11-openjdk-11.0.18.0.9-0.3.ea.el8.x86_64

3. To check tomcat on yum repository

[root@tomcatsrv ~]# yum list tomcat
[root@tomcatsrv ~]# yum list *tomcat*

4. Download latest version of tomcat and install it

[root@tomcatsrv ~]# cd /opt
[root@tomcatsrv opt]# wget https://dlcdn.apache.org/tomcat/tomcat-10/v10.1.10/bin/apache-tomcat-10.1.10.tar.gz
[root@tomcatsrv opt]# ls
apache-tomcat-10.1.10.tar.gz
[root@tomcatsrv opt]# tar -zxvf apache-tomcat-10.1.10.tar.gz
[root@tomcatsrv opt]# ls
apache-tomcat-10.1.10 apache-tomcat-10.1.10.tar.gz
[root@tomcatsrv opt]# cd apache-tomcat-10.1.10/
[root@tomcatsrv apache-tomcat-10.1.10]# ls
bin conf lib logs README.md RUNNING.txt webapps
BUILDING.txt CONTRIBUTING.md LICENSE NOTICE RELEASE-NOTES temp work
[root@tomcatsrv apache-tomcat-10.1.10]# cd bin
[root@tomcatsrv bin]# pwd
/opt/apache-tomcat-10.1.10/bin
[root@tomcatsrv bin]# ls
bootstrap.jar configtest.bat migrate.sh tomcat-native.tar.gz
catalina.bat configtest.sh setclasspath.bat tool-wrapper.bat
catalina.sh daemon.sh setclasspath.sh tool-wrapper.sh
catalina-tasks.xml digest.bat shutdown.bat version.bat
ciphers.bat digest.sh shutdown.sh version.sh
ciphers.sh makebase.bat startup.bat
commons-daemon.jar makebase.sh startup.sh
commons-daemon-native.tar.gz migrate.bat tomcat-juli.jar

5. To start tomcat server

[root@tomcatsrv bin]# netstat -tnl | grep 8080
[root@tomcatsrv bin]# pwd
/opt/apache-tomcat-10.1.10/bin
[root@tomcatsrv bin]# ./startup.sh
[root@tomcatsrv bin]# netstat -tnl | grep 8080
tcp6 0 0 :::8080 :::* LISTEN

6. To shutdown tomcat server

[root@tomcatsrv bin]# pwd
/opt/apache-tomcat-10.1.10/bin
[root@tomcatsrv bin]# ./shutdown.sh
[root@tomcatsrv bin]# netstat -tnl | grep 8080

7. To set bin directory path /opt/apache-tomcat-10.1.10/binto path variable temporarily

[root@tomcatsrv bin]# PATH=$PATH:/opt/apache-tomcat-10.1.10/bin
[root@tomcatsrv bin]# startup.sh
[root@tomcatsrv bin]# netstat -tnl | grep 8080
[root@tomcatsrv bin]# cd
[root@tomcatsrv ~]# shutdown.sh
[root@tomcatsrv ~]# netstat -tnl | grep 8080

8. To set path environment variable permanently on .bashrc file

[root@tomcatsrv ~]# ls -a
[root@tomcatsrv ~]# vim .bashrc
---------------------------------
# .bashrc

# User specific aliases and functions

alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi

export PATH=$PATH:/opt/apache-tomcat-10.1.10/bin
---------------------------------
[root@tomcatsrv ~]# source .bashrc
[root@tomcatsrv ~]# echo $PATH
/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/root/bin:/opt/apache-tomcat-10.1.10/bin
[root@tomcatsrv ~]# startup.sh
[root@tomcatsrv ~]# exit
[root@tomcatsrv ~]# shutdown.sh

9. To create softlink of startup.sh and shutdown.sh

[root@tomcatsrv ~]# cd /opt/apache-tomcat-10.1.10/bin
[root@tomcatsrv bin]# ln -s startup.sh starttomcat.sh
[root@tomcatsrv bin]# ln -s shutdown.sh shutdowntomcat.sh
[root@tomcatsrv bin]# ls -l

-rwxr-x---. 1 root root 1902 Jun 2 23:56 shutdown.sh
lrwxrwxrwx. 1 root root 11 Jun 19 22:06 shutdowntomcat.sh -> shutdown.sh
lrwxrwxrwx. 1 root root 10 Jun 19 22:04 starttomcat.sh -> startup.sh
-rwxr-x---. 1 root root 1904 Jun 2 23:56 startup.sh

[root@tomcatsrv bin]# cd
[root@tomcatsrv ~]# starttomcat.sh
[root@tomcatsrv ~]# netstat -tnl | grep 8080
tcp6 0 0 :::8080 :::* LISTEN
[root@tomcatsrv ~]# shutdowntomcat.sh
[root@tomcatsrv ~]# netstat -tnl | grep 8080

10. To make auto-start (running) tomcat after machine reboot: open startup script /etc/rc.d/rc.local on vim editor and goto bottom of the file and add startcomcat.sh command full path

[root@tomcatsrv ~]# which starttomcat.sh
/opt/apache-tomcat-10.1.10/bin/starttomcat.sh
[root@tomcatsrv ~]# vim /etc/rc.d/rc.local
---------------------------------
touch /var/lock/subsys/local
/opt/apache-tomcat-10.1.10/bin/starttomcat.sh
---------------------------------
[root@tomcatsrv ~]# ls -l /etc/rc.d/rc.local
-rw-r--r--. 1 root root 520 Jun 19 22:21 /etc/rc.d/rc.local
[root@tomcatsrv ~]# chmod u+x /etc/rc.d/rc.local
[root@tomcatsrv ~]# ls -l /etc/rc.d/rc.local
-rwxr--r--. 1 root root 520 Jun 19 22:21 /etc/rc.d/rc.local
[root@tomcatsrv ~]# reboot
[root@tomcatsrv ~]# netstat -tnl | grep 8080
tcp6 0 0 :::8080 :::* LISTEN

11. To change default port of tomcat from 8080 to 8090
Configure tomcat to listen on non-default port: change connector port default value

[root@tomcatsrv ~]# cd /opt/apache-tomcat-10.1.10/conf/
[root@tomcatsrv conf]# ls
Catalina catalina.properties jaspic-providers.xml logging.properties tomcat-users.xml web.xml
catalina.policy context.xml jaspic-providers.xsd server.xml tomcat-users.xsd
[root@tomcatsrv conf]# vim server.xml
---------------------------------
<Connector port="8090" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
maxParameterCount="1000"
/>

12. To allow tomcat non-default port 8090 on firewall

[root@tomcatsrv conf]# firewall-cmd --permanent  --add-port=8090/tcp
success
[root@tomcatsrv conf]# firewall-cmd --reload
success
[root@tomcatsrv conf]# firewall-cmd --list-all

ports: 8090/tcp

13. To refresh tomcat server after configuration change

[root@tomcatsrv conf]# shutdowntomcat.sh
[root@tomcatsrv conf]# starttomcat.sh

14. To connect to tomcat

http://192.168.13.135:8090/
Apache Tomcat/10.1.10

15. To give access to Manager App page, edit the manager’s context.xml file and comment the Valve className parameter

[root@tomcatsrv conf]# cd /opt/apache-tomcat-10.1.10/
[root@tomcatsrv apache-tomcat-10.1.10]# ls
bin conf lib logs README.md RUNNING.txt webapps
BUILDING.txt CONTRIBUTING.md LICENSE NOTICE RELEASE-NOTES temp work
[root@tomcatsrv apache-tomcat-10.1.10]# cd webapps/
[root@tomcatsrv webapps]# ls
docs examples host-manager manager ROOT
[root@tomcatsrv webapps]# cd host-manager/
[root@tomcatsrv host-manager]# ls
css images index.jsp META-INF WEB-INF
[root@tomcatsrv host-manager]# cd META-INF/
[root@tomcatsrv META-INF]# pwd
/opt/apache-tomcat-10.1.10/webapps/host-manager/META-INF
[root@tomcatsrv META-INF]# ls
context.xml
[root@tomcatsrv META-INF]# vim context.xml
---------------------------------
<!-- <Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->
---------------------------------
[root@tomcatsrv META-INF]# cd /opt/apache-tomcat-10.1.10/webapps/manager/META-INF
[root@tomcatsrv META-INF]# pwd
/opt/apache-tomcat-10.1.10/webapps/manager/META-INF
[root@tomcatsrv META-INF]# ls
context.xml
[root@tomcatsrv META-INF]# vim context.xml
---------------------------------
<!-- <Valve className="org.apache.catalina.valves.RemoteAddrValve"
allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->
---------------------------------
[root@tomcatsrv META-INF]# shutdowntomcat.sh
[root@tomcatsrv META-INF]# starttomcat.sh
[root@tomcatsrv META-INF]# netstat -tnl | grep 8090
tcp6 0 0 :::8090 :::* LISTEN
Manager App page

16. To configure roles and users, edit the conf/tomcat-users.xml file to add credentials to access the Tomcat manager web application. Add XML statements immediately before the root end tag, i.e., </tomcat-users>

[root@tomcatsrv META-INF]# cd /opt/apache-tomcat-10.1.10/conf
[root@tomcatsrv conf]# vim tomcat-users.xml
---------------------------------
<!--
<role rolename="tomcat"/>
<role rolename="role1"/>
<user username="tomcat" password="<must-be-changed>" roles="tomcat"/>
<user username="both" password="<must-be-changed>" roles="tomcat,role1"/>
<user username="role1" password="<must-be-changed>" roles="role1"/>
-->
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="manager-jmx"/>
<role rolename="manager-status"/>
<user username="surya" password="S3ry@!" roles="manager-gui,manager-script,manager-jmx,manager-status"/>
</tomcat-users>
---------------------------------
[root@tomcatsrv conf]# shutdowntomcat.sh
[root@tomcatsrv conf]# starttomcat.sh
Tomcat Web Application Manager

17. Build java application using apache maven

18. Pushed test.war file into tomcat server /opt/apache-tomcat-10.1.10/webapps/ folder

[surya@developer test]$ scp target/test.war root@192.168.13.135:/opt/apache-tomcat-10.1.10/webapps/ 

To verify on server machine

[root@tomcatsrv ~]# ls /opt/apache-tomcat-10.1.10/webapps/
docs examples host-manager manager ROOT test test.war

19. Access the application from end user

http://192.168.13.135:8090/test/
Accessing Java Web App

--

--