Skip to content

Commit

Permalink
[WFCORE-6935] Improve systemd service units
Browse files Browse the repository at this point in the history
Jira issue: https://issues.redhat.com/browse/WFCORE-6935

[WFCORE-6935] Minor fix for README
  • Loading branch information
yersan committed Oct 15, 2024
1 parent f8eca17 commit 96f6ca3
Show file tree
Hide file tree
Showing 10 changed files with 333 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
= How to configure WildFly as a Systemd service

The following steps describe the process to configure WildFly as a Systemd service.
You can use the provided wildfly-[standalone,domain].service file as a template for your own Systemd Unit File.
It can be adjusted to your needs, for example, you can manually change the user that runs the service,
the location of the WildFly installation, logs, etc.
Alternatively, you can use the generate_systemd_unit.sh script to automatically generate a new Systemd Unit File
using this server installation as the WildFly home.

If you want to install WildFly as a Systemd service from scratch as a Systemd service, follow the steps below.

== Install WildFly and initialize the WILDFLY_HOME variable

# unzip -d /opt wildfly-34.0.0.Final.zip
# ln -s /opt/wildfly-34.0.0.Final /opt/wildfly
# WILDFLY_HOME=/opt/wildfly

== Standalone or Domain mode ?

# MODE=standalone

== Create a wildfly user and group

# groupadd -r wildfly
# useradd -r -g wildfly -d "${WILDFLY_HOME}" -s /sbin/nologin wildfly

== Configure systemd

# cd "${WILDFLY_HOME}/bin/systemd"
# cp "wildfly-${MODE}.conf" /etc/sysconfig/
# cp "wildfly-${MODE}.service" $(pkg-config systemd --variable=systemdsystemunitdir)
# chown -R wildfly:wildfly "${WILDFLY_HOME}"/
# chmod +x "${WILDFLY_HOME}/bin/systemd/launch.sh"
# systemctl daemon-reload

=== If SELinux is enabled, you need to set the appropriate context for the launch.sh script:

# sudo chcon -R -t bin_t "${WILDFLY_HOME}/"
# sudo semanage fcontext -a -t systemd_unit_file_t '${WILDFLY_HOME}(/.*)?'

== Start and enable WildFly

# systemctl start "wildfly-${MODE}.service"
# systemctl enable "wildfly-${MODE}.service"

== Check the status of WildFly

# systemctl status "wildfly-${MODE}.service"



= How to remove WildFly as a systemd service

== Standalone or Domain mode ?

# MODE=standalone

== Stop and disable WildFly

# systemctl stop "wildfly-${MODE}.service"
# systemctl disable "wildfly-${MODE}.service"

== If you are using SELinux, remove the custom context added

sudo semanage fcontext -d '${WILDFLY_HOME}(/.*)?'

== Remove WildFly systemd service

# rm -f "$(pkg-config systemd --variable=systemdsystemunitdir)/wildfly-${MODE}.service"
# rm -f "/etc/sysconfig/wildfly-${MODE}.conf"
# systemctl daemon-reload

== Remove WildFly installation

# rm -rf $(readlink "${WILDFLY_HOME}")
# rm -rf "${WILDFLY_HOME}"

== Remove WildFly user and group

# userdel wildfly
# groupdel wildfly
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/bin/sh

# This script is just a helper to generate the Systemd Unit Files for WildFly
# assuming that the server home will be the one that hold this script file.
# It also allows to specify the user and group that will run the server.

SCRIPT_NAME=$(basename "$0")
SCRIPT_DIR="$(dirname "$(realpath "$0")")"
JBOSS_HOME="$(realpath "${SCRIPT_DIR}/../..")"
JBOSS_SYSTEMD_MODE="${1}"

if [ -z "${JBOSS_SYSTEMD_MODE}" ]; then
echo "INFO: Usage: "
echo "${SCRIPT_NAME} standalone|domain [user name] [group name]"
exit 0;
fi

