Automate Your Tomcat Server with shell scripting

Automate Your Tomcat Server with shell scripting

Welcome to the blog on installing and configuring Tomcat server!

In this guide, we will walk you through the process of setting up Tomcat server on your CentOS system using a convenient shell script. Tomcat server is a popular choice for hosting Java-based web applications, providing a robust and efficient environment. By following this step-by-step guide, you'll be able to install and configure Tomcat server quickly and effortlessly.

What is Tomcat?

Tomcat, also known as Apache Tomcat, is an open-source web server and servlet container developed by he Apache Software Foundation. It provides a Java-based environment for running Java web applications.

No alt text provided for this image

Why Tomcat?

Tomcat is widely used as a web server and servlet container for Java web applications. It is popular among developers and organisations for several reasons:

  • Java support: Specifically designed for executing Java servlets and JSP, making it ideal for Java web development.
  • Lightweight: Consumes fewer system resources compared to other Java application servers,making it suitable for low-end hardware.
  • Portability: Platform-independent and can run on Windows, Linux, and macOS.
  • Extensibility: Supports Java Servlet API, JSP, and other Java-based technologies for building complex web applications.
  • Community and support: Large and active community providing excellent support and resources.

Benefits of Tomcat

  • Ease of use.
  • Web application deployment
  • Security
  • Scalability
  • Performance.

Challenges of Tomcat

  • Complexity.
  • Limited support for advanced features.
  • Memory consumption

What is Shell scripting?

Shell scripting is writing and executing scripts using a command-line interpreter to automate tasks and perform system administration operations.

How Shell Script helps for automation?

  • Shell scripting automates repetitive or complex tasks.
  • It enables batch processing of files or data sets.
  • Shell scripts automate system configuration and management tasks.
  • Scheduled jobs can be set up using shell scripts.
  • Shell scripts integrate with other command-line tools and utilities.
  • They streamline software deployment processes.

Prerequisites

Before we begin, make sure you have the following prerequisites:

  • A CentOS system
  • Basic knowledge of Linux and Shell scripting.

Shell-Script

#!/usr/bin/bas
# Script to install and configure Tomcat server

# Step 1: Updating and Installing Dependencies
if ! (yum update -y && yum install wget -y); then
    echo "Failed to update or install wget. Please check your system configuration." >&2
    exit 1
fi

# Step 2: Installing Java
if ! yum install fontconfig java-11-openjdk -y &> /dev/null; then
    echo "Failed to install Java." >&2
    exit 1
fi

# Step 3: Checking Existing Tomcat Installation
if [[ -d "/opt/tomcat${tom_m_ver}" ]]; then
    read -p "Tomcat is already installed. Do you want to reinstall? [y/n]: " reinstall
    if [[ $reinstall != "y" ]]; then
        echo "Script terminated by user."
        exit 0
    fi
    rm -rf "/opt/tomcat${tom_m_ver}"
fi

# Step 4: Downloading and Installing Tomcat
read -p "Enter Tomcat version to install (e.g., 9.0.48): " req_tom_ver
read -p "Enter the desired port number (default: 8089): " port_number

port_number=${port_number:-8089}

tom_m_ver=$(echo "$req_tom_ver" | cut -c 1)
url="https://dlcdn.apache.org/tomcat/tomcat-${tom_m_ver}/v${req_tom_ver}/bin/apache-tomcat-${req_tom_ver}.tar.gz"

cd /opt
if ! wget "$url" &>/dev/null; then
    echo "Failed to download Tomcat archive. Please check the Tomcat version and your network connection." >&2
    exit 1
fi

if ! tar -xvzf "apache-tomcat-${req_tom_ver}.tar.gz" &>/dev/null; then
    echo "Failed to extract Tomcat archive." >&2
    exit 1
fi

mv "apache-tomcat-${req_tom_ver}" "tomcat${tom_m_ver}"
rm -rf "apache-tomcat-${req_tom_ver}.tar.gz"

# Step 5: Configuring Tomcat
if ! ln -s "/opt/tomcat${tom_m_ver}/bin/startup.sh" "/usr/local/bin/tomcatup"; then
    echo "Failed to create symbolic link for startup.sh." >&2
    exit 1
fi

if ! ln -s "/opt/tomcat${tom_m_ver}/bin/shutdown.sh" "/usr/local/bin/tomcatdown"; then
    echo "Failed to create symbolic link for shutdown.sh." >&2
    exit 1
