Saturday, 6 September 2025

Tomcat Server installation on EC2 instance to run Java App


Install Apache Tomcat , You must have EC2 server with JAVA install on that:-


[root@WebServer ~]# cd /opt/

[root@WebServer opt]# yum install wget

[root@WebServer opt]# wget https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.109/bin/apache-tomcat-9.0.109.tar.gz

[root@WebServer opt]# tar -xvzf apache-tomcat-9.0.109.tar.gz

[root@WebServer opt]# mv apache-tomcat-9.0.109 tomcat

give executing permissions to startup.sh and shutdown.sh which are under bin.

chmod +x /opt/apache-tomcat-<version>/bin/startup.sh 
chmod +x /opt/apache-tomcat-<version>/bin/shutdown.sh

create link files for tomcat startup.sh and shutdown.sh

ln -s /opt/tomcat/bin/startup.sh /usr/local/bin/tomcatup
ln -s /opt/tomcat/bin/shutdown.sh /usr/local/bin/tomcatdown


Add /usr/local/bin to your $PATH (if missing)


Check your current $PATH:

echo $PATH

If /usr/local/bin is missing, add it temporarily:

export PATH=$PATH:/usr/local/bin

To make it permanent for root, add the line to /root/.bashrc or /root/.bash_profile:

echo 'export PATH=$PATH:/usr/local/bin' >> /root/.bashrc
source /root/.bashrc

=======================================

[root@WebServer tomcat]# find / -name context.xml

/opt/tomcat/conf/context.xml

/opt/tomcat/webapps/docs/META-INF/context.xml

/opt/tomcat/webapps/examples/META-INF/context.xml

/opt/tomcat/webapps/host-manager/META-INF/context.xml

/opt/tomcat/webapps/manager/META-INF/context.xml

[root@WebServer tomcat]# vi /opt/tomcat/webapps/host-manager/META-INF/context.xml

[root@WebServer tomcat]# vi /opt/tomcat/webapps/manager/META-INF/context.xml



And comment below line in both files by adding

<!-- and at end -->






Add below line in this file at the end:- 

[root@WebServer tomcat]# vi /opt/tomcat/conf/tomcat-users.xml


 <role rolename="manager-gui"/>
 <role rolename="manager-script"/>
 <role rolename="manager-jmx"/>
 <role rolename="manager-status"/>
 <user username="admin" password="admin" roles="manager-gui, manager-script, manager-jmx, manager-status"/>
 <user username="deployer" password="deployer" roles="manager-script"/>
 <user username="tomcat" password="s3cret" roles="manager-gui"/>



Now access by using public ip and port 8080 after stopping and starting tomcat server by using below command:-

tomcatdown

tomcatup






Using unique ports for each application is a best practice in an environment. But tomcat and Jenkins runs on ports number 8080. Hence lets change tomcat port number to 8090. Change port number in conf/server.xml file under tomcat home


cd /opt/apache-tomcat-<version>/conf
# update port number in the "connecter port" field in server.xml
# restart tomcat after configuration update
tomcatdown
tomcatup

No comments:

Post a Comment