Deploying Spring 3 MVC Hello World Application to Tomcat Server

Deploying Spring 3 MVC Hello World Application to Tomcat Server

Spring 3 MVC Hello World Sample code has been built using the Maven Build tool and used Jenkins as a continuous integration tool and then deployed the

GitHub Repo

Prerequisites: Git, GitHub, Maven, Jenkins, Tomcat

First launch a Linux server, for this project am using AWS EC2 instance and make sure to open port 8080 (for Jenkins) and port 8282 (for tomcat, by default tomcat runs on port 8080 but we are using it for Jenkins) in security group.

Configuring Tomcat:

$ sudo -i
Create a directory
$ mkdir /opt/tomcat
Download apache tomcat 
$ wget https://dlcdn.apache.org/tomcat/tomcat-8/v8.5.87/bin/apache-tomcat-8.5.87.tar.gz
$ tar xvzf apache-tomcat-8.5.87.tar.gz
$ cd apache-tomcat-8.5.87

The typical directory hierarchy of a Tomcat installation consists of the following:

  • bin - startup, shutdown and other scripts and executables

  • Common - common classes that Catalina and web applications can use

  • conf - XML files and related DTDs to configure Tomcat(server.xml, tomcat-users.xml)

  • logs - Catalina and application logs

  • server - classes used only by Catalina

  • shared - classes shared by all web applications

  • webapps - directory containing the web applications

  • work - temporary storage for files and directories Open server.xml file and change the tomcat port from 8080 to 8282

$ vim /opt/tomcat/apache-tomcat-8.5.87/conf/server.xml
<Connector port="8282" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />

To access the tomcat GUI, we have to create user with gui roles

$ vim /opt/tomcat/apache-tomcat-8.5.87/conf/tomcat-users.xml
  <role rolename="admin-gui"/>
  <role rolename="admin-script"/>
  <role rolename="manager-gui"/>
  <role rolename="manager-script"/>
  <user username="tomcat" password="tomcat" roles="admin-gui,admin-script,manager-gui,manager-script"/>

Now to start the Tomcat server go to /opt/tomcat/apache-tomcat-8.5.87/bin and run the startup.sh script.

$ cd /opt/tomcat/apache-tomcat-8.5.87/bin
$ ./startup.sh

Now in the browser, search with ip address of the instance and port number (ex: 10.0.0.1:8282)

tomcat1

Click on Manager App and give the credentials we have configured in tomcat-users.xml (username=tomcat password=tomcat)

tomcat2

You have configured Tomcat successfully.

Configuring Jenkins:

$ wget -O /etc/yum.repos.d/jenkins.repo \
    https://pkg.jenkins.io/redhat-stable/jenkins.repo
$ rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key
$ yum upgrade -y
# Add required dependencies for the jenkins package
$ yum install java-11-openjdk -y
$ yum install Jenkins -y
$ systemctl start Jenkins.service

Now in the browser, search with ip address of the instance and port number (ex: 10.0.0.1:8080)

$ cat /var/lib/jenkins/secrets/initialAdminPassword

Copy the password in Jenkins, install the plugins and create a user.

Required plugins for this project: Maven integration, Deploy to container.

Go to Manage Jenkins --> Manage Plugins click on Available and search for the plugins and install them.

Now Configure Maven for that go to Manage Jenkins --> Global Tool Configuration and edit as below

maven1

Now create a freestyle job and configure as below

jenkins1

jenkins2

jenkins3

Add the Tomcat credentials(username and password) and click on Save.

Now click Build Now to build the project.

Now go to tomcat server and you should see /kishq

app1

Now click on /kishq

app2

Yeah! Our application was deploy successfully to Tomcat server.

Hope you all are enjoyed.