Press "Enter" to skip to content

linux配置tomcat并配置开机自启(CentOS7)

1.在tomcat的bin目录下创建setenv.sh当catalina.sh启动时会自动调用

# touch /opt/softWare/apache-tomcat-7.0.57/bin/setenv.sh

2.编辑setenv.sh

# vi /opt/softWare/apache-tomcat-7.0.57/bin/setenv.sh

文件内容为:(前两行为tomcat安装地址)
export CATALINA_HOME=/opt/softWare/apache-tomcat-7.0.57
export CATALINA_BASE=/opt/softWare/apache-tomcat-7.0.57
#设置Tomcat的PID文件
CATALINA_PID="$CATALINA_BASE/tomcat.pid"
#添加JVM选项
JAVA_OPTS="-server -XX:PermSize=256M -XX:MaxPermSize=1024m -Xms512M -Xmx1024M -XX:MaxNewSize=256m"

3.添加tomcat.service

# touch /usr/lib/systemd/system/tomcat.service
# vi /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 start tomcat  
Job for tomcat.service failed because the control process exited with error code.
 See "systemctl status tomcat.service" and "journalctl -xe" for details.

遇见问题解决问题此时输入

# systemctl status tomcat.service
● tomcat.service - Tomcat
   Loaded: loaded (/usr/lib/systemd/system/tomcat.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since 五 2018-11-26 17:52:49 CST; 5s ago
  Process: 34944 ExecStop=/bin/kill -s QUIT $MAINPID (code=exited, status=1/FAILURE)
  Process: 34953 ExecStart=/opt/softWare/apache-tomcat-7.0.57/bin/startup.sh (code=exited, status=1/FAILURE)
 Main PID: 34899 (code=exited, status=0/SUCCESS)

12月 16 17:52:49 klmy-460-res01 systemd[1]: Starting Tomcat...
12月 16 17:52:49 klmy-460-res01 startup.sh[34953]: Neither the JAVA_HOME nor the JRE_HOME environment varia...ined
12月 16 17:52:49 klmy-460-res01 startup.sh[34953]: At least one of these environment variable is needed to ...gram
12月 16 17:52:49 klmy-460-res01 systemd[1]: tomcat.service: control process exited, code=exited status=1
12月 16 17:52:49 klmy-460-res01 systemd[1]: Failed to start Tomcat.
12月 16 17:52:49 klmy-460-res01 systemd[1]: Unit tomcat.service entered failed state.
12月 16 17:52:49 klmy-460-res01 systemd[1]: tomcat.service failed.
Hint: Some lines were ellipsized, use -l to show in full.

经过检查$JAVA_HOME$JRE_HOME都是正常的。

解决办法在catalina.sh 中添加:

export JAVA_HOME=/opt/softWare/java/jdk1.8.0_141
export JRE_HOME=/opt/softWare/java/jdk1.8.0_141/jre

5.启动tomcat.service

# systemctl start tomcat.service  启动tomcat
# systemctl status tomcat.service  启动并查看详细启动信息
# systemctl enable tomcat.service 开机自启
# systemctl disable tomcat.service 禁用开机自启

Be First to Comment

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注