Search

October 20, 2013

Build and Install JBoss 7.2.0 on CentOS 6.4

As you may or may not be aware, a great many changes have been happening to the JBoss application server since being purchased by Red Hat. Most importantly, the application server is being rebranded as WildFly for version 8. ("Why?" is the first entry on the WildFly FAQ if you are curious.) But since WildFly is not quite ready as of this writing (but looks real close), we are going to deal with the latest community release, JBoss AS 7.2.0.

Before the name change to WildFly, there was a split in JBoss into a 7.x community edition and a 6.x EAP edition. The community edition remains the open source version free to use as before. The EAP edition requires a subscription to use for production purposes (but is free for development). The EAP edition is based on the community edition, typically taking the "Final" version of the community edition as the "Alpha" of the corresponding EAP edition (although keep in mind that the community edition versions are 7.x while the EAP is 6.x). This appears to be a split in the mold of Red Hat Linux begetting Fedora and Red Hat Enterprise.

Whew, that is a lot of change and I haven’t even mentioned that JBoss AS 7 appears to be a complete rewrite. The configuration files and tools are completely different, so if are coming from JBoss 5 or 6, be prepared to invest a little time to get familiar with the new JBoss, er, WildFly.

OK, we are getting closer to the good stuff. In addition to all the changes mentioned above, Red Hat (or JBoss.org or WildFly.org whoever is responsible for the community editions of JBoss AS 7) has stopped providing builds of the JBoss AS 7 line after 7.1.1. Since then there have been versions 7.1.2, 7.1.3 and 7.2.0 but no corresponding build on the JBoss AS download page. Fortunately it is easy to get the source and build JBoss yourself.

We will be using a new CentOS 6.4 box created using the CentOS minimal DVD. We’ll start by installing Java; instead of going to Oracle, I decided to try the OpenJDK from the CentOS repositories to see how that goes:

[root@wxyz ~]# yum install java-1.7.0-openjdk java-1.7.0-openjdk-devel wget

You’ll notice I threw in wget since we will need it shortly.

Next we create a jboss user on the server to build and run JBoss as. (You may want to call the user jboss-as since the JBoss scripts reference this user by default. I already have a convention of using the user jboss and I want to stick with it.)

[root@wxyz ~]# useradd --system --comment JBossAS --create-home \
--home /opt/jboss --user-group jboss

Now we are ready to download the JBoss source. Fortunately for us, github provides tarballs of tags so we can easily download the JBoss source for 7.2.0:

[jboss@wxyz ~]$ wget https://github.com/jbossas/jboss-as/archive/7.2.0.Final.tar.gz
[jboss@wxyz ~]$ mv 7.2.0.Final /opt/jboss/JBoss-7.2. 0.Final.tar.gz
[jboss@wxyz ~]$ mkdir src
[jboss@wxyz ~]$ cd src
[jboss@wxyz src]$ tar xzf JBoss-7.2.0.Final.tar.gz
[jboss@wxyz src]$ cd jboss-as-7.2.0.Final/
[jboss@wxyz jboss-as-7.2.0.Final]$ # This next command kicks off the build
[jboss@wxyz jboss-as-7.2.0.Final]$ ./build.sh -DskipTests -Drelease=true

If you take a look at the README.md file in the download there are some basic instructions on how to do the build. One pleasant surprise is that there is no need to install maven. We do want a couple of tweaks to the command line they suggest however in order to skip the unit tests and get a release tarball. After running the command above you will find the tarball in the dist/target directory.

[jboss@wxyz jboss-as-7.2.0.Final]$ ls dist/target
archive-tmp  jboss-as-7.2.0.Final-src.tar.gz  jboss-as-7.2.0.Final-src.zip
jboss-as-7.2.0.Final.tar.gz  jboss-as-7.2.0.Final.zip
[jboss@wxyz jboss-as-7.2.0.Final]$ cd
[jboss@wxyz ~]$ tar zxvf src/jboss-as-7.2.0.Final/dist/target/jboss-as-7.2.0.Final.tar.gz

Now that we have a build of JBoss AS 7.2.0 on the system, we need to configure CentOS to treat it like a service. JBoss comes with some scripts in the bin/init.d directory that will help with this. I am using the standalone server, but it is not difficult to modify the instructions for the domain server. First up is the /etc/jboss-as/jboss-as.conf file.

[root@wxyz ~]# cd /etc
[root@wxyz etc]# mkdir jboss-as
[root@wxyz etc]# cd jboss-as/
[root@wxyz jboss-as]# cp /opt/jboss/jboss-as-7.2.0.Final/bin/init.d/jboss-as.conf ./
[root@wxyz jboss-as]# # We'll just create an empty properties file for now
[root@wxyz jboss-as]# touch jboss-as.properties

Next edit the file. We need to define a few environment variables:

# General configuration for the init.d scripts,
# not necessarily for JBoss AS itself.

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

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

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

# Location to keep the console log
#
JBOSS_CONSOLE_LOG=/var/log/jboss-as/console.log

# JBoss installation directory (default is /usr/share/jboss)
JBOSS_HOME=/opt/jboss/jboss-as-7.2.0.Final

