以下是“在CentOS 6.3中安装与配置Tomcat-7方法”的完整攻略:
安装Java
- 首先,从官网下载Java安装包。在本示例中,我们将操作JDK 8版本:
wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" https://download.oracle.com/otn-pub/java/jdk/8u121-b13/e9e7ea248e2c4826b92b3f075a80e441/jdk-8u121-linux-x64.tar.gz
- 下载完成后,将其解压至 /usr/local目录下:
tar zxvf jdk-8u121-linux-x64.tar.gz -C /usr/local/
- 配置环境变量:
export JAVA_HOME=/usr/local/jdk-8u121
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
- 验证Java是否安装成功:
java -version
安装Tomcat
- 下载并解压Tomcat安装包。在本示例中,我们将操作Tomcat 7版本:
wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-7/v7.0.107/bin/apache-tomcat-7.0.107.tar.gz
tar zxvf apache-tomcat-7.0.107.tar.gz -C /usr/local/
- 配置Tomcat用户:
useradd -M -s /sbin/nologin tomcat
- 修改Tomcat目录的权限:
chown -R tomcat:tomcat /usr/local/apache-tomcat-7.0.107
chmod -R 755 /usr/local/apache-tomcat-7.0.107
- 编辑Tomcat的init.d脚本(/etc/init.d/tomcat),并输入以下内容:
#!/bin/bash
# Tomcat init script
# Source function library.
if [ -f /etc/init.d/functions ]; then
. /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ]; then
. /etc/rc.d/init.d/functions
else
exit 0
fi
RETVAL=$?
export CATALINA_HOME=/usr/local/apache-tomcat-7.0.107:/usr/local/apache-tomcat-7.0.107/lib
export CATALINA_BASE=/usr/local/apache-tomcat-7.0.107
export JAVA_HOME=/usr/local/jdk-8u121/
start() {
echo -n "Starting Tomcat: "
if [ `whoami` = "tomcat" ]; then
/usr/local/apache-tomcat-7.0.107/bin/startup.sh >/dev/null
else
su tomcat -c "/usr/local/apache-tomcat-7.0.107/bin/startup.sh >/dev/null"
fi
RETVAL=$?
if [ $RETVAL -eq 0 ]; then
echo "Tomcat started."
else
echo "Tomcat failed to start."
fi
return $RETVAL
}
stop() {
echo -n "Stopping Tomcat: "
if [ `whoami` = "tomcat" ]; then
/usr/local/apache-tomcat-7.0.107/bin/shutdown.sh >/dev/null
else
su tomcat -c "/usr/local/apache-tomcat-7.0.107/bin/shutdown.sh >/dev/null"
fi
RETVAL=$?
if [ $RETVAL -eq 0 ]; then
echo "Tomcat stopped."
else
echo "Tomcat failed to Stop."
fi
return $RETVAL
}
restart() {
stop
sleep 2
start
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo $"Usage: $0 {start|stop|restart}"
RETVAL=1
esac
exit $RETVAL
注意更改脚本中的路径。
- 设为开机自启动:
chkconfig --add tomcat
chkconfig tomcat on
- 启动Tomcat:
service tomcat start
- 访问Tomcat默认页面:
http://localhost:8080/
示例
示例1:安装并启动Tomcat
- 安装Java:
# 按照前文中的步骤安装Java
- 下载并解压Tomcat安装包:
wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-7/v7.0.107/bin/apache-tomcat-7.0.107.tar.gz
tar zxvf apache-tomcat-7.0.107.tar.gz -C /usr/local/
- 设为开机自启动:
chkconfig --add tomcat
chkconfig tomcat on
- 启动Tomcat:
service tomcat start
示例2:停止Tomcat
- 停止Tomcat:
service tomcat stop
以上就是“在CentOS 6.3中安装与配置Tomcat-7方法”的完整攻略。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:在CentOS 6.3中安装与配置Tomcat-7方法 - Python技术站