SYSTEMD_FILE="${SCRIPT_DIR}/wildfly-${JBOSS_SYSTEMD_MODE}.service"
JBOSS_SYSTEMD_USER="${2:-wildfly}"
JBOSS_SYSTEMD_GROUP="${3:-wildfly}"

echo "INFO: Systemd Unit File to generate: ${SYSTEMD_FILE}"
echo "INFO: Using JBOSS_HOME: ${JBOSS_HOME}"
echo "INFO: User: ${JBOSS_SYSTEMD_USER}"
echo "INFO: Group: ${JBOSS_SYSTEMD_GROUP}"

while true; do
read -p "Do you want to generate ${SYSTEMD_FILE}? (y/n): " choice
case "$choice" in
y|Y ) break;;
n|N ) echo "Operation cancelled."; exit 1;;
* ) ;;
esac
done

sed -e "s|@@@JBOSS_SYSTEMD_SERVER_HOME@@@|${JBOSS_HOME}|g" \
-e "s|@@@JBOSS_SYSTEMD_USER@@@|${JBOSS_SYSTEMD_USER}|g" \
-e "s|@@@JBOSS_SYSTEMD_GROUP@@@|${JBOSS_SYSTEMD_GROUP}|g" \
"${SYSTEMD_FILE}.template" > "${SYSTEMD_FILE}"

systemd-analyze verify --recursive-errors=no "${SYSTEMD_FILE}"

echo ""
echo "INFO: Systemd Unit File generated."
echo "INFO: The ${JBOSS_SYSTEMD_USER}:${JBOSS_SYSTEMD_GROUP} are the user:group configured to launch the server. You have to ensure this user and group exist and have the necessary permissions to read and launch the server."
echo "INFO: Use ${JBOSS_HOME}/bin/systemd/wildfly-${JBOSS_SYSTEMD_MODE}.conf to configure the server environment."
echo "INFO: After configuring your environment, to install the server as a Systemd service do the following:"
echo ""
echo "sudo cp ${SYSTEMD_FILE} $(pkg-config systemd --variable=systemdsystemunitdir)"
echo "sudo cp ${JBOSS_HOME}/bin/systemd/wildfly-${JBOSS_SYSTEMD_MODE}.conf /etc/sysconfig/wildfly-${JBOSS_SYSTEMD_MODE}.conf"
echo "sudo systemctl daemon-reload"
echo "sudo systemctl start $(basename "${SYSTEMD_FILE}")"
echo ""
echo "INFO: In case of issues, you can check the service logs with:"
echo "sudo journalctl -u $(basename "${SYSTEMD_FILE}")"
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/sh
if [ "$WILDFLY_SYSTEMD_DEBUG" == "true" ]; then
set -x
fi

echo "INFO: Systemd Unit File server launch script"

# Disable color output for the standard output
export JAVA_OPTS="$JAVA_OPTS -Dorg.jboss.logmanager.nocolor=true"
export PATH=$JAVA_HOME/bin:$PATH

logDir=$(dirname $WILDFLY_CONSOLE_LOG)
if [ ! -d "$logDir" ]; then
mkdir -p "$logDir"
fi

wildflyOpts="$WILDFLY_OPTS"
if [ -n "$WILDFLY_SUSPEND_TIMEOUT" ]; then
wildflyOpts="-Dorg.wildfly.sigterm.suspend.timeout=$WILDFLY_SUSPEND_TIMEOUT $wildflyOpts"
fi

if [ -z "$WILDFLY_HOST_CONFIG" ]; then
${WILDFLY_SH} -c "$WILDFLY_SERVER_CONFIG" -b $WILDFLY_BIND $wildflyOpts > "$WILDFLY_CONSOLE_LOG" 2>&1
else
${WILDFLY_SH} --host-config="$WILDFLY_HOST_CONFIG" -c "$WILDFLY_SERVER_CONFIG" -b $WILDFLY_BIND $wildflyOpts > "$WILDFLY_CONSOLE_LOG" 2>&1
fi
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# The configuration you want to run

