今天有空在阿里云centos7.4上安装并配置tomcat8.5版本,特将安装过程记录下来,以备查阅.
安装tomcat8.5
下载
去tomcat官网找到需要下载版本的链接,我这里找到的链接是http://mirrors.hust.edu.cn/apache/tomcat/tomcat-8/v8.5.29/bin/apache-tomcat-8.5.29.tar.gz
.使用$ pwd
查看当前路径输出/root
,使用wget下载tomcat的tar.gz文件到这个目录:wget http://mirrors.hust.edu.cn/apache/tomcat/tomcat-8/v8.5.29/bin/apache-tomcat-8.5.29.tar.gz
,终端输出
--2018-04-05 14:24:05-- http://mirrors.hust.edu.cn/apache/tomcat/tomcat-8/v8.5.29/bin/apache-tomcat-8.5.29.tar.gz
Resolving mirrors.hust.edu.cn (mirrors.hust.edu.cn)... 202.114.18.160
Connecting to mirrors.hust.edu.cn (mirrors.hust.edu.cn)|202.114.18.160|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 9532698 (9.1M) [application/octet-stream]
Saving to: 'apache-tomcat-8.5.29.tar.gz'
100%[======================================>] 9,532,698 4.07MB/s in 2.2s
2018-04-05 14:24:08 (4.07 MB/s) - 'apache-tomcat-8.5.29.tar.gz' saved [9532698/9532698]
查看下载的文件大小:
$ du -m apache-tomcat-8.5.29.tar.gz
terminal输出
10 apache-tomcat-8.5.29.tar.gz
说明压缩文件大小为10m
解压缩并移动
现在需要把压缩文件解压到目标文件夹.一般用户安装的程序会放到/usr/local目录中,参考Unix目录结构的来历以及CentOS目录结构详解.先在当前目录(/root)下使用$ tar -zvxf apache-tomcat-8.5.29.tar.gz
命令解压,解压后得到一个名为apache-tomcat-8.5.29
的文件夹,现在将该文件夹移动到/usr/local
目录下:
$ mv apache-tomcat-8.5.29 /usr/local
尝试启动
进入/usr/local/apache-tomcat-8.5.29/bin:
$ cd /usr/local/apache-tomcat-8.5.29/bin
执行$ sh startup.sh
输出
Using CATALINA_BASE: /usr/local/apache-tomcat-8.5.29
Using CATALINA_HOME: /usr/local/apache-tomcat-8.5.29
Using CATALINA_TMPDIR: /usr/local/apache-tomcat-8.5.29/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/apache-tomcat-8.5.29/bin/bootstrap.jar:/usr/local/apache-tomcat-8.5.29/bin/tomcat-juli.jar
Tomcat started.
说明tomcat启动成功,此时可以通过8080端口访问tomcat
尝试关闭
执行$ sh shutdown.sh
输出
Using CATALINA_BASE: /usr/local/apache-tomcat-8.5.29
Using CATALINA_HOME: /usr/local/apache-tomcat-8.5.29
Using CATALINA_TMPDIR: /usr/local/apache-tomcat-8.5.29/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/apache-tomcat-8.5.29/bin/bootstrap.jar:/usr/local/apache-tomcat-8.5.29/bin/tomcat-juli.jar
此时无法再通过8080端口访问tomcat.
配置开机自动启动
目前的配置只能每次手动启动,如果经常重启服务器调试的话会比较麻烦,因此配置成开机自动启动.
开机自动启动配置方案
-
/etc/rc.d/rc.local: 把启动命令配置到该文件中.使用这种方法不需要chkconfig
-
/etc/rc.d/init.d或者/etc/init.d: 这2个是等价的,选择任何一个都可以.在目录下创建启动命令文件tomcat,添加适当内容.使用这种方法需要把启动命令文件添加到chkconfig列表,使用--add参数添加.
参考CentOS设置程序开机自启动的方法和CentOS设置开机自启动服务的两种方法以及CentOS开机自动运行程序的脚本
配置开机启动
这里选择第二种方案,即在/etc/init.d/中添加tomcat:
$ cd /etc/init.d
$ vi tomcat
添加
#!/bin/bash
CATALINA_HOME=/usr/local/apache-tomcat-8.5.29
case $1 in
start)
sh $CATALINA_HOME/bin/startup.sh
;;
stop)
sh CATALINA_HOME/bin/shutdown.sh
;;
restart)
sh $CATALINA_HOME/bin/shutdown.sh
sh $CATALINA_HOME/bin/startup.sh
;;
*)
echo “Usage: start|stop|restart”
;;
esac
exit 0
现在给脚本权限:
$ chmod 755 tomcat
脚本服务添加到chkconfig:
chkconfig --add tomcat
结果报错
service tomcat does not support chkconfig
网上找到一个相关问题chkconfig does not support my script,大意是脚本开头要加上chkconfig和description,修改脚本如下:
#!/bin/bash
# description: Tomcat Start Stop Restart
# processname: tomcat
# chkconfig: 2345 20 80
CATALINA_HOME=/usr/local/apache-tomcat-8.5.29
case $1 in
start)
sh $CATALINA_HOME/bin/startup.sh
;;
stop)
sh $CATALINA_HOME/bin/shutdown.sh
;;
restart)
sh $CATALINA_HOME/bin/shutdown.sh
sh $CATALINA_HOME/bin/startup.sh
;;
*)
echo “Usage: start|stop|restart”
;;
esac
exit 0
重新执行chkconfig --add tomcat
成功.
之后执行chkconfig --level 2345 tomcat on
.
检查是否添加成功$ chkconfig --list tomcat
,输出
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
tomcat 0:off 1:off 2:on 3:on 4:on 5:on 6:off
可见已经配置成功.
验证配置
如果配置成功,可以使用命令$ service tomcat start
启动.现在执行该命令,输出:
Using CATALINA_BASE: /usr/local/apache-tomcat-8.5.29
Using CATALINA_HOME: /usr/local/apache-tomcat-8.5.29
Using CATALINA_TMPDIR: /usr/local/apache-tomcat-8.5.29/temp
Using JRE_HOME: /
Using CLASSPATH: /usr/local/apache-tomcat-8.5.29/bin/bootstrap.jar:/usr/local/apache-tomcat-8.5.29/bin/tomcat-juli.jar
Tomcat started.
发现无法访问,同时对比之前发现JRE_HOME竟然从/usr
编程了/
,说明该变量需要显示配置.
在/etc/init.d/tomcat修改为
#!/bin/bash
# description: Tomcat Start Stop Restart
# processname: tomcat
# chkconfig: 2345 20 80
export JAVA_HOME=/usr/java/jdk1.8.0_162/jre
CATALINA_HOME=/usr/local/apache-tomcat-8.5.29
case $1 in
start)
sh $CATALINA_HOME/bin/startup.sh
;;
stop)
sh $CATALINA_HOME/bin/shutdown.sh
;;
restart)
sh $CATALINA_HOME/bin/shutdown.sh
sh $CATALINA_HOME/bin/startup.sh
;;
*)
echo “Usage: start|stop|restart”
;;
esac
exit 0
查看服务端口
使用$ netstat -tln
查看服务的端口:
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:8005 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:8009 0.0.0.0:* LISTEN
tcp6 0 0 :::21 :::* LISTEN
tcp6 0 0 :::3306 :::* LISTEN
其中8005是关闭tomcat时需要使用的端口,8009是tomcat的ajp使用的端口,相关的配置在Tomcat下conf文件夹中的server.xml文件
网友评论