fi

if ! sed -i "s/8080/$port_number/" "/opt/tomcat${tom_m_ver}/conf/server.xml"; then
    echo "Failed to update server.xml." >&2
    exit 1
fi

if ! sed -i '21s/^/<!-- /' "/opt/tomcat${tom_m_ver}/webapps/host-manager/META-INF/context.xml"; then
    echo "Failed to update host-manager context.xml." >&2
    exit 1
fi

if ! sed -i '22s/$/ -->/' "/opt/tomcat${tom_m_ver}/webapps/host-manager/META-INF/context.xml"; then
    echo "Failed to update host-manager context.xml." >&2
    exit 1
fi

if ! sed -i '21s/^/<!-- /' "/opt/tomcat${tom_m_ver}/webapps/manager/META-INF/context.xml"; then
    echo "Failed to update manager context.xml." >&2
    exit 1
fi

if ! sed -i '22s/$/ -->/' "/opt/tomcat${tom_m_ver}/webapps/manager/META-INF/context.xml"; then
    echo "Failed to update manager context.xml." >&2
    exit 1
fi

line_number=57
file_path="/opt/tomcat${tom_m_ver}/conf/tomcat-users.xml"
user_info="<role rolename=\"admin-gui,manager-gui,manager,admin\"/>
<user username=\"admin\" password=\"tomcat\" roles=\"manager-gui,admin-gui,manager,admin\"/>"

if ! sed -i "${line_number}a\\$user_info" "$file_path"; then
    echo "Failed to update tomcat-users.xml." >&2
    exit 1
fi

# Step 6: Restarting Tomcat
tomcatdown &>/dev/null
tomcatup &>/dev/null

# Get host IP
host_ip=$(hostname -I | awk '{print $1}')

# Print completion message
echo "Tomcat server configuration completed."
echo "Access Tomcat server using the following URL:"
echo "http://${host_ip}:${port_number}"
        


Step 1: Updating and Installing Dependencies

  • Update system packages with yum update -y.
  • Install wget utility with yum install wget -y.
  • Display error message and exit if commands fail.

Step 2: Installing Java

  • Use yum to install fontconfig and java-11-openjdk packages.
  • Display error message and exit if installation fails.

Step 3: Checking Existing Tomcat Installation

  • Check if the /opt/tomcat{version} directory exists.
  • Prompt user to confirm reinstallation if directory exists.
  • Remove existing Tomcat directory if user confirms.

Step 4: Downloading and Installing Tomcat

  • Prompt user for Tomcat version and port number.
  • Construct download URL based on user input.
  • Download Tomcat archive using wget.
  • Extract the archive and rename the directory.
  • Remove downloaded archive.

Step 5: Configuring Tomcat

  • Create symbolic links for startup and shutdown scripts.
  • Use sed command to update specific lines in configuration files.
  • Add HTML comments to context.xml files.
  • Append user and role definitions to tomcat-users.xml.

Step 6: Restarting Tomcat

  • Restart Tomcat using tomcatdown and tomcatup commands.
  • Discard output generated by the commands.
  • These steps provide a concise overview of the actions performed in each step of the script.

Executing the Shell Script

The shell script can be executed by saving it to a file (e.g., tomcat_install.sh) and running it using the bash

bash tomcat_install.sh        


Conclusion

  • This shell script automates the installation and configuration of the Apache Tomcat server. It performs the following tasks:
  • Updates the system and instals necessary dependencies.
  • Install Java if not already installed.
  • Checks if Tomcat is already installed and prompts for reinstallation if necessary.
  • Downloads the specified version of Tomcat from the Apache website.
  • Configures Tomcat by creating symbolic links, updating configuration files, and adding user and role definitions.
  • Restarts Tomcat to apply the changes.
  • By executing this script, users can easily set up a functioning Tomcat server with custom configurations and access it through the provided URL.


I hope this article has provided you with valuable insights into the topic at hand. Whether you're a beginner or an experienced professional, there's always something new to learn and discover in the world. By sharing my knowledge and experience with you, I hope to have sparked your curiosity and inspired you to explore further. Thank you for taking the time to read this article, and I look forward to hearing your thoughts and feedback.

Written by,

Lakshmi Narayanan R


To view or add a comment, sign in

Others also viewed

Explore topics