欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

Linux(CentOS7)安装Tomcat与设置Tomcat为开机启动项(tomcat8为例)

程序员文章站 2022-06-23 20:34:02
安装tomcat 下载tomcat压缩包 tomcat有tomcat7,tomcat8和tomcat9等版本,目前企业使用较多的是tomcat8,所以这里以tomcat8为列 进...

安装tomcat

下载tomcat压缩包

tomcat有tomcat7,tomcat8和tomcat9等版本,目前企业使用较多的是tomcat8,所以这里以tomcat8为列

进入tomcat8下载网址:tomcat8下载网址

点击左侧download下的对应版本,这里我下载的是apache-tomcat-8.5.47.tar.gz,即linux环境的压缩包

tomcat主要有三个安装版本

  • tar.gz:linux环境下的压缩包,免安装
  • windows.zip:windows压缩包,免安装,解压即用,同时注意根据自己电脑是64位系统还是32位系统下载对应的压缩包
  • windows service installer:windows安装包,32位和64位版本的windows系统都适用

安装tomcat

将下载好的apache-tomcat-8.5.47.tar.gz放到指定目录,我这里放到/usr/local/tomcat,如下图所示

进入/usr/local/tomcat目录,解压tomcat压缩包

# 进入/usr/local/tomcat目录
cd /usr/local/tomcat
# 解压tomcat压缩包
tar -zxvf apache-tomcat-8.5.47.tar.gz

启动tomcat

进入tomcat的bin目录,启动tomcat

# 进入tomcat的bin目录,启动tomcat
cd apache-tomcat-8.5.47/bin/
# 启动tomcat
./startup.sh

查看tomcat是否启动成功

# 查看tomcat是否启动成功执行
ps -ef | grep tomcat
# 如果输出如下,说明tomcat安装成功
root  2381  1 11 22:18 pts/0 00:00:02 /usr/local/jdk1.8.0_152/bin/java -djava.util.logging.config.file=/usr/local/tomcat/apache-tomcat-8.5.47/conf/logging.properties -djava.util.logging.manager=org.apache.juli.classloaderlogmanager -djdk.tls.ephemeraldhkeysize=2048 -djava.protocol.handler.pkgs=org.apache.catalina.webresources -dorg.apache.catalina.security.securitylistener.umask=0027 -dignore.endorsed.dirs= -classpath /usr/local/tomcat/apache-tomcat-8.5.47/bin/bootstrap.jar:/usr/local/tomcat/apache-tomcat-8.5.47/bin/tomcat-juli.jar -dcatalina.base=/usr/local/tomcat/apache-tomcat-8.5.47 -dcatalina.home=/usr/local/tomcat/apache-tomcat-8.5.47 -djava.io.tmpdir=/usr/local/tomcat/apache-tomcat-8.5.47/temp org.apache.catalina.startup.bootstrap start
root  2513 29060 0 22:18 pts/0 00:00:00 grep --color=auto tomcat

使用浏览器访问tomcat,地址linux的ip:8080,我这里的ip端口是,如下图说明在linux(centos7)环境安装启动tomcat成功,为自己点一个赞

注意:开放8080端口或者关闭防火墙

附加:开放8080端口或者关闭防火墙,如果是阿里云只能在阿里云控制台配置开放端口

# 开启8080端口
firewall-cmd --zone=public --add-port=8080/tcp --permanent
# 查询端口号8080是否开启
firewall-cmd --query-port=8080/tcp
# 重启防火墙
firewall-cmd --reload
# 查看开放端口列表
firewall-cmd --list-port
# 命令含义
--zone #作用域
--add-port=8080/tcp #添加端口,格式为:端口/通讯协议
--permanent #永久生效,没有此参数重启后失效
# 关闭防火墙
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动

设置tomcat为开机启动项

按照上面的方式启动tomcat,如果我们的虚拟机或者服务器关闭了,重启服务器后tomcat是关闭的,但是我们希望虚拟机或者服务器重启后,tomcat可以自己启动,所以我们需要设置tomcat为开机启动项

创建setenv.sh文件,为tomcat添加启动参数

catalina.sh在执行的时候会调用同级路径下的setenv.sh来设置额外的环境变量,因此在/usr/local/tomcat/apache-tomcat-8.5.47/bin路径下创建setenv.sh文件,内容如下:

# 设置tomcat的pid文件
catalina_pid="$catalina_base/tomcat.pid"
# 添加jvm选项
java_opts="-server -xx:permsize=256m -xx:maxpermsize=1024m -xms512m -xmx1024m -xx:maxnewsize=256m"

/usr/local/tomcat/apache-tomcat-8.5.47/bin/catalina.sh文件开头添加java_home和jre_home,其中/usr/local/jdk1.8.0_152为jdk的安装目录