# Server configuration file, using full JEE 6 profile
JBOSS_CONFIG=standalone-full.xml

# Need to modify the init script to account for these
JBOSS_OPTS="--properties=/etc/jboss-as/jboss-as.properties"

Just in case you are confused on the two files, jboss-as.conf is a bash script that is sourced by the JBoss init script; use it to set environment variables for the scripts. As for jboss-as.properties, it is a Java properties file that is read by JBoss on start up. Use it to set JBoss system properties (we will get to an example shortly).

Next up we will deposit the init script in /etc/init.d and set the server to start on boot:

[root@wxyz jboss-as]# cd /etc/ini t.d/
[root@wxyz init.d]# cp /opt/jboss/jboss-as-7.2.0.Final/bin/init.d/jboss-as-standalone.sh ./
[root@wxyz init.d]# mv jboss-as-standalone.sh jboss-as
[root@wxyz init.d]# chkconfig --add jboss-as
[root@wxyz init.d]# chkconfig --level 345 jboss-as on
[root@wxyz init.d]# chkconfig --list jboss-as
jboss-as        0:off   1:off   2:off   3:on    4:on    5:on    6:off

As-is the scripts do not allow passing any additional options to the JBoss process, so we need to edit the /etc/init.d/jboss-as script to add the $JBOSS_OPTS variable whenever JBoss is starting. Here is the edited snippet (I’ve added some line breaks for readability):

  if [ ! -z "$JBOSS_USER" ]; then
    if [ -r /etc/rc.d/init.d/functions ]; then
      daemon --user $JBOSS_USER LAUNCH_JBOSS_IN_BACKGROUND=1 \
        JBOSS_PIDFILE=$JBOSS_PIDFILE $JBOSS_SCRIPT -c $JBOSS_CONFIG \
        $JBOSS_OPTS 2>&1 > $JBOSS_CONSOLE_LOG &
    else
      su - $JBOSS_USER -c "LAUNCH_JBOSS_IN_BACKGROUND=1 \
        JBOSS_PIDFILE=$JBOSS_PIDFILE $JBOSS_SCRIPT -c $JBOSS_CONFIG \
        $JBOSS_OPTS" 2>&1 > $JBOSS_CONSOLE_LOG &
    fi
  fi

Next up we create the log directories and redirect the JBoss log directory to /var/log/jboss-as and the tmp directory to /tmp/jboss-as

[root@wxyz ~]# mkdir /var/log/jboss-as
[root@wxyz ~]# chown jboss:jboss /var/log/jboss-as/

To get JBoss to use these directories, we set the system properties for them in /etc/jboss-as/jboss-as.properties:

# System properties for jboss-as
jboss.server.log.dir=/var/log/jboss-as
jboss.server.temp.dir=/tmp/jboss-as

At this point you can start the server using the command service jboss-as start. You won’t be able to do much however, unless you like browsing via wget since the server is bound to the localhost interface only (and we are working on a headless server, i.e. no GUI web browser). So we’ll need to bind JBoss to all interfaces and open up the CentOS firewall to allow browsing to JBoss and the management console. Keep in mind that this may not be exactly what you want in a production environment or in environment where unknown users might access your server (like shared hosting).

First we’ll edit the /etc/jboss-as/jboss-as.properties file:

# System properties for jboss-as
jboss.bind.address=0.0.0.0
jboss.bind.address.management=0.0.0.0
jboss.server.log.dir=/var/log/jboss-as
jboss.server.temp.dir=/tmp/jboss-as

Depending on how your local network is setup you may need further configuration for JBoss to be completely happy binding to all interfaces. If your DNS recognizes the name of the CentOS server you are working on, congratulations, you are all set. On the other hand, if DNS is not configured for the server, you will need to add an entry to the /etc/hosts file pointing the name of the server to the external IP.

Next we need to open the firewall for ports 8080 (regular JBoss) and 9990 (the management console). Be careful when adjusting the firewall. The commands below use hard-coded line numbers that may not be appropriate for whatever system you are using. If you think you have screwed up your firewall use service iptables restart to reset it. Of course, this won’t work after you issue service iptables save so be extra sure before you save the rules. You will also need to replace 10.0.0.0 with your subnet below.

[root@wxyz ~]# # 8080 for JBoss applications
[root@wxyz ~]# iptables --insert INPUT 5 \
         --match state \
         --state NEW \
         --protocol tcp \
         --destination-port 8080 \
         --source 10.0.0.0/24 \
         --jump ACCEPT
[root@wxyz ~]# # 9990 for JBoss management
[root@wxyz ~]# iptables --insert INPUT 6 \
         --match state \
         --state NEW \
         --protocol tcp \
         --destination-port 9990 \
         --source 10.0.0.0/24 \
         --jump ACCEPT
[root@wxyz ~]# # Verify the state of the firewall
[root@wxyz ~]# iptables -L --line-numbers
[root@wxyz ~]# # Go and test it out before doing the following
[root@wxyz ~]# service iptables save

At this point you should be ready to go. Start up JBoss and try accessing it from your desktop.