# Location of java in the JRE (the default)
#JAVA_HOME=/usr/lib/jvm/jre
# Use the following for location of java in the SDK
#JAVA_HOME=/usr/lib/jvm/java


# Configure the suspend timeout. By default, the suspend timeout is disabled, meaning that the server will not wait
# in the SUSPENDING state for any in-flight requests. This property configures the maximum number of seconds to wait for
# in-flight requests to complete. A value of -1 means the server will wait indefinitely.
#
# See https://docs.wildfly.org/32/Admin_Guide.html#graceful-shutdown-from-an-os-signal
#
# Note that increasing this value will extend the time the service unit takes to shut down the server.
# If WILDFLY_SUSPEND_TIMEOUT is configured, ensure that the TimeoutStopSec directive in the systemd service unit
# is set to a value that allows the server to wait until the suspend timeout expires and the server is stopped.
#WILDFLY_SUSPEND_TIMEOUT=300

# Location of the server standard out, e.g /opt/wildfly/standalone/log/service-console.log
#WILDFLY_CONSOLE_LOG="/opt/wildfly/standalone/log/service-console.log"

# Define the script to use to start wildfly
#WILDFLY_SH="/opt/wildfly/bin/domain.sh"

# Define server configuration to start, eg. domain.xml
#WILDFLY_SERVER_CONFIG=domain.xml

# Define the host controller configuration, eg. host.xml
#WILDFLY_HOST_CONFIG=host.xml

# The address to bind to
#WILDFLY_BIND=0.0.0.0

# Additional server args to include on startup
# For example, to add two properties to the server:
#WILDFLY_OPTS="-Dproperty1=value1 -Dproperty2=value2"

# Enables debug traces for the server launch script used by the systemd service unit
#WILDFLY_SYSTEMD_DEBUG=true
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[Unit]
Description=WildFly (domain mode)
After=syslog.target network.target
Before=httpd.service

[Service]
Environment=LAUNCH_JBOSS_IN_BACKGROUND=1
Environment="JAVA_HOME=/usr/lib/jvm/jre"

Environment="WILDFLY_SH=/opt/wildfly/bin/domain.sh"
Environment="WILDFLY_SERVER_CONFIG=domain.xml"
Environment="WILDFLY_CONSOLE_LOG=/opt/wildfly/domain/log/systemd-console.log"
Environment="WILDFLY_HOST_CONFIG="
Environment="WILDFLY_BIND=0.0.0.0"
EnvironmentFile=-/etc/sysconfig/wildfly-domain.conf

User=wildfly
Group=wildfly
LimitNOFILE=102642
ExecStart=/bin/sh -c "/opt/wildfly/bin/systemd/launch.sh"

[Install]
WantedBy=multi-user.target
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[Unit]
Description=WildFly (domain mode)
After=syslog.target network.target
Before=httpd.service

[Service]
Environment=LAUNCH_JBOSS_IN_BACKGROUND=1
Environment="JAVA_HOME=/usr/lib/jvm/jre"

Environment="WILDFLY_SH=@@@JBOSS_SYSTEMD_SERVER_HOME@@@/bin/domain.sh"
Environment="WILDFLY_SERVER_CONFIG=domain.xml"
Environment="WILDFLY_CONSOLE_LOG=@@@JBOSS_SYSTEMD_SERVER_HOME@@@/domain/log/systemd-console.log"
Environment="WILDFLY_HOST_CONFIG="
Environment="WILDFLY_BIND=0.0.0.0"
EnvironmentFile=-/etc/sysconfig/wildfly-domain.conf

User=@@@JBOSS_SYSTEMD_USER@@@
Group=@@@JBOSS_SYSTEMD_GROUP@@@
LimitNOFILE=102642
ExecStart=/bin/sh -c "@@@JBOSS_SYSTEMD_SERVER_HOME@@@/bin/systemd/launch.sh"

[Install]
WantedBy=multi-user.target
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# The configuration you want to run

