192.168.10.10
192.168.10.20
192.168.10.30
location ~ \.jsp$ {proxy_pass http://192.168.10.50:8080;} location ~ \.(jsp|html)$ {root /usr/share/nginx/html;}
192.168.10.40和192.168.10.50用脚本完成搭建此处安装附上脚本:
#!/bin/bash# 定义变量 JDK_PACKAGE="jdk-8u201-linux-x64.rpm" JDK_INSTALL_PATH="/usr/java/jdk1.8.0_201-amd64"TOMCAT_PACKAGE="apache-tomcat-9.0.16.tar.gz" TOMCAT_INSTALL_PATH="/usr/local/tomcat" TOMCAT_USER="tomcat" TOMCAT_GROUP="tomcat"# Function to install JDK install_jdk() {if [ -e "$JDK_PACKAGE" ]; thenrpm -ivh "$JDK_PACKAGE" &> /dev/nullif [ $? -eq 0 ]; thenecho -e "E[1;32m JDK 安装成功 E[0m"elseecho -e "E[1;31m JDK 安装失败 E[0m"fielseecho "无 JDK 包"fi }# Function to modify environment variables setup_environment() {echo " export JAVA_HOME=$JDK_INSTALL_PATH export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar export PATH=$JAVA_HOME/bin:$PATH" >> /etc/profilesource /etc/profile }# Function to install Tomcat install_tomcat() {if [ -e "$TOMCAT_PACKAGE" ]; thentar zxvf "$TOMCAT_PACKAGE" &> /dev/nullcp -r $(basename "$TOMCAT_PACKAGE" .tar.gz) "$TOMCAT_INSTALL_PATH"useradd -s /sbin/nologin "$TOMCAT_USER"chown "$TOMCAT_USER":"$TOMCAT_GROUP" "$TOMCAT_INSTALL_PATH" -Rcat > /usr/lib/systemd/system/tomcat.service <<EOF [Unit] Description=Tomcat After=syslog.target network.target[Service] Type=forking ExecStart=$TOMCAT_INSTALL_PATH/bin/startup.sh ExecStop=$TOMCAT_INSTALL_PATH/bin/shutdown.sh RestartSec=3 PrivateTmp=true User=$TOMCAT_USER Group=$TOMCAT_GROUP[Install] WantedBy=multi-user.target EOFsystemctl daemon-reloadsystemctl start tomcatif systemctl status tomcat &> /dev/null; thenecho -e "E[1;32m Tomcat 启动成功 E[0m"elseecho -e "E[1;31m Tomcat 启动失败 E[0m"fielseecho "无 Tomcat 安装包"fi }# Main script execution install_jdk setup_environment java -version install_tomcat# Create symbolic links for Tomcat binary files ln -s $TOMCAT_INSTALL_PATH/bin/* /usr/bin# 检查 JDK 版本 echo -e "E[1;36m 检查 JDK 版本: E[0m" java -version# 检查 Tomcat 版本 echo -e "E[1;36m 检查 Tomcat 版本: E[0m" cat $TOMCAT_INSTALL_PATH/RELEASE-NOTES | grep "Apache Tomcat Version" # 获取本机 IP 地址 IP_ADDRESS=$(hostname -I | awk '{print $1}')# 将 IP 地址写入到 index.jsp 文件 echo $IP_ADDRESS > /usr/local/tomcat/webapps/ROOT/index.jspecho "IP 地址已写入到 /usr/local/tomcat/webapps/ROOT/index.jsp 文件中"
192.168.10.40
192.168.10.50
结果验证: