Book Image

Wildfly Cookbook

Book Image

Wildfly Cookbook

Overview of this book

Table of Contents (23 chapters)
WildFly Cookbook
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Running WildFly as a service


In this recipe, you will learn how to install WildFly as a service, actually having WildFly run automatically when the OS starts. If you want to run Wildfly manually on demand, you can skip this recipe, unless for knowledge purpose.

Most Unix-like systems have different "runlevels" (think about them as steps) for various stages of the system running phase. At the operating system level, a service can be activated only if other services have been successfully activated. Thus if you activate a service that needs network before this one is up and running, it gets faulty or useless. This is essentially what "runlevels" are for.

The following is a list of runlevels:

  • rc1.d: Single user mode

  • rc2.d: Single user mode with networking

  • rc3.d: Multi-user mode—boot up in text mode

  • rc4.d: Undefined

  • rc5.d: Multi-user mode—boot up in X Windows

  • rc6.d: Shutdown

Most production Linux systems boot using runlevel 3 (UI is not needed and it will be a waste of resources), but to reach all audiences, we will use level 2, 3 and 5.

How to do it…

WildFly comes with a predefined script, which can be used to run WildFly as a service. This script is located in the bin/init.d folder of the WildFly installation folder. So we just need to copy the file inside the /etc/init.d system folder and set it as a service, as follows:

$ sudo cp $WILDFLY_HOME/bin/init.d/wildfly-init-redhat.sh /etc/init.d/wildfly
$ sudo chkconfig --add wildfly
$ sudo chkconfig wildfly on --level 235

There is also one more file that we need to take a look at, and it is the wildfly.conf, placed into the same bin/init.d directory of the WildFly installation folder. The following is the file as is:

# General configuration for the init.d scripts,
# not necessarily for JBoss AS itself.
# default location: /etc/default/wildfly

## Location of JDK
# JAVA_HOME="/usr/lib/jvm/default-java"

## Location of WildFly
# JBOSS_HOME="/opt/wildfly"

## The username who should own the process.
# JBOSS_USER=wildfly

## The mode WildFly should start, standalone or domain
# JBOSS_MODE=standalone

## Configuration for standalone mode
# JBOSS_CONFIG=standalone.xml

## Configuration for domain mode
# JBOSS_DOMAIN_CONFIG=domain.xml
# JBOSS_HOST_CONFIG=host-master.xml

## The amount of time to wait for startup
# STARTUP_WAIT=60

## The amount of time to wait for shutdown
# SHUTDOWN_WAIT=60

## Location to keep the console log
# JBOSS_CONSOLE_LOG="/var/log/wildfly/console.log"

The previous configuration file basically sets a series of parameters that tell init-script which WildFly mode to use, which configuration file, which user WildFly should run with, and so on. Any update should be placed into that file.

For the moment, we will rely on the defaults, except for the user that we will mention explicitly by uncommenting the line # JBOSS_USER=wildfly, by removing the hash # symbol. You may have noticed that you can also specify the mode which WildFly will run with: domain or standalone.

We now need to create the wildfly user, and give the ownership of the WildFly home folder to the wildfly user. Do as follows:

$ sudo groupadd -r wildfly
$ sudo useradd -r -g wildfly -s /sbin/nologin -c "WildFly user" wildfly
$ sudo passwd -d wildfly
$ sudo chown -R :wildfly $WILDFLY_HOME/*

Now if you reboot your system, you will have WildFly up and running as a service with the default settings, launched by the wildfly user.