Linux

CentOS 7 安装tomcat9并把tomcat做成服务

2018-10-22  本文已影响0人  思念_似水流年

1、确认机型:

[root@ln tomcat]# uname -a

Linux ln 3.10.0-862.14.4.el7.x86_64 #1 SMP Wed Sep 26 15:12:11 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

2、确保安装了 tomcat 运行所需要的JDK:

[root@ln tomcat]# java -version

java version "1.8.0_191"

Java(TM) SE Runtime Environment (build 1.8.0_191-b12)

Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)

3、下载 tomcat 的安装包并上传到 Linux 服务器上:

apache-tomcat-9.0.12.tar.gz

4、切换到安装目录后,下载好后解压

tar -zxvf apache-tomcat-9.0.12.tar.gz

5、创建一个用户并设置适当的所有权:

[root@ln system]# useradd -rs /bin/false tomcat

[root@ln system]# chown -R tomcat.tomcat /richstonedt/tomcat/tomcat9

6、修改 tomcat 的catalina.sh 文件,增加 tomcat.pid 的配置:

image.png

7、创建一个系统初始化文件以便 start、restart、stop tomcat:

[root@ln tomcat9]# vi /etc/systemd/system/tomcat9.service

copy 下面的内容:

[Unit]

Description=Apache Tomcat Web Application Container

After=syslog.target network.target

[Service]

Type=forking

Environment=JAVA_HOME=/usr/java/jdk1.8.0_191

Environment=CATALINA_HOME=/richstonedt/tomcat/tomcat9

Environment=CATALINA_BASE=/richstonedt/tomcat/tomcat9

Environment='CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC'

Environment='JAVA_OPTS=-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom'

Environment=CATALINA_PID=/richstonedt/tomcat/tomcat9/tomcat.pid

ExecStart=/richstonedt/tomcat/tomcat9/bin/startup.sh

ExecStop=/richstonedt/tomcat/tomcat9/bin/shutdown.sh

ExecReload=/bin/kill -s HUP $MAINPID

PrivateTmp=true

User=tomcat

Group=tomcat

[Install]

WantedBy=multi-user.target

8、重载所有修改过的配置文件:

[root@ln tomcat9]# systemctl start tomcat9

[root@ln tomcat9]# systemctl status tomcat9.service

● tomcat9.service - Apache Tomcat Web Application Container

  Loaded: loaded (/etc/systemd/system/tomcat9.service; enabled; vendor preset: disabled)

  Active: active (running) since Sun 2018-10-21 15:45:22 EDT; 27s ago

  Process: 2984 ExecStart=/richstonedt/tomcat/tomcat9/bin/startup.sh (code=exited, status=0/SUCCESS)

Main PID: 2992 (java)

  CGroup: /system.slice/tomcat9.service

          └─2992 /usr/java/jdk1.8.0_191/bin/java -Djava.util.logging.config.file=/richstonedt/tomcat/tomcat9/conf/lo...

Oct 21 15:45:22 ln systemd[1]: Starting Apache Tomcat Web Application Container...

Oct 21 15:45:22 ln systemd[1]: Started Apache Tomcat Web Application Container.

9、查看 8080 端口是否处于监听状态:

[root@ln tomcat9]# lsof -i:8080

COMMAND  PID  USER  FD  TYPE DEVICE SIZE/OFF NODE NAME

java    2992 tomcat  49u  IPv6  49384      0t0  TCP *:webcache (LISTEN)

10、访问 tomcat 页面,成功:

image.png

踩过的坑:

1、不修改 catalina.sh 文件添加 tomcat.pid 配置:

    由于在 tomcat9.service 中,找不到 tomcat.pid 的相关配置信息,导致启动不成功

2、没有设置 tomcat9.service 的执行权限,导致启动不成功:

    需要设置可执行权限后,才能启动成功:

    chmod +x tomcat9.service
上一篇下一篇

猜你喜欢

热点阅读