首先安装 JDK  (参考​​ Centos 6.5 安装java及设置默认jdk版本​​)


apache-tomcat-8.5.9.zip  

下载地址:​​http://tomcat.apache.org/download-80.cgi​


作为进程启动时,需要下载 commons-daemon

下载地址:​​ https://www.apache.org/dist/commons/daemon/source/​


目录配置

cd /root
unzip apache-tomcat-8.5.9.zip
mkdir -p /usr/local/java
mv apache-tomcat-8.5.9 /usr/local/java/tomcat
cd /usr/local/java/tomcat
chmod 777 bin/*


配置环境变量

echo "export TOMCAT_HOME=/usr/local/java/tomcat" >> /etc/profile
echo "export CATALINA_HOME=/usr/local/java/tomcat" >> /etc/profile
echo "export CATALINA_BASE=/usr/local/java/tomcat" >> /etc/profile
source /etc/profile
echo $TOMCAT_HOME
echo $CATALINA_HOME
echo $CATALINA_BASE


Tomcat 启动/关闭方法

./bin/startup.sh
./bin/shutdown.sh


添加防火墙规则

shell> vi /etc/sysconfig/iptables
-A INPUT -p tcp -m state --state NEW -m tcp --dport 8080 -j ACCEPT

shell> service iptables restart


基本完成!

此时可打开浏览器访问 ​http://localhost:8080​ 即可打开 tomcat 默认页面。


现将 tomcat 添加到服务启动

shell> ln -s /usr/local/java/tomcat/bin/daemon.sh /etc/init.d/tomcat
shell> /etc/init.d/tomcat start


启动,出现错误

./daemon.sh: line 196: /usr/local/java/tomcat/bin/jsvc: No such file or directory


解决 ,安装 commons-daemon (下载地址:​​https://www.apache.org/dist/commons/daemon/source/​​ )

tar zxvf commons-daemon-1.0.15-native-src.tar.gz
mv commons-daemon-1.0.15-native-src /usr/local/java/
cd /usr/local/java/commons-daemon-1.0.15-native-src/unix
./configure
make
#生成的jsvc 拷贝到tomcat/bin 目录
cp jsvc /usr/local/java/tomcat/bin/


辑daemon.sh添加以下内容

shell> vi /usr/local/java/tomcat/bin/daemon.sh
#!/bin/sh
# chkconfig: 2345 10 90
# description: Start up the Tomcat servlet engine.
TOMCAT_USER=root
JAVA_HOME=/usr/local/java/jdk1.8.0_111
CATALINA_HOME=/usr/local/java/tomcat
CATALINA_BASE=/usr/local/java/tomcat


启动/关闭服务,正常

/etc/init.d/tomcat start
/etc/init.d/tomcat status
/etc/init.d/tomcat stop
service tomcat stop
service tomcat start


服务自启动(文件 daemon.sh 加上chkconfig和description)

chkconfig --add tomcat
chkconfig tomcat on


更改默认端口(8080)

shell> vi /usr/local/java/tomcat/conf/server.xml 
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />


IP首页默认tomcat地址改为项目首页(在 Host 标签内)

shell> vi /usr/local/java/tomcat/conf/server.xml
<Host>
<Context path="" docBase="/usr/local/java/tomcat/webapps/projeact/" debug="0" reloadable="true" />
</Host>