因CentOS7与6在系统上,变化较大,所以在之前的文章中讲到的使用Tomcat7开机自启动的方式在CentOS7是是无法使用的,所以这篇文章的目的是如何在CentOS7上将Tomcat7设置为开机自启动。

安装JAVA环境

  1. [root@7 ~]# curl -LO -H "Cookie: oraclelicense=accept-securebackup-cookie" \
  2. "http://download.oracle.com/otn-pub/java/jdk/7u75-b13/jdk-7u75-linux-x64.rpm"
  3. [root@7 ~]# rpm -Uvh jdk-7u75-linux-x64.rpm
  4. Preparing... ########################################### [100%]
  5. 1:jdk ########################################### [100%]
  6. Unpacking JAR files...
  7. rt.jar...
  8. jsse.jar...
  9. charsets.jar...
  10. tools.jar...
  11. localedata.jar...
  12. jfxrt.jar...
  13. [root@7 ~]# vi /etc/profile
  14. # add follows to the end
  15. export JAVA_HOME=/usr/java/default
  16. export PATH=$PATH:$JAVA_HOME/bin
  17. export CLASSPATH=.:$JAVA_HOME/jre/lib:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar
  18. [root@7 ~]# source /etc/profile

安装Tomcat7

  1. [root@7 ~]# wget http://ftp.riken.jp/net/apache/tomcat/tomcat-7/v7.0.77/bin/apache-tomcat-7.0.77.tar.gz
  2. [root@7 ~]# tar zxvf apache-tomcat-7.0.77.tar.gz
  3. [root@7 ~]# mv apache-tomcat-7.0.77 /usr/tomcat7
  4. [root@7 ~]# useradd -M -d /usr/tomcat7 tomcat7
  5. [root@7 ~]# chown -R tomcat7. /usr/tomcat7

创建开机自启动脚本

  1. [root@7 ~]# cat /usr/lib/systemd/system/tomcat7.service
  2. # create new
  3. [Unit]
  4. Description=Apache Tomcat 7
  5. After=network.target
  6. [Service]
  7. Type=oneshot
  8. ExecStart=/usr/tomcat7/bin/startup.sh
  9. ExecStop=/usr/tomcat7/bin/shutdown.sh
  10. RemainAfterExit=yes
  11. User=tomcat7
  12. Group=tomcat7
  13. [Install]
  14. WantedBy=multi-user.target

启动Tomcat7

  1. [root@7 ~]# systemctl start tomcat7.service
  2. [root@7 ~]# systemctl enable tomcat7.service