CentOS下把Tomcat安装配置成系统服务方便重启、开机启动

2020-12-27 1384点热度 0人点赞 0条评论

Tomcat从官网下载后,是个压缩包,解压后如果没有特别的设置,启动和重启Tomcat的时候需要到Tomcat目录下的bin下启动,也不方便弄成开机启动,安装成服务后,就都可以实现了。

  1. 安装OracleJDK 或者OpenJDK
    # download rpm from: https://www.oracle.com/java/technologies/javase-downloads.html
    rpm -ivh jdk-11.0.9_linux-x64_bin.rpm
    
    #check java version
    java -version
  2. 添加tomcat用户
    sudo groupadd tomcat
    sudo useradd -M -s /bin/bash -g tomcat -d /home/tomcat tomcat

    可根据实际情况看tomcat是否需要可以登录,如果不需要登录,执行usermod tomcat -s /sbin/nologin

  3. 下载Tomcat http://tomcat.apache.org/
    cd /opt
    wget https://downloads.apache.org/tomcat/tomcat-9/v9.0.40/bin/apache-tomcat-9.0.40.tar.gz
    tar zxvf apache-tomcat-9.0.40.tar.gz
    ln -s apache-tomcat-9.0.40 tomcat
    
    #change permission
    chmod +x /opt/apache-tomcat-9.0.40/bin/*.sh
    chown -R tomcat: /opt/apache-tomcat-9.0.40
    chown -R tomcat: /opt/tomcat
    #把tomcat/work下的目录权限改成777,发现有时候tomcat起不来,报错:java.lang.IllegalStateException: No output folder
    #改了如下权限就好了
    chmod -R 777 /opt/tomcat/work
  4. 安装服务相关:
    编辑文件 vim /etc/systemd/system/tomcat.service

    [Unit]
    Description=Tomcat 9 servlet container
    After=network.target
    
    [Service]
    Type=forking
    
    User=tomcat
    Group=tomcat
    
    Environment="JAVA_HOME=/usr/lib/jvm/jre"
    Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom"
    
    Environment="CATALINA_BASE=/opt/tomcat"
    Environment="CATALINA_HOME=/opt/tomcat"
    Environment="CATALINA_PID=/opt/tomcat/temp/tomcat.pid"
    Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"
    
    ExecStart=/opt/tomcat/bin/startup.sh
    ExecStop=/opt/tomcat/bin/shutdown.sh
    
    [Install]
    WantedBy=multi-user.target
    
    
    检查下以上文件内容,里面的变量和自己的系统是否一致,
    如果Oracle的JDK可能是:
    JAVA_HOME=/usr/java/default
    CATALINA_OPTS的参数根据自己的机器调整Xmx的数据可以是自己机器内存的3/4

    关于tomcat内存的调整,详见:https://blog.terrynow.com/2021/10/10/tomcat-startup-with-memory-options-to-avoid-out-of-memory-error-or-make-full-use-of-memory/

  5. 服务相关操作
    #使新安装的tomcat服务生效
    systemctl daemon-reload
    #开机启动tomcat
    systemctl enable tomcat
    #启动tomcat
    systemctl start tomcat
    systemctl status tomcat
    systemctl restart tomcat

     

admin

这个人很懒,什么都没留下

文章评论

您需要 登录 之后才可以评论