Skip to content

Instantly share code, notes, and snippets.

@GuyPaddock
Forked from sukharevd/wildfly-install.sh
Last active April 18, 2017 17:28
Show Gist options
  • Save GuyPaddock/264e11cf496c1033472a2ec60c27383f to your computer and use it in GitHub Desktop.
Save GuyPaddock/264e11cf496c1033472a2ec60c27383f to your computer and use it in GitHub Desktop.
Script to install JBoss Wildfly 10.x as service in Linux
#!/bin/bash
/usr/bin/authbind --deep /usr/bin/java "$@"
#!/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
set -e
set -u
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_GROUP="wildfly"
WILDFLY_SERVICE="wildfly"
WILDFLY_MODE="standalone"
WILDFLY_AJP_PORT=8009
WILDFLY_HTTP_PORT=80
WILDFLY_HTTPS_PORT=443
WILDFLY_MANAGEMENT_PORT=9990
WILDFLY_STARTUP_TIMEOUT=240
WILDFLY_SHUTDOWN_TIMEOUT=30
# Options: "AUTO", "SYSTEMD", "RHEL_INIT", or "OTHER"
INIT_SYSTEM="DEBIAN_INIT"
UFW_PATH='/usr/sbin/ufw'
AUTHBIND_DEST='/usr/local/bin/java_authbind'
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
authorize_port() {
port=$1
port_file="/etc/authbind/byport/$port"
touch $port_file
chown $WILDFLY_USER:$WILDFLY_GROUP $port_file
chmod 500 $port_file
}
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root."
exit 1
fi
([ -x /bin/systemctl ] && systemctl stop $WILDFLY_SERVICE) > /dev/null || service $WILDFLY_SERVICE stop > /dev/null || true > /dev/null
echo "Downloading: $WILDFLY_DOWNLOAD_ADDRESS..."
[ -e "$WILDFLY_ARCHIVE_NAME" ] && echo 'Wildfly archive already exists.'
if [ ! -e "$WILDFLY_ARCHIVE_NAME" ]; then
wget --progress=bar $WILDFLY_DOWNLOAD_ADDRESS
if [ $? -ne 0 ]; then
echo "Not possible to download Wildfly."
exit 1
fi
fi
echo "Cleaning up..."
set -x
rm -f "$WILDFLY_DIR"
rm -rf "$WILDFLY_FULL_DIR"
rm -rf "/var/run/$WILDFLY_SERVICE/"
rm -f "/etc/init.d/$WILDFLY_SERVICE"
set +x
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 || true
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 && $INIT_SYSTEM = "AUTO" ) || ( $INIT_SYSTEM = "SYSTEMD" ) ]]; then
echo " Using systemd"
# 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
# if non-systemd Debian-like distribution
elif [[ ( -x /lib/lsb/init-functions && $INIT_SYSTEM = "AUTO" ) || ( $INIT_SYSTEM = "DEBIAN_INIT" ) ]]; then
echo " Using Debian init.d"
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
# if non-systemd RHEL-like distribution
elif [[ ( -x /etc/init.d/functions && $INIT_SYSTEM = "AUTO" ) || ( $INIT_SYSTEM = "RHEL_INIT" ) ]]; then
echo " Using RHEL init.d"
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
# if neither Debian nor RHEL like distribution
else
echo " Using vanilla init.d"
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 WILDFLY_HOME=\"$WILDFLY_DIR\" >> $WILDFLY_SERVICE_CONF
echo JBOSS_USER=$WILDFLY_USER >> $WILDFLY_SERVICE_CONF
echo WILDFLY_USER=\"$WILDFLY_USER\" >> $WILDFLY_SERVICE_CONF
echo JBOSS_CONFIG=$WILDFLY_MODE.xml >> $WILDFLY_SERVICE_CONF
echo WILDFLY_CONFIG=$WILDFLY_MODE.xml >> $WILDFLY_SERVICE_CONF
echo JBOSS_MODE=$WILDFLY_MODE >> $WILDFLY_SERVICE_CONF
echo WILDFLY_MODE=$WILDFLY_MODE >> $WILDFLY_SERVICE_CONF
echo WILDFLY_BIND=0.0.0.0 >> $WILDFLY_SERVICE_CONF
echo JAVA=\"$AUTHBIND_DEST\" >> $WILDFLY_SERVICE_CONF
echo STARTUP_WAIT=$WILDFLY_STARTUP_TIMEOUT >> $WILDFLY_SERVICE_CONF
echo SHUTDOWN_WAIT=$WILDFLY_SHUTDOWN_TIMEOUT >> $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=\"$WILDFLY_AJP_PORT\"/>,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=\"$WILDFLY_HTTP_PORT\"/>,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=\"$WILDFLY_HTTPS_PORT\"/>,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=\"$WILDFLY_MANAGEMENT_PORT\"/>,g" $WILDFLY_DIR/$WILDFLY_MODE/configuration/$WILDFLY_MODE.xml
if [ -x "$UFW_PATH" ]; then
echo "Configuring UFW..."
$UFW_PATH enable
$UFW_PATH allow 22
$UFW_PATH allow $WILDFLY_HTTP_PORT
$UFW_PATH allow $WILDFLY_HTTPS_PORT
fi
echo "Installing and configuring Authbind..."
sudo apt-get -y install authbind || sudo yum -y install authbind || true
sudo cp ./java_authbind $AUTHBIND_DEST
sudo chmod 755 $AUTHBIND_DEST
authorize_port $WILDFLY_AJP_PORT
authorize_port $WILDFLY_HTTP_PORT
authorize_port $WILDFLY_HTTPS_PORT
authorize_port $WILDFLY_MANAGEMENT_PORT
if [ -x "/etc/init.d/$WILDFLY_SERVICE" ]; then
# Fixes a bug where JAVA is not exported from the init script to the launcher
#
# This is needed so we can launch it in an authbind wrapper
sed -i -e ':a;N;$!ba;s,\n\n# Location of wildfly,\nexport JAVA\n\n# Location of wildfly,g' "/etc/init.d/$WILDFLY_SERVICE"
fi
[ -x /bin/systemctl ] && systemctl start $WILDFLY_SERVICE || service $WILDFLY_SERVICE start
echo "Done."
@GuyPaddock
Copy link
Author

This version allows us to control which init system we want to use via the $INIT_SYSTEM variable. This can help when using, for example, an Ubuntu 16.04 system that's running with Upstart instead of systemd.

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