100-Days-Of-DevOps-Challenge-KodeKloud

Install and Setup Tomcat Server

The Nautilus application development team recently finished the beta version of one of their Java-based applications, which they are planning to deploy on one of the app servers in Stratos DC. After an internal team meeting, they have decided to use the tomcat application server. Based on the requirements mentioned below complete the task:

Deploy it on this tomcat server and make sure the webpage works directly on base URL i.e curl http://stapp01:8086

Steps

  1. Install JVM

     sudo yum install java-1.8.0-openjdk-devel -y
    
  2. Create tomcat user

     sudo groupadd tomcat
     sudo useradd -M -U -d /opt/tomcat -s /bin/nologin -g tomcat tomcat
    
  3. Create /opt/tomcat directory

     sudo mkdir /opt/tomcat
    
  4. Download tomcat and Extract

     wget https://archive.apache.org/dist/tomcat/tomcat-9/v9.0.80/bin/apache-tomcat-9.0.80.tar.gz
    
     sudo tar -xf apache-tomcat-9.0.80.tar.gz -C /opt/tomcat --strip-components=1
    
  5. Set Permissions

     sudo chown -R tomcat:tomcat /opt/tomcat
    
  6. Create a systemd service

     sudo vi /etc/systemd/system/tomcat.service
    

    Copy and paste the following lines:

     [Unit]
     Description=Apache Tomcat Web Application Container
     After=network.target
    
     [Service]
     Type=forking
     User=tomcat
     Group=tomcat
     Environment="JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.362.b09-4.el9.x86_64"
     Environment="CATALINA_PID=/opt/tomcat/temp/tomcat.pid"
     Environment="CATALINA_HOME=/opt/tomcat"
     Environment="CATALINA_BASE=/opt/tomcat"
     ExecStart=/opt/tomcat/bin/startup.sh
     ExecStop=/opt/tomcat/bin/shutdown.sh
     RestartSec=10
     Restart=always
    
     [Install]
     WantedBy=multi-user.target
    
  7. Start daemon service

     sudo systemctl daemon-reload
     sudo systemctl enable tomcat
     sudo systemctl start tomcat
    
  8. Setup firewall, you can skip this step

     sudo firewall-cmd --permanent --zone=public --add-port=8080/tcp
     sudo firewall-cmd --reload
    
  9. Test you can access using curl http://stapp01:8080. Now lets modify the port and deploy ROOT.war

  10. To modify the port edit the /opt/tomcat/conf/server.xml and change port 8080 to 8086

    vi /opt/tomcat/conf/server.xml
    
     <Connector port="8086" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443"
               maxParameterCount="1000"
               />
    
  11. Take backup of /opt/tomcat/webapps/ROOT

    cd /opt/tomcat/webapps
    mv ROOT ROOT.bak
    
  12. Copy /tmp/ROOT.war from jump host server to app server.

    scp /tmp/ROOT.war tony@stapp01:/home/tony/
    
  13. Unzip the ROOT.war

    unzip /home/tony/ROOT.war -d /opt/tomcat/webapps/ROOT
    
  14. Restart tomcat service

    systemctl restart tomcat
    
  15. Test

    curl http://stapp01:8086
    
    <!DOCTYPE html>
    <!--
    To change this license header, choose License Headers in Project Properties.
    To change this template file, choose Tools | Templates
    and open the template in the editor.
    -->
    <html>
        <head>
            <title>SampleWebApp</title>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
        </head>
        <body>
            <h2>Welcome to xFusionCorp Industries!</h2>
            <br>
            
        </body>
    </html>
    

Good to Know?

Apache Tomcat Fundamentals

Java Application Deployment

Tomcat Configuration

Security Best Practices