Search code examples
javatomcatsystemdrhelrhel8

Systemd Script for creating Apache Tomcat Service in RHEL 8


Starting with version 8.0, Red Hat Enterprise Linux (RHEL) no longer provides any version of the Apache Tomcat JAVA webserver/servlet as part of the RHEL distribution.[1]

Therefore, we have to install Tomcat via WAR file in the RHEL systems. The problem which arises is that it becomes difficult to start, stop or restart the Tomcat service as there is to service file installed, through which we could have easily used the command service tomcat start to start the service.

But there is a way through which we can create this service manually by writing a Systemd script. By placing this script in the /etc/systemd/system/ directory, we can use the service commands for managing the Tomcat Service.

Please share the Tomcat Service Creation Script.


Solution

  • This the Service Creation File. Copy and paste this file in the /etc/systemd/system/ directory.

    The name of the file should be tomcat.service

    [Unit]
    Description=Apache Tomcat Web Application Container
    Wants=network.target
    After=network.target
    
    [Service]
    Type=forking
    
    Environment=JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.292.b10-0.el8_2.x86_64/jre
    
    Environment=CATALINA_PID={{ tomcat_dir }}/tomcat/temp/tomcat.pid
    Environment=CATALINA_HOME={{ tomcat_dir }}/tomcat
    Environment='CATALINA_OPTS=-Xms512M -Xmx1G -Djava.net.preferIPv4Stack=true'
    Environment='JAVA_OPTS=-Djava.awt.headless=true'
    
    ExecStart={{ tomcat_dir }}/tomcat/bin/startup.sh
    ExecStop={{ tomcat_dir }}/tomcat/bin/shutdown.sh
    SuccessExitStatus=143
    
    RestartSec=10
    Restart=always
    
    [Install]
    WantedBy=multi-user.target
    

    Now you can easily manage the Tomcat Service with the Systemd Commands, even in RHEL8.