export java_home=/usr/local/jdk1.8.0_152
export jre_home=/usr/local/jdk1.8.0_152/jre

如果在catalina.sh不配置java_home和jre_home就会报如下的错误

[root@jourwon ~]# systemctl status tomcat
● tomcat.service - tomcat
 loaded: loaded (/usr/lib/systemd/system/tomcat.service; enabled; vendor preset: disabled)
 active: failed (result: exit-code) since mon 2019-10-21 19:54:54 cst; 6s ago
 process: 8746 execstart=/usr/local/tomcat/apache-tomcat-8.5.47/bin/startup.sh (code=exited, status=1/failure)

oct 21 19:54:54 jourwon systemd[1]: starting tomcat...
oct 21 19:54:54 jourwon startup.sh[8746]: neither the java_home nor the jre_...d
oct 21 19:54:54 jourwon startup.sh[8746]: at least one of these environment ...m
oct 21 19:54:54 jourwon systemd[1]: tomcat.service: control process exited,...=1
oct 21 19:54:54 jourwon systemd[1]: failed to start tomcat.
oct 21 19:54:54 jourwon systemd[1]: unit tomcat.service entered failed state.
oct 21 19:54:54 jourwon systemd[1]: tomcat.service failed.
hint: some lines were ellipsized, use -l to show in full.

在/usr/lib/systemd/system路径下添加tomcat.service文件,内容如下:

[unit]
description=tomcat
after=network.target remote-fs.target nss-lookup.target
[service]
type=forking
timeoutsec=0
pidfile=/usr/local/tomcat/apache-tomcat-8.5.47/tomcat.pid
execstart=/usr/local/tomcat/apache-tomcat-8.5.47/bin/startup.sh
execreload=/bin/kill -s hup $mainpid
execstop=/bin/kill -s quit $mainpid
privatetmp=true
[install]
wantedby=multi-user.target

此外service文件修改后需要调用systemctl daemon-reload命令重新加载。

配置timeoutsec=0的目的是让开机启动不处理tomcat启动超时,保证tomcat耗时过长时不会被系统terminating,如果不配置可能出现下面的情况

oct 21 20:26:37 jourwon startup.sh[1634]: existing pid file found during start.
oct 21 20:26:37 jourwon startup.sh[1634]: removing/clearing stale pid file.
oct 21 20:26:37 jourwon startup.sh[1634]: tomcat started.
oct 21 20:26:37 jourwon systemd[1]: pid file /usr/local/tomcat/apache-tomcat-8.5.47/tomcat.pid not readable (yet?) after start.
oct 21 20:26:38 jourwon polkitd[464]: unregistered authentication agent for unix-process:1628:19013 (system bus name :1.23, object path /org/freedesktop/policykit1/authenticationagent, loca
oct 21 20:28:07 jourwon systemd[1]: tomcat.service start operation timed out. terminating.
oct 21 20:28:07 jourwon systemd[1]: failed to start tomcat.

把tomcat加入开机自启动

systemctl enable tomcat.service

重启服务器

reboot

再次连接后,查看服务状态

[root@jourwon ~]# systemctl status tomcat
● tomcat.service - tomcat
 loaded: loaded (/usr/lib/systemd/system/tomcat.service; enabled; vendor preset: disabled)
 active: activating (start) since mon 2019-10-21 20:12:19 cst; 8s ago
 process: 9244 execstart=/usr/local/tomcat/apache-tomcat-8.5.47/bin/startup.sh (code=exited, status=0/success)
 cgroup: /system.slice/tomcat.service
   └─9255 /usr/local/jdk1.8.0_152/jre/bin/java -djava.util.logging.config.file=/usr/local/tomcat/apache-tomcat-8.5.47/conf/logging.properties -djava.util.logging.manager=org.apac...

oct 21 20:12:19 jourwon systemd[1]: starting tomcat...
oct 21 20:12:19 jourwon startup.sh[9244]: existing pid file found during start.
oct 21 20:12:19 jourwon startup.sh[9244]: removing/clearing stale pid file.
oct 21 20:12:19 jourwon startup.sh[9244]: tomcat started.
oct 21 20:12:19 jourwon systemd[1]: pid file /usr/local/tomcat/apache-tomcat-8.5.47/tomcat.pid not readable (yet?) after start.

查看开机启动列表命令

systemctl list-unit-files | grep enabled

查看tomcat是否设置为开机启动项,如果显示为enabled,说明设置成功

参数说明

  • static:表示该服务与其他服务相关联,不能单独设置该服务的启动状态
  • disabled:表示禁止开机启动
  • enabled:表示允许开机启动
[root@jourwon ~]# systemctl list-unit-files | grep tomcat
tomcat.service        enabled

总结

以上所述是小编给大家介绍的linux(centos7)安装tomcat与设置tomcat为开机启动项(tomcat8为例),希望对大家有所帮助