Skip to content

Instantly share code, notes, and snippets.

@sukharevd
Last active October 21, 2023 11:56
Show Gist options
  • Save sukharevd/6087988 to your computer and use it in GitHub Desktop.
Save sukharevd/6087988 to your computer and use it in GitHub Desktop.
Script to install JBoss Wildfly 10.x as service in Linux
#!/bin/bash
#title :wildfly-install.sh
#description :The script to install Wildfly 10.x
#more :http://sukharevd.net/wildfly-8-installation.html
#author :Dmitriy Sukharev
#date :2016-06-18T02:45-0700
#usage :/bin/bash wildfly-install.sh
#tested-version1 :10.0.0.CR3
#tested-distros1 :Ubuntu 15.10; Debian 7,8; CentOS 7; Fedora 22
#tested-version2 :10.0.0.Final
#tested-distros2 :Debian 8
WILDFLY_VERSION=10.0.0.Final
WILDFLY_FILENAME=wildfly-$WILDFLY_VERSION
WILDFLY_ARCHIVE_NAME=$WILDFLY_FILENAME.tar.gz
WILDFLY_DOWNLOAD_ADDRESS=http://download.jboss.org/wildfly/$WILDFLY_VERSION/$WILDFLY_ARCHIVE_NAME
INSTALL_DIR=/opt
WILDFLY_FULL_DIR=$INSTALL_DIR/$WILDFLY_FILENAME
WILDFLY_DIR=$INSTALL_DIR/wildfly
WILDFLY_USER="wildfly"
WILDFLY_SERVICE="wildfly"
WILDFLY_MODE="standalone"
WILDFLY_STARTUP_TIMEOUT=240
WILDFLY_SHUTDOWN_TIMEOUT=30
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root."
exit 1
fi
echo "Downloading: $WILDFLY_DOWNLOAD_ADDRESS..."
[ -e "$WILDFLY_ARCHIVE_NAME" ] && echo 'Wildfly archive already exists.'
if [ ! -e "$WILDFLY_ARCHIVE_NAME" ]; then
wget -q $WILDFLY_DOWNLOAD_ADDRESS
if [ $? -ne 0 ]; then
echo "Not possible to download Wildfly."
exit 1
fi
fi
echo "Cleaning up..."
rm -f "$WILDFLY_DIR"
rm -rf "$WILDFLY_FULL_DIR"
rm -rf "/var/run/$WILDFLY_SERVICE/"
rm -f "/etc/init.d/$WILDFLY_SERVICE"
echo "Installation..."
mkdir $WILDFLY_FULL_DIR
tar -xzf $WILDFLY_ARCHIVE_NAME -C $INSTALL_DIR
ln -s $WILDFLY_FULL_DIR/ $WILDFLY_DIR
useradd -s /sbin/nologin $WILDFLY_USER
chown -R $WILDFLY_USER:$WILDFLY_USER $WILDFLY_DIR
chown -R $WILDFLY_USER:$WILDFLY_USER $WILDFLY_DIR/
#mkdir -p /var/log/$WILDFLY_SERVICE
echo "Registrating Wildfly as service..."
# if should use systemd
if [ -x /bin/systemctl ]; then
# Script from $WILDFLY_DIR/docs/contrib/scripts/systemd/launch.sh didn't work for me
cat > $WILDFLY_DIR/bin/launch.sh << "EOF"
#!/bin/sh
if [ "x$WILDFLY_HOME" = "x" ]; then
WILDFLY_HOME="/opt/wildfly"
fi
if [ "x$1" = "xdomain" ]; then
echo 'Starting Wildfly in domain mode.'
$WILDFLY_HOME/bin/domain.sh -c $2 -b $3
#>> /var/log/$WILDFLY_SERVICE/server-`date +%Y-%m-%d`.log
else
echo 'Starting Wildfly in standalone mode.'
$WILDFLY_HOME/bin/standalone.sh -c $2 -b $3
#>> /var/log/$WILDFLY_SERVICE/server-`date +%Y-%m-%d`.log
fi
EOF
# $WILDFLY_HOME is not visible here
sed -i -e 's,WILDFLY_HOME=.*,WILDFLY_HOME='$WILDFLY_DIR',g' $WILDFLY_DIR/bin/launch.sh
#sed -i -e 's,$WILDFLY_SERVICE,'$WILDFLY_SERVICE',g' $WILDFLY_DIR/bin/launch.sh
chmod +x $WILDFLY_DIR/bin/launch.sh
cp $WILDFLY_DIR/docs/contrib/scripts/systemd/wildfly.service /etc/systemd/system/$WILDFLY_SERVICE.service
WILDFLY_SERVICE_CONF=/etc/default/$WILDFLY_SERVICE
# To install multiple instances of Wildfly replace all hardcoding in systemd file
sed -i -e 's,EnvironmentFile=.*,EnvironmentFile='$WILDFLY_SERVICE_CONF',g' /etc/systemd/system/$WILDFLY_SERVICE.service
sed -i -e 's,User=.*,User='$WILDFLY_USER',g' /etc/systemd/system/$WILDFLY_SERVICE.service
sed -i -e 's,PIDFile=.*,PIDFile=/var/run/wildfly/'$WILDFLY_SERVICE'.pid,g' /etc/systemd/system/$WILDFLY_SERVICE.service
sed -i -e 's,ExecStart=.*,ExecStart='$WILDFLY_DIR'/bin/launch.sh $WILDFLY_MODE $WILDFLY_CONFIG $WILDFLY_BIND,g' /etc/systemd/system/$WILDFLY_SERVICE.service
systemctl daemon-reload
#systemctl enable $WILDFLY_SERVICE.service
fi
# if non-systemd Debian-like distribution
if [ ! -x /bin/systemctl -a -r /lib/lsb/init-functions ]; then
cp $WILDFLY_DIR/docs/contrib/scripts/init.d/wildfly-init-debian.sh /etc/init.d/$WILDFLY_SERVICE
sed -i -e 's,NAME=wildfly,NAME='$WILDFLY_SERVICE',g' /etc/init.d/$WILDFLY_SERVICE
WILDFLY_SERVICE_CONF=/etc/default/$WILDFLY_SERVICE
fi
# if non-systemd RHEL-like distribution
if [ ! -x /bin/systemctl -a -r /etc/init.d/functions ]; then
cp $WILDFLY_DIR/docs/contrib/scripts/init.d/wildfly-init-redhat.sh /etc/init.d/$WILDFLY_SERVICE
WILDFLY_SERVICE_CONF=/etc/default/wildfly.conf
chmod 755 /etc/init.d/$WILDFLY_SERVICE
fi
# if neither Debian nor RHEL like distribution
if [ ! -x /bin/systemctl -a ! -r /lib/lsb/init-functions -a ! -r /etc/init.d/functions ]; then
cat > /etc/init.d/$WILDFLY_SERVICE << "EOF"
#!/bin/sh
### BEGIN INIT INFO
# Provides: ${WILDFLY_SERVICE}
# Required-Start: $local_fs $remote_fs $network $syslog
# Required-Stop: $local_fs $remote_fs $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start/Stop ${WILDFLY_FILENAME}
### END INIT INFO
WILDFLY_USER=${WILDFLY_USER}
WILDFLY_DIR=${WILDFLY_DIR}
case "$1" in
start)
echo "Starting ${WILDFLY_FILENAME}..."
start-stop-daemon --start --background --chuid $WILDFLY_USER --exec $WILDFLY_DIR/bin/standalone.sh
exit $?
;;
stop)
echo "Stopping ${WILDFLY_FILENAME}..."
start-stop-daemon --start --quiet --background --chuid $WILDFLY_USER --exec $WILDFLY_DIR/bin/jboss-cli.sh -- --connect command=:shutdown
exit $?
;;
log)
echo "Showing server.log..."
tail -500f $WILDFLY_DIR/standalone/log/server.log
;;
*)
echo "Usage: /etc/init.d/wildfly {start|stop}"
exit 1
;;
esac
exit 0
EOF
sed -i -e 's,${WILDFLY_USER},'$WILDFLY_USER',g; s,${WILDFLY_FILENAME},'$WILDFLY_FILENAME',g; s,${WILDFLY_SERVICE},'$WILDFLY_SERVICE',g; s,${WILDFLY_DIR},'$WILDFLY_DIR',g' /etc/init.d/$WILDFLY_SERVICE
chmod 755 /etc/init.d/$WILDFLY_SERVICE
fi
if [ ! -z "$WILDFLY_SERVICE_CONF" ]; then
echo "Configuring service..."
echo JBOSS_HOME=\"$WILDFLY_DIR\" > $WILDFLY_SERVICE_CONF
echo JBOSS_USER=$WILDFLY_USER >> $WILDFLY_SERVICE_CONF
echo WILDFLY_HOME=\"$WILDFLY_DIR\" > $WILDFLY_SERVICE_CONF
echo WILDFLY_USER=\"$WILDFLY_USER\" > $WILDFLY_SERVICE_CONF
echo STARTUP_WAIT=$WILDFLY_STARTUP_TIMEOUT >> $WILDFLY_SERVICE_CONF
echo SHUTDOWN_WAIT=$WILDFLY_SHUTDOWN_TIMEOUT >> $WILDFLY_SERVICE_CONF
echo WILDFLY_CONFIG=$WILDFLY_MODE.xml >> $WILDFLY_SERVICE_CONF
echo WILDFLY_MODE=$WILDFLY_MODE >> $WILDFLY_SERVICE_CONF
echo WILDFLY_BIND=0.0.0.0 >> $WILDFLY_SERVICE_CONF
fi
echo "Configuring application server..."
sed -i -e 's,<deployment-scanner path="deployments" relative-to="jboss.server.base.dir" scan-interval="5000",<deployment-scanner path="deployments" relative-to="jboss.server.base.dir" scan-interval="5000" deployment-timeout="'$WILDFLY_STARTUP_TIMEOUT'",g' $WILDFLY_DIR/$WILDFLY_MODE/configuration/$WILDFLY_MODE.xml
sed -i -e 's,<inet-address value="${jboss.bind.address:127.0.0.1}"/>,<any-address/>,g' $WILDFLY_DIR/$WILDFLY_MODE/configuration/$WILDFLY_MODE.xml
sed -i -e 's,<socket-binding name="ajp" port="${jboss.ajp.port:8009}"/>,<socket-binding name="ajp" port="${jboss.ajp.port:28009}"/>,g' $WILDFLY_DIR/$WILDFLY_MODE/configuration/$WILDFLY_MODE.xml
sed -i -e 's,<socket-binding name="http" port="${jboss.http.port:8080}"/>,<socket-binding name="http" port="${jboss.http.port:28080}"/>,g' $WILDFLY_DIR/$WILDFLY_MODE/configuration/$WILDFLY_MODE.xml
sed -i -e 's,<socket-binding name="https" port="${jboss.https.port:8443}"/>,<socket-binding name="https" port="${jboss.https.port:28443}"/>,g' $WILDFLY_DIR/$WILDFLY_MODE/configuration/$WILDFLY_MODE.xml
sed -i -e 's,<socket-binding name="osgi-http" interface="management" port="8090"/>,<socket-binding name="osgi-http" interface="management" port="28090"/>,g' $WILDFLY_DIR/$WILDFLY_MODE/configuration/$WILDFLY_MODE.xml
[ -x /bin/systemctl ] && systemctl start $WILDFLY_SERVICE || service $WILDFLY_SERVICE start
echo "Done."
@xtecuan
Copy link

