centos7设置tomcat7为系统服务的方法

Posted 未名胡

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了centos7设置tomcat7为系统服务的方法相关的知识,希望对你有一定的参考价值。

 感觉在centos中启动tomcat比较麻烦、要一直cd到目录下面startup.sh才可以,所以网上找到将tomcat作为系统服务,使用systemctl直接启动方法。设置记录如下:

1,准备工作:

JKD:jdk-7u72-linux-x64.gz

Tomcat:apache-tomcat-7.0.70.tar.gz

OS:CentOS Linux release 7.2.1511 (Core) 

2,安装jdk

A,解压jdk在 /usr/ 目录下

B, root用户配置全局环境变量,
vi /etc/profile
追加以下内容

export JAVA_HOME=/usr/jdk1.7.0_72
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar

生效:source /etc/profile

3,安装tomcat7

A,解压tomcat至 /usr/目录下

B,在/usr/lib/systemd/system/目录下新建文件tomcat7.service,内容如下:

[Unit]
Description=Tomcat7
After=syslog.target network.target remote-fs.target nss-lookup.target
   
[Service]
Type=forking
Environment='JAVA_HOME=/usr/jdk1.7.0_72'
Environment='CATALINA_PID=/usr/apache-tomcat-7.0.70/bin/tomcat.pid'
Environment='CATALINA_HOME=/usr/apache-tomcat-7.0.70/'
Environment='CATALINA_BASE=/usr/apache-tomcat-7.0.70/'
Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'
 
WorkingDirectory=/usr/apache-tomcat-7.0.70/
 
ExecStart=/usr/apache-tomcat-7.0.70/bin/startup.sh
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
   
[Install]
WantedBy=multi-user.target

C,设置为开启机启动:systemctl enable tomcat7

4,启停服务

A,启动服务:systemctl start tomcat7

B,停止服务:systemctl stop tomcat7

C,重启服务:systemctl restart tomcat7

检查状态:systemctl status tomcat7

运行的状态:

[root@localhost merrick]# systemctl status tomcat7
● tomcat7.service - Tomcat7
   Loaded: loaded (/usr/lib/systemd/system/tomcat7.service; disabled; vendor preset: disabled)
   Active: active (running) since 四 2016-09-08 11:34:16 CST; 17min ago
  Process: 3231 ExecStop=/bin/kill -s QUIT $MAINPID (code=exited, status=0/SUCCESS)
  Process: 3239 ExecStart=/usr/apache-tomcat-7.0.70/bin/startup.sh (code=exited, status=0/SUCCESS)
 Main PID: 3249 (java)
   CGroup: /system.slice/tomcat7.service
           └─3249 /usr/jdk1.7.0_72/bin/java -Djava.util.logging.config.file=/usr/apache-tomcat-7.0.70//conf/logging.properties -D...

9月 08 11:34:16 localhost.localdomain startup.sh[3239]: Using CATALINA_BASE:   /usr/apache-tomcat-7.0.70/
9月 08 11:34:16 localhost.localdomain startup.sh[3239]: Using CATALINA_HOME:   /usr/apache-tomcat-7.0.70/
9月 08 11:34:16 localhost.localdomain startup.sh[3239]: Using CATALINA_TMPDIR: /usr/apache-tomcat-7.0.70//temp
9月 08 11:34:16 localhost.localdomain startup.sh[3239]: Using JRE_HOME:        /usr/jdk1.7.0_72
9月 08 11:34:16 localhost.localdomain startup.sh[3239]: Using CLASSPATH:       /usr/apache-tomcat-7.0.70//bin/bootstrap.jar...i.jar
9月 08 11:34:16 localhost.localdomain startup.sh[3239]: Using CATALINA_PID:    /usr/apache-tomcat-7.0.70/bin/tomcat.pid
9月 08 11:34:16 localhost.localdomain startup.sh[3239]: Existing PID file found during start.
9月 08 11:34:16 localhost.localdomain startup.sh[3239]: Removing/clearing stale PID file.
9月 08 11:34:16 localhost.localdomain startup.sh[3239]: Tomcat started.
9月 08 11:34:16 localhost.localdomain systemd[1]: Started Tomcat7.
Hint: Some lines were ellipsized, use -l to show in full.

 

5,测试服务

浏览器中输入 : http://ip:8080/

检查服务器能正确响应。

或者终端命令行输入: wget htp://localhost:8080/

正常响应:

[root@localhost merrick]# wget http://localhost:8080/
--2016-09-08 11:54:47--  http://localhost:8080/
正在解析主机 localhost (localhost)... ::1, 127.0.0.1
正在连接 localhost (localhost)|::1|:8080... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:未指定 [text/html]
正在保存至: “index.html”

    [ <=>                                                                                       ] 11,197      --.-K/s 用时 0s      

2016-09-08 11:54:47 (245 MB/s) - “index.html” 已保存 [11197]

 

 

 

 

 

 

 

以上是关于centos7设置tomcat7为系统服务的方法的主要内容,如果未能解决你的问题,请参考以下文章

Centos7.0挂载优盘安装jdk1.7和tomcat7.0

Centos7下添加开机自启动服务和脚本

centos7中设置服务开机自启的两种方法

CentOS7添加自定义系统服务

CentOS7+Nginx设置Systemctl restart nginx.service服务

Centos7设置静态IP后无法上网的解决方法