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
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)
Click on Manager App and give the credentials we have configured in tomcat-users.xml (username=tomcat password=tomcat)
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
Now create a freestyle job and configure as below
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
Now click on /kishq
Yeah! Our application was deploy successfully to Tomcat server.