xtecuan commented Feb 2, 2017

It work correctly in Red Hat Enterprise Linux Server release 7.2 (Maipo) , thanks for the script

@avreddy2
Copy link

I'm able to install the wildfly as a service on RHEL 6.5. However, there is an issue with wildfly service. The wildfly service is stopping immediately after startup with following error message in logs:

WFLYSRV0220: Server shutdown has been requested via an OS signal

Any help you can provide would be greatly appreciated.

Thanks

@ymartin59
Copy link

To leave official unzipped archive unmodified on disk, I create a "standalone" server structure out of installation directory with configuration, logs and applications, using startup properties to point to modules/lib at installation location. That way, it is easy to run multiple standalone instances or discard and rebuild an instance with scripts.

I recommend use of jboss-cli commands to configure application server instead of sed expressions on XML configuration file, to be robust to wildfly upgrades. See https://docs.jboss.org/author/display/WFLY10/CLI+Recipes

@jaywax
Copy link

jaywax commented Mar 1, 2017

Works like a charm on Debian 8 and openjdk-8-jdk.
Thanks !

@dortegau
Copy link

At idealista we have created an Ansible role based on this gist: https://github.com/idealista/wildfly-role/

@GuyPaddock
Copy link

GuyPaddock commented Apr 17, 2017

