Linux(Centos7) 配置 Tomcat 开机自启
1.解压tomcat
去官网下载好源码压缩包之后,解压到自己的路径下,一般是 /usr/local/ 目录下。
2.添加环境变量
vim打开 /etc/profile 文件,在末尾添加:
export CATALINA_HOME=/usr/local/tomcat
export CLASSPATH=.:${JAVA_HOME}/lib:${CATALINA_HOME}/lib
export PATH=$PATH:${CATALINA_HOME}/bin
注意更改你的 tomcat 安装路径
3.添加服务脚本
在 /usr/lib/systemd/system/ 路径下,新建 tomcat.service文件,写入如下代码:
[Unit]
Description=Tomcat
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/opt/softWare/apache-tomcat-7.0.57/tomcat.pid
ExecStart=/opt/softWare/apache-tomcat-7.0.57/bin/startup.sh
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
4.启动服务
systemctl daemon-reload
systemctl start tomcat.service
5.异常处理
如果报以下异常
# systemctl start tomcat.service
Job for tomcat.service failed because the control process exited with error code.
See "systemctl status tomcat.service" and "journalctl -xe" for details.
并且经过检查$JAVA_HOME
和$JRE_HOME
都是正常的。
解决办法
在tomcat/bin/catalina.sh
中添加jdk环境:
export JAVA_HOME=/usr/local/jdk1.8.0_281/
export JRE_HOME=/usr/local/jdk1.8.0_281/jre
6.启动tomcat.service
# systemctl start tomcat.service 启动tomcat
# systemctl status tomcat.service 启动并查看详细启动信息
# systemctl restart tomcat.service 重启tomcat
# systemctl stop tomcat.service 关闭tomcat
# systemctl enable tomcat.service 开机自启
# systemctl disable tomcat.service 禁用开机自启