# Location of java in the JRE (the default)
#JAVA_HOME=/usr/lib/jvm/jre
# Use the following for location of java in the SDK
#JAVA_HOME=/usr/lib/jvm/java


# Configure the suspend timeout. By default, the suspend timeout is disabled, meaning that the server will not wait
# in the SUSPENDING state for any in-flight requests. This property configures the maximum number of seconds to wait for
# in-flight requests to complete. A value of -1 means the server will wait indefinitely.
#
# See https://docs.wildfly.org/32/Admin_Guide.html#graceful-shutdown-from-an-os-signal
#
# Note that increasing this value will extend the time the service unit takes to shut down the server.
# If WILDFLY_SUSPEND_TIMEOUT is configured, ensure that the TimeoutStopSec directive in the systemd service unit
# is set to a value that allows the server to wait until the suspend timeout expires and the server is stopped.
#WILDFLY_SUSPEND_TIMEOUT=300

# Location of the server standard out, e.g /opt/wildfly/standalone/log/service-console.log
#WILDFLY_CONSOLE_LOG="/opt/wildfly/standalone/log/service-console.log"

# Define the script to use to start wildfly
#WILDFLY_SH="/opt/wildfly/bin/standalone.sh"

# Define server configuration to start, eg. standalone.xml
#WILDFLY_SERVER_CONFIG=standalone.xml

# The address to bind to
#WILDFLY_BIND=0.0.0.0

# Additional server args to include on startup
# For example, to add two properties to the server:
#WILDFLY_OPTS="-Dproperty1=value1 -Dproperty2=value2"

# Enables debug traces for the server launch script used by the systemd service unit
#WILDFLY_SYSTEMD_DEBUG=true
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[Unit]
Description=WildFly (standalone mode)
After=syslog.target network.target
Before=httpd.service

[Service]
Environment=LAUNCH_JBOSS_IN_BACKGROUND=1
Environment="JAVA_HOME=/usr/lib/jvm/jre"

Environment="WILDFLY_SH=/opt/wildfly/bin/standalone.sh"
Environment="WILDFLY_SERVER_CONFIG=standalone.xml"
Environment="WILDFLY_CONSOLE_LOG=/opt/wildfly/standalone/log/systemd-console.log"
Environment="WILDFLY_BIND=0.0.0.0"
EnvironmentFile=-/etc/sysconfig/wildfly-standalone.conf

User=wildfly
Group=wildfly
LimitNOFILE=102642
ExecStart=/bin/sh -c "/opt/wildfly/bin/systemd/launch.sh"


[Install]
WantedBy=multi-user.target
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[Unit]
Description=WildFly (standalone mode)
After=syslog.target network.target
Before=httpd.service

[Service]
Environment=LAUNCH_JBOSS_IN_BACKGROUND=1
Environment="JAVA_HOME=/usr/lib/jvm/jre"

Environment="WILDFLY_SH=@@@JBOSS_SYSTEMD_SERVER_HOME@@@/bin/standalone.sh"
Environment="WILDFLY_SERVER_CONFIG=standalone.xml"
Environment="WILDFLY_CONSOLE_LOG=@@@JBOSS_SYSTEMD_SERVER_HOME@@@/standalone/log/systemd-console.log"
Environment="WILDFLY_BIND=0.0.0.0"
EnvironmentFile=-/etc/sysconfig/wildfly-standalone.conf

User=@@@JBOSS_SYSTEMD_USER@@@
Group=@@@JBOSS_SYSTEMD_GROUP@@@
LimitNOFILE=102642
ExecStart=/bin/sh -c "@@@JBOSS_SYSTEMD_SERVER_HOME@@@/bin/systemd/launch.sh"

[Install]
WantedBy=multi-user.target
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
= How to configure WildFly as a systemd service

Note: The systemd scripts provided here have been deprecated in favor of $WFLY_HOME/bin/systemd
See $WFLY_HOME/bin/systemd/README to get more information.

== Create a wildfly user

# groupadd -r wildfly
Expand Down

0 comments on commit 96f6ca3

Please sign in to comment.