Is it me, or are the WILDFLY_X values in /etc/default/wildfly not actually used to provide any values to the startup scripts?

EDIT: It looks like the WILDFLY_X values work only if systemd is being used. The init.d / SysV Init approaches use the old JBOSS_X values. This is a bug with the script.

@cfbetancurm
Copy link

Amazing!...wildfky stantalone in 10 seconds, thanks!!!

@gabtrr
Copy link

gabtrr commented Apr 26, 2017

Very useful. Thanks!

@duspenskiy
Copy link

Author, great work! However, two typos sent me on a wild goose chase. Following two lines need to be corrected in order not to overwrite the service config file:

`echo WILDFLY_HOME=\"$WILDFLY_DIR\" > $WILDFLY_SERVICE_CONF`
`echo WILDFLY_USER=\"$WILDFLY_USER\" > $WILDFLY_SERVICE_CONF`

Change the operator to "append":

`echo WILDFLY_HOME=\"$WILDFLY_DIR\" >> $WILDFLY_SERVICE_CONF`
`echo WILDFLY_USER=\"$WILDFLY_USER\" >> $WILDFLY_SERVICE_CONF`

This should solve the issue people are having with the "chown: missing operand" error.

@diversunny
Copy link

Works perfectly for Wildfly 10.1 on Raspberry Pi2 with CentOS 7:

  • changed http port binding to 8080 in /opt/wildfly/standalone/configuration/standalone.xml
  • added firewall rules for external access: firewall-cmd --permanent --add-port=8080/tcp
  • reloaded firewall: firewall-cmd --reload
  • added admin user: sudo -u wildfly /opt/wildfly/bin/add_user.sh

