Wednesday, February 7, 2024

tomcat config script

 #!/bin/bash


# Variables

TOMCAT_VERSION="9.0.59"  # Adjust to the desired Tomcat version

TOMCAT_INSTALL_DIR="/opt/tomcat"

TOMCAT_USER="tomcat"

TOMCAT_PASSWORD="your_password"  # Change this to a secure password

TOMCAT_MANAGER_USER="admin"

TOMCAT_MANAGER_PASSWORD="your_manager_password"  # Change this to a secure password


# Update and install necessary packages

sudo yum update -y

sudo yum install -y java wget


# Download and extract Tomcat

cd /tmp

wget https://downloads.apache.org/tomcat/tomcat-9/v${TOMCAT_VERSION}/bin/apache-tomcat-${TOMCAT_VERSION}.tar.gz

tar -xf apache-tomcat-${TOMCAT_VERSION}.tar.gz

sudo mv apache-tomcat-${TOMCAT_VERSION} ${TOMCAT_INSTALL_DIR}


# Create Tomcat user and group

sudo groupadd ${TOMCAT_USER}

sudo useradd -g ${TOMCAT_USER} -d ${TOMCAT_INSTALL_DIR} -s /bin/nologin ${TOMCAT_USER}


# Set ownership and permissions

sudo chown -R ${TOMCAT_USER}:${TOMCAT_USER} ${TOMCAT_INSTALL_DIR}

sudo chmod +x ${TOMCAT_INSTALL_DIR}/bin/*.sh


# Configure Tomcat users for Manager and Admin roles

echo -e "  <user username=\"${TOMCAT_MANAGER_USER}\" password=\"${TOMCAT_MANAGER_PASSWORD}\" roles=\"manager-gui,admin-gui\"/>\n" | sudo tee -a ${TOMCAT_INSTALL_DIR}/conf/tomcat-users.xml


# Set Tomcat environment variables (optional)

# You may customize this section based on your requirements


# Create systemd service for Tomcat

cat <<EOL | sudo tee /etc/systemd/system/tomcat.service

[Unit]

Description=Apache Tomcat Web Application Container

After=syslog.target network.target


[Service]

Type=forking


Environment=CATALINA_PID=${TOMCAT_INSTALL_DIR}/temp/tomcat.pid

Environment=CATALINA_HOME=${TOMCAT_INSTALL_DIR}

Environment=CATALINA_BASE=${TOMCAT_INSTALL_DIR}


ExecStart=${TOMCAT_INSTALL_DIR}/bin/startup.sh

ExecStop=${TOMCAT_INSTALL_DIR}/bin/shutdown.sh


User=${TOMCAT_USER}

Group=${TOMCAT_USER}


[Install]

WantedBy=multi-user.target

EOL


# Reload systemd and start Tomcat

sudo systemctl daemon-reload

sudo systemctl start tomcat

sudo systemctl enable tomcat


# Open firewall port 8080

sudo firewall-cmd --add-port=8080/tcp --permanent

sudo firewall-cmd --reload


# Cleanup

rm -rf /tmp/apache-tomcat-${TOMCAT_VERSION}.tar.gz


echo "Tomcat installation and configuration completed successfully."

echo "Tomcat Manager URL: http://localhost:8080/manager/html"

echo "Tomcat Admin URL: http://localhost:8080/host-manager/html"


No comments:

Post a Comment

Enable OpenSSH on Windows 11

Step 1: Install OpenSSH Server You can do this via PowerShell (run as Administrator ): Check if it's already available: Get-WindowsCapab...