Skip to content

Instantly share code, notes, and snippets.

@ovichiro
Last active March 13, 2025 13:12
Show Gist options
  • Save ovichiro/d24c53ce4902ef41cc208efeadd596b6 to your computer and use it in GitHub Desktop.
Save ovichiro/d24c53ce4902ef41cc208efeadd596b6 to your computer and use it in GitHub Desktop.

Revisions

  1. ovichiro revised this gist Sep 4, 2018. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion tomcat-linux.service.md
    Original file line number Diff line number Diff line change
    @@ -128,7 +128,9 @@ Environment=CATALINA_PID=/opt/apache-tomcat-8.5.6/temp/tomcat.pid
    Environment=CATALINA_HOME=/opt/apache-tomcat-8.5.6
    Environment=CATALINA_BASE=/opt/apache-tomcat-8.5.6
    Environment=CATALINA_OPTS=
    Environment="JAVA_OPTS=-Dfile.encoding=UTF-8 -Dnet.sf.ehcache.skipUpdateCheck=true -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -XX:+UseParNewGC -Xms2g -Xmx4g"
    Environment="JAVA_OPTS=-Dfile.encoding=UTF-8 -Dnet.sf.ehcache.skipUpdateCheck=true \
    -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled \
    -XX:+UseParNewGC -Xms2g -Xmx4g"

    ExecStart=/opt/apache-tomcat-8.5.6/bin/startup.sh
    ExecStop=/bin/kill -15 $MAINPID
  2. ovichiro revised this gist Aug 17, 2018. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion tomcat-linux.service.md
    Original file line number Diff line number Diff line change
    @@ -29,7 +29,8 @@ chown -R tomcat.tomcat /opt/apache-tomcat-8.5.6
    ### Using init.d

    Add `/etc/init.d/tomcat` init script. Notice there are other init scripts in `/etc/init.d/`.
    The script shown below will have a LSB type header to define dependencies and runlevels. Some details here: https://wiki.debian.org/LSBInitScripts
    The script shown below will have a LSB type header to define dependencies and runlevels.
    Some details here: https://wiki.debian.org/LSBInitScripts
    It will start and stop the server as the `tomcat` user, preserving the existing environment variables, by using `su -p -s /bin/sh tomcat ...`.


  3. ovichiro created this gist Aug 17, 2018.
    172 changes: 172 additions & 0 deletions tomcat-linux.service.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,172 @@
    Install Tomcat as a service on Linux
    ======================================

    Download Tomcat from the Apache website.
    Unpack in `/opt/apache-tomcat-x.y.z`. E.g. `/opt/apache-tomcat-8.5.6`.
    You'll need a terminal and root access.


    ## Create Tomcat user with restricted permissions

    This is the user the Tomcat service will run as.

    ```sh
    groupadd tomcat
    useradd -s /sbin/nologin -g tomcat -d /opt/apache-tomcat-8.5.6 tomcat
    passwd tomcat
    ```


    Set the `tomcat` user as the owner of the $CATALINA_HOME folder.

    ```sh
    chown -R tomcat.tomcat /opt/apache-tomcat-8.5.6
    ```


    ## Configure Tomcat to run as a service

    ### Using init.d

    Add `/etc/init.d/tomcat` init script. Notice there are other init scripts in `/etc/init.d/`.
    The script shown below will have a LSB type header to define dependencies and runlevels. Some details here: https://wiki.debian.org/LSBInitScripts
    It will start and stop the server as the `tomcat` user, preserving the existing environment variables, by using `su -p -s /bin/sh tomcat ...`.


    ```sh
    #!/bin/bash

    ### BEGIN INIT INFO
    # Provides: tomcat
    # Required-Start: $network $remote_fs $syslog
    # Required-Stop: $network $remote_fs $syslog
    # Default-Start: 2 3 4 5
    # Default-Stop: 0 1 6
    # Short-Description: Start Tomcat at boot time
    # Description: Start Tomcat at boot time
    ### END INIT INFO

    export JAVA_HOME=/usr/lib/jvm/jre
    export CATALINA_HOME=/opt/apache-tomcat-8.5.6
    export JAVA_OPTS="-Xms250m -Xmx1024m"

    RETVAL=$?
    case $1 in
    start)
    if [ -f $CATALINA_HOME/bin/startup.sh ];
    then
    echo $"Starting Tomcat"
    su -p -s /bin/sh tomcat $CATALINA_HOME/bin/startup.sh
    fi
    ;;
    stop)
    if [ -f $CATALINA_HOME/bin/shutdown.sh ];
    then
    echo $"Stopping Tomcat"
    su -p -s /bin/sh tomcat $CATALINA_HOME/bin/shutdown.sh
    fi
    ;;
    *)
    echo $"Usage: $0 {start|stop}"
    exit 1
    ;;
    esac

    exit $RETVAL
    ```

    Make the script executable:

    ```sh
    chmod ug+x /etc/init.d/tomcat
    ```

    Configure the system to run the script at boot:

    ```sh
    sudo update-rc.d tomcat defaults # Debian, Ubuntu
    sudo chkconfig --add tomcat # Red Hat & co.
    ```

    If you want to remove the service

    ```sh
    sudo update-rc.d -f tomcat remove # Debian, Ubuntu
    ```

    To start/stop the script manually:

    ```sh
    service tomcat [start | stop]
    ```

    Or the old-fashioned way (Ubuntu):

    ```sh
    /etc/init.d/tomcat [start | stop]
    ```


    ### Using systemd

    Add `/etc/systemd/system/tomcat.service` init script:

    ```sh
    # Systemd unit file for tomcat
    [Unit]
    Description=Apache Tomcat Web Application Container
    After=syslog.target network.target

    [Service]
    Type=forking
    User=tomcat
    Group=tomcat

    Environment=JAVA_HOME=/usr/lib/jvm/jre
    Environment=CATALINA_PID=/opt/apache-tomcat-8.5.6/temp/tomcat.pid
    Environment=CATALINA_HOME=/opt/apache-tomcat-8.5.6
    Environment=CATALINA_BASE=/opt/apache-tomcat-8.5.6
    Environment=CATALINA_OPTS=
    Environment="JAVA_OPTS=-Dfile.encoding=UTF-8 -Dnet.sf.ehcache.skipUpdateCheck=true -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -XX:+UseParNewGC -Xms2g -Xmx4g"

    ExecStart=/opt/apache-tomcat-8.5.6/bin/startup.sh
    ExecStop=/bin/kill -15 $MAINPID

    [Install]
    WantedBy=multi-user.target
    ```

    The script tells the system to run the service as the `tomcat` user with the specified configs.

    Reload Systemd in order to discover and load the new Tomcat service file:

    ```sh
    systemctl daemon-reload
    ```

    Enable the service to start at boot:

    ```sh
    systemctl enable tomcat.service
    ```

    To control the service:

    ```sh
    service tomcat [start | stop | restart | status]
    ```

    Or with Systemd directly:

    ```sh
    systemctl [start | stop | restart | status] tomcat
    ```


    ## Configure Tomcat with APR native library

    For better performance, scalability and SSL usage, especially on production environments, it is recommended to configure Tomcat to run with the APR library.

    Docs: https://tomcat.apache.org/tomcat-8.5-doc/apr.html