Had a lot of fun on this tiny wildlfy machine

@ltang007
Copy link

failed on Fedora 25 with following error, could anyone help?

● wildfly.service - The WildFly Application Server Loaded: loaded (/etc/systemd/system/wildfly.service; enabled; vendor preset: disabled) Active: failed (Result: exit-code) since Fri 2017-07-14 10:10:54 HKT; 7min ago Process: 10251 ExecStart=/opt/wildfly/bin/launch.sh $WILDFLYMODE $WILDFLYCONFIG $WILDFLY_BIND (code=exited, status=203/EXEC) Main PID: 10251 (code=exited, status=203/EXEC)

Jul 14 10:10:54 ISNTDEVOS systemd[1]: Started The WildFly Application Server. Jul 14 10:10:54 ISNTDEVOS systemd[1]: wildfly.service: Main process exited, code=exited, status=203/EXEC Jul 14 10:10:54 ISNTDEVOS systemd[1]: wildfly.service: Unit entered failed state. Jul 14 10:10:54 ISNTDEVOS systemd[1]: wildfly.service: Failed with result 'exit-code'.

@russellhoff
Copy link

Thank you, I'll test Wildfly 11 on Ubuntu 17.10 64 bits

@BastienDurel
Copy link

The systemd unit misses a line in [Service] section :
WorkingDirectory=$WILDFLY_DIR

Or wildfly will start in /

@dragosrosculete
Copy link

Thank you BastienDurel . Adding WorkingDirectory=$WILDFLY_DIR solved a problem I was having with user.dir .

@fabmars
Copy link

fabmars commented Jun 14, 2018

In this script I saw the line "# Script from $WILDFLY_DIR/docs/contrib/scripts/systemd/launch.sh didn't work for me"

Well, I agree it's not out of the box, but to make the official wildfly contrib work, besides what's written in the README you only need to:

  • EITHER unzip wildfly in /opt/wildfly OR make a symlink on that path
  • AND populate $JAVA_HOME and $WILDFLY_HOME for the wildfly user. Its shall is nologin and I'm using Debian so defining system-wide env vars is sort of a challenge, so I directly changed launch.sh and set those env vars there.
    Then,
    systemctl daemon-reload
    systemctl start wildfly.service
    Done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment