DEV Community

Optimizing Web Application Deployment: Integrating Tomcat with Nginx for scalability

This article gives a more broad explanation on the configuration needed to set up your applications needed in building your optimizable web application.

Setting your maven application
create your ec2 instance with a 4GB RAM using the amazon-Linux to confirm check system requirements on apache maven website
install the maven and java packages and dependencies.

step 1 .
Enter the repository with a Maven package

sudo wget https://repos.fedorapeople.org/repos/dchen/apache-maven/epel-apache-maven.repo -O /etc/yum.repos.d/epel-apache-maven.repo
Enter fullscreen mode Exit fullscreen mode

run the code for the version number

sudo sed -i s/\$releasever/6/g /etc/yum.repos.d/epel-apache-maven.repo
Enter fullscreen mode Exit fullscreen mode

then install maven and git

sudo yum install -y apache-maven git
Enter fullscreen mode Exit fullscreen mode

install java 8

sudo yum install java-1.8.0-devel
Enter fullscreen mode Exit fullscreen mode

run this code to set Java 8 as the default runtime on your ec2

sudo /usr/sbin/alternatives --config java
Enter fullscreen mode Exit fullscreen mode

then you prompted to enter the number 3
run this command to confirm the maven application is installed

mvn --version
Enter fullscreen mode Exit fullscreen mode

Image description

STEP 2
one of the benefit of using a build tool like maven is that you can define the details of the project and any dependencies make configurations. you an also run the test and builds continuously.

in setting up your configurations you configure the settings.xml
and the location is based on this type of installation /etc/maven/settings.xml . updating the server id

Image description

then updating the pom.xml file

Image description

the first four properties in the properties sections just defines the encoding (UTF-8) and the version of java (java 1.8) that maven uses to compile the application code. amongst other instructions the organization name and host url is included

the repository in the distribution management is updated with the (j-frog URL) the id and name used to generate a configuration used at maven to upload the pom.xml file with generated distributed management config.

update application.properties file with JDBC connection string to authenticate with MYSQL.
the should be updated with RDS endpoint and connection credentials

STEP 3
push the code changes to your repository from your local system

git add . && git commit -m "changes with pom and properties file"
git push origin
Enter fullscreen mode Exit fullscreen mode

STEP 4
clone the repository in your server
git clone https://github.com/A-LPHARM/javalogin-app.git && cd javalogin-app

STEP 5
build the source code using the maven arguments

mvn deploy 
mvn sonar:sonar
Enter fullscreen mode Exit fullscreen mode

Image description

STAGE 2
step 1
SETTING UP TOMCAT SERVER
Create your ec2 instance and with the requirements t2-micro
and the security group 8080. using the script to build the tomcat application

sudo yum update -y
cd /opt
sudo yum install git wget unzip tree -y

#The java version being install here is a function of the tomcat version we are going to install
sudo yum install java-1.8.0-openjdk-devel -y

# Download tomcat software and extract it.
sudo wget https://dlcdn.apache.org/tomcat/tomcat-9/v9.0.84/bin/apache-tomcat-9.0.84.tar.gz
sudo tar -xvf apache-tomcat-9.0.84.tar.gz
sudo rm apache-tomcat-9.0.84.tar.gz
sudo mv apache-tomcat-9.0.84 tomcat
sudo chmod 777 -R /opt/tomcat
sudo sh /opt/tomcat/bin/startup.sh

# create a soft link to start and stop tomcat
sudo ln -s /opt/tomcat/bin/startup.sh /usr/bin/starttomcat
sudo ln -s /opt/tomcat/bin/shutdown.sh /usr/bin/stoptomcat
sudo starttomcat
Enter fullscreen mode Exit fullscreen mode

next
run chmod +x tomcat.sh

then
./tomcat.sh

STEP 2
To configure the tomcat server you comment out the words
vi /opt/tomcat/webapps/manager/META-INF/context.xml
then
comment out

<!--
  <Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" />
-->
Enter fullscreen mode Exit fullscreen mode

Image description

STEP 3
add tomcat users
first
vi /opt/tomcat/conf/tomcat-users.xml
then go to the last line and add user before closing tag

<user username="henriksin" password="admin123" roles="manager-gui,admin-gui"/>
<user username="henriksin" password="admin123" roles="manager-gui,admin-gui,manager-script"/>
Enter fullscreen mode Exit fullscreen mode

copy the java web application from the Jfrog central repository to the tomcat server path /opt/tomcat/webapps/
first
cd /opt/tomcat/webapp
then

then wget -o --user=admin --password=admin123  http://54.173.150.66:8081/repository/henryrepo/com/devopsrealtime/dptweb/1.0/dptweb-1.0.war
Enter fullscreen mode Exit fullscreen mode

STAGE 3

STEP 1
deploy your Nginx on ec2 instance

install and configure the nginx on your Ec2 Instance

sudo yum install -y nginx

sudo systemctl start nginx 

sudo systemctl enable nginx 

sudo systemctl status nginx
Enter fullscreen mode Exit fullscreen mode

Image description

step 2
Configure the Tomcat to work with the Nginx

navigate to the Nginx configuration directory
cd /etc/nginx

then

cp nginx.conf nginx_old
Enter fullscreen mode Exit fullscreen mode

Create a backup of the existing Nginx configuration

vi /etc/nginx/nginx.conf
Enter fullscreen mode Exit fullscreen mode

Image description

and edit the file
this is the place where we configure our local tomcat instance
add
...
http {
server {
listen 8080;
location /webapp/ {
proxy_pass http://56.173.245.100:8080/dptweb-1.0/;
}
}
}
……

then restart the service

sudo systemctl restart nginx

sudo systemctl status nginx
Enter fullscreen mode Exit fullscreen mode

TESTING THE SERVER

access the server through the web

with the provided ip http://56.173.245.100
you should now observe the java web application is working seamlessly.

following these steps, you have successfully deployed a java web application to a tomcat server and configured Nginx as a reverse proxy on an aws ec2 instance.

Top comments (0)