How To Run a Java Application on Linux using Systemd

Systemd is a system manager and the new Linux init system. This guide shows how to start a Java application with the systemctl command.

Create Java Application User

For security purposes, the application should be run as an unprivileged user (i.e. not root). We will create a new user and group that will run the application service.

First, create a new jvmapp group:

$ sudo groupadd -r jvmapp

Next, create a new jvmapp user. This user should be a member of the former created jvmapp group, with a home directory of where the location of the application is (e.g. /opt/application), and with a shell of /bin/false (so nobody is able to log into the account)

$ sudo useradd -r -g jvmapp -s /bin/false -d /opt/application jvmapp

Now, your jvmapp user is set up.

Create a systemd Service File

As the user root, create a application.service file in the /etc/systemd/system directory.

$ vi /etc/systemd/system/application.service

Write the following content inside the file.

[Unit]
Description=Java Application Service
After=network.target

[Service]
ExecStart=/path/to/java -jar /path/to/application.jar

User=jvmapp
Group=jvmapp

[Install]
WantedBy=multi-user.target

Start the application as a service

After saving the file, reload the systemd daemon so that it knows about the new service.

$ systemctl daemon-reload

Start the Application service by typing

$ systemctl start application

Show the status, if the service is started

$ systemctl status application

Now the service should start by each start of the system.

How To Run Apache Tomcat8 on Linux using Systemd

Introduction

Systemd is a system manager and init system that is becoming a new standard for Linux operating systems. This guide show how to start Apache Tomcat8 with the systemctl command.

Prerequisites

A running Linux maschine with systemd enabled, installed Java JDK, an Apache Tomcat 8 and for security reasons an unprivileged tomcat user.

Create a systemd Service File

To run Tomcat as a service, a systemd service file is needed. We also have to know where the Java JDK is installed.

As the user root, create a tomcat8.service file in the /etc/systemd/system directory:

$ vi /etc/systemd/system/tomcat8.service

The following content has to be in the service file. You can modify the environment variables for your needs.

[Unit]
Description=Apache Tomcat Web Application Container
After=network.target

[Service]
Type=forking

Environment=JAVA_HOME=/usr/lib/jvm/java-8-oracle/jre
Environment=CATALINA_PID=/var/run/tomcat8.pid
Environment=CATALINA_HOME=/opt/tomcat8
Environment=CATALINA_BASE=/opt/tomcat8
Environment='CATALINA_OPTS=-Xms512M -Xmx=1024M -server \
-XX:+UseParallelGC'
Environment='JAVA_OPTS=-Djava.awt.headless=true \
-Djava.security.egd=file:/dev/./urandom'

ExecStart=/opt/tomcat8/bin/startup.sh
ExecStop=/opt/tomcat8/bin/shutdown.sh

User=tomcat8
Group=tomcat8
RestartSec=10
Restart=always

[Install]
WantedBy=multi-user.target

Start the Tomcat service

After editing the file, reload the systemd daemon so that it knows about the new service file.

$ systemctl daemon-reload

Start the Tomcat service by typing

$ systemctl start tomcat8

Show the status that it was started

$ systemctl status tomcat8

Open your webbrowser and go to http://localhost:8080

How To Install Apache Tomcat 8 on Ubuntu Server 14.04

Introduction

Apache Tomcat is a web application container that is used to serve Java servlet applications. This How To covers the basic installation of Tomcat 8 on a Ubuntu Server 14.04.

Prerequisites

You need a running Ubuntu Server 14.04 installation, an actual Java JDK version 7 or 8.

Create a Tomcat User

For security reasons, Tomcat should be run as an unprivileged user. So, we will create a new Tomcat user and group that will run the Tomcat service.

Create a new Tomcat group:

$ sudo groupadd tomcat

Then we create a new Tomcat user, which is a member of the Tomcat group, with a home directory of /tmp with the following command:

$ sudo useradd -s /bin/false -g tomcat -d /tmp tomcat

Our Tomcat user is set up.

Install Tomcat

You have to download the latest Tomcat 8 binary distribution and extract the packed file into the /opt directory and rename the Tomcat installation directory to tomcat8:

$ cd /opt
$ sudo tar xvfz apache-tomcat-8*tar.gz
$ sudo mv apache-tomcat8* tomcat8

Update Permissions

We give the Tomcat user write/read access to specific files and directories:

Change into Tomcat installation path:

$ cd /opt/tomcat8

Set the permissions:

$ sudo chgrp -R tomcat conf
$ sudo chmod g+rwx conf
$ sudo chmod g+r conf/*

We have also to make the Tomcat user the owner of the work, temp, and logs directories:

$ sudo chmod -R tomcat work/ temp/ logs/

Install Upstart Script

Because we want to run Tomcat as a service, we neet to set up an Upstart script.

Create an Upstart script:

$ sudo vi /etc/init/tomcat.conf

Copy and paste the following script into the created tomcat.conf file:

description "Tomcat8 Production Server"

start on runlevel [2345]
stop on runlevel [!2345]
respawn
respawn limit 10 5

setuid tomcat
setgid tomcat

env JAVA_HOME=/usr/lib/jvm/java-8-oracle/jre
env CATALINA_HOME=/opt/tomcat8-prod

# Modify these options as needed
env JAVA_OPTS="-Djava.awt.headless=true -Djava.security.egd=file:/dev/./urandom"
env CATALINA_OPTS="-Xms512M -Xmx1024M -server -XX:+UseParallelGC"

exec $CATALINA_HOME/bin/catalina.sh run

# cleanup temp directory after stop
post-stop script
 rm -rf $CATALINA_HOME/temp/*
end script

This script is used to run the Tomcat service with the unprivileged Tomcat user.

Now we need to reload the Upstart configuration:

$ sudo initctl reload-configuration

Let’s start the Tomcat service with the following command:

$ sudo initctl start tomcat

Tomcat is now running and accessable with the domain or IP address followed by the default port :8080 in a web browser. Open the URL http://server_IP_address:8080 in w web browser.