How to install and configure JBoss AS with Java and Apache2 as a reverse proxy


jboss-logoToday we are going to show you how to setup JAVA, JBoss AS and an Apache server acting as a reverse proxy using one of our Debian Virtual Servers. We assume you have a working Apache server in place.

So, what is JBoss ?
JBoss is an open-source application server developed by RedHat based on the J2EE platform for developing and deploying enterprise Java applications, Web applications, Services, Portals and more. The J2EE allows applications to be standardized and modular allowing JAVA to handle many programming aspects when developing an application.

Before we start with the setup, make sure you have a fully up-to-date Debian system, so execute:

# apt-get update && apt-get upgrade --show-upgraded -y

Once your Debian system is fully updated, we are going to install JAVA. In this setup we are using the ‘sun-java’ package which resides in Debian’s ‘non-free’ repository. In order to install it we need to enable the sources first, so do the following:

# echo "deb http://ftp.us.debian.org/debian squeeze main contrib non-free" >> /etc/apt/sources.list

Once you’ve placed the sources the next step is to install JAVA on your Debian System. To do that execute:

# apt-get update && apt-get install sun-java6-jdk -y

you should now have JAVA installed on your server. To verify this please execute:

# java -version

and you should see something like this:

java version "1.6.0_26"
Java(TM) SE Runtime Environment (build 1.6.0_26-b03)
Java HotSpot(TM) Client VM (build 20.1-b02, mixed mode, sharing)

Ok, the next step is to install and configure the JBoss Application Server. We are using the latest final (at the time of writing this article) version of JBoss which is ‘ver.7.1.1’. So go to http://www.jboss.org/jbossas/downloads/ and download the JBoss version you want installed on your Debian Virtual Server.
The steps are as follows:

# cd /opt
# wget -P /var/tmp http://download.jboss.org/jbossas/7.1/jboss-as-7.1.1.Final/jboss-as-7.1.1.Final.tar.gz
# tar -zxf /var/tmp/jboss-as-7.1.1.Final.tar.gz -C /opt/

Next, we need to configure the JBoss AS so it will run without ‘root’ privileges because running it as ‘root’ is a big security risk. The best way to run JBoss is by using a dedicated system user which will run the JBoss server using the ‘sudo’ powers, so:

Create a group, add the jboss user to the group, set jboss user’s password and make it be the owner of the JBoss AS

# addgroup jbossgroup
# useradd -g jbossgroup jboss
# passwd jboss
# chown jboss:jbossgroup /opt/jboss-as-7.1.1.Final/ -R

NOTE: if you already have sudo setup it is ok, but if you haven’t you need to install it. If you’re unsure about this execute the following:

# [[ ! $(which sudo) ]] && apt-get install sudo -y


Next, add the jboss user to the sudo group:

# gpasswd -a jboss sudo

Ok, with all that in place we are somewhat ready to start the JBoss server. The last thing we need to do is the following:

Open the JBoss’ standalone.xml configuration file using your favorite editor and make the following changes:

# vim /opt/jboss-as-7.1.1.Final/standalone/configuration/standalone.xml

<interface name="management">
    <!--<inet-address value="${jboss.bind.address.management:127.0.0.1}"/>-->
    <any-ipv4-address/>
</interface>
<interface name="public">
    <!--<inet-address value="${jboss.bind.address:127.0.0.1}"/>-->
    <any-ipv4-address/>
</interface

This will make JBoss’ public and management frontend available from anywhere (using authentication of course) so you can manage it via a browser.

With all that in place the JBoss server is setup and ready but how are we supposed to start it? Unfortunately, JBoss is not shipping with an init script for Debian so it can be run as a service. To achieve this, we need to create this init script so create it by adding the following lines in ( /etc/init.d/jboss ):

#!/bin/sh
### BEGIN INIT INFO
# Provides:          jboss
# 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/Restart JBoss AS v7.0.0
### END INIT INFO

set -e

### declare the directory where is jboss/java installed
JAVA_HOME=/usr/lib/jvm/java-6-sun
JBOSS_HOME=/opt/jboss-as-7.1.1.Final
export PATH=${JAVA_HOME}/bin:${JBOSS_HOME}/bin:${PATH}

case $1 in
        start )
                echo -e "\aStarting JBoss ..."
                start-stop-daemon --start --quiet --background --chuid jboss --exec ${JBOSS_HOME}/bin/standalone.sh
        ;;
        stop )
                echo -e "\aStopping JBoss ..."
                start-stop-daemon --start --quiet --background --chuid jboss --exec ${JBOSS_HOME}/bin/jboss-cli.sh -- --connect command=:shutdown
        ;;
        restart )
                echo -e "\aRestarting the JBoss server ..."
                start-stop-daemon --start --quiet --background --chuid jboss --exec ${JBOSS_HOME}/bin/jboss-cli.sh -- --connect command=:shutdown && \
                start-stop-daemon --start --quiet --background --chuid jboss --exec ${JBOSS_HOME}/bin/standalone.sh
        ;;
        * )
                echo -e "\aUsage: /etc/init.d/jboss {start|stop|restart}"
                exit 1
        ;;
esac
exit 0

Save the file and assign an executable bit to it by:

chmod +x /etc/init.d/jboss

then add it to all runlevels and fire it up. Do this by executing:

# update-rc.d -f jboss defaults
# service jboss start

JBoss should be up and listening on port 8080 now. So, access the frontend by opening http://<yourdomain.tld>:8080 in your web browser. Once you’re there, follow the instructions on how to create a new user for your JBoss and once the user is created go deploy and enable your application.

After you’ve deployed and enabled your application you can access it at http://<yourdomain.tld>:8080/appname/

Next step is to configure Apache as a reverse proxy so it can handle and pass the requests to your JBoss and hide the port number in the URL. So, edit apache’s configuration file (/etc/apache2/apache2.conf) and add the following snippet (virtual host) at the bottom of the file:

<VirtualHost *:80>
ServerName www.yourdomain.tld
ServerAlias yourdomain.tld

    ProxyRequests Off

    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>

    ProxyPass / http://yourdomain.tld:8080/helloworld/
    ProxyPassReverse / http://yourdomain.tld:8080/helloworld/

    <Location />
        Order allow,deny
        Allow from all
    </Location>
</VirtualHost>

save the file and before reloading apache for the changes to take effect make sure the mod_proxy module is enabled or else it will fail to start.

# a2enmod proxy_http && service apache2 restart

now your JBoss application can be accessed by opening ‘http://yourdomain.tld/’


As an addition to this setup, what if you want to include some ‘PHP’ scripts under your JBoss application? Let’s say for example, you want to have ‘http://yourdomain.tld/service.php’ so how can we tell Apache to not pass those requests to JBoss but instead serve them by itself? It’s quite trivial to achieve this, so let’s go back and edit our ‘Virtual Host’ so it will look like the one below:

<VirtualHost *:80>
ServerName www.yourdomain.tld
ServerAlias yourdomain.tld

DocumentRoot /var/www/demoWeb
ProxyPassMatch ^/(.*)+\.php !

    ProxyRequests Off

    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>

    ProxyPass / http://yourdomain.tld:8080/helloworld/
    ProxyPassReverse / http://yourdomain.tld:8080/helloworld/

    <Location />
        Order allow,deny
        Allow from all
    </Location>
</VirtualHost>

every php script inside the ‘/var/www/demoWeb’ directory will not be passed to JBoss and will be accessible at http://yourdomain.com/<script>.php .

PS. If you liked this post please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

Tutorials
Install Selfoss on Debian Wheezy
Tutorials
How to install Serendipity CMS on a Linux Virtual Server
Tutorials
How to Install SugarCRM Community Edition on an Ubuntu 14.04 VPS
  • Chris Jacobs

    Thank you very much. I wish I had found this a week ago.


  • Tobias

    TobiasTobias

    Author Reply

    Reading several (outdated) instructions I was guided into trouble. With this everything went without problems! Thank you!
    You pointed up how to include PHP scripts under the JBoss. Of this state I tried to get a little further. I’m pretty new to this topic and I tried to have Apache serving the files existing in its directory and just let the request pass to JBoss if nothing is found.
    Is that even possible? If yes, how?

    Thanks for any advice!


  • Artem

    ArtemArtem

    Author Reply

    Why after the opening page, the web server generates an error HTTP 500 ???


  • Leonardo

    LeonardoLeonardo

    Author Reply

    Hi, very nice guide, but I’m wondering how can I change Jboss default port to be another for example 8086 and one more question, Can jboss co-exists with tomcat on the same server?


    • admin

      adminadmin

      Author Reply

      You need to navigate to your JBoss home directory, which if you followed this tutorial thoroughly, should be the /opt directory. Locate and edit the standalone.xml file. It’s location path is: /standalone/configuration/standalone.xml . Find ‘socket-binding-group’ and ‘socket-binding’ in the file and edit the port to your liking.

      Yes, you can run Tomcat and JBoss on one server, but you will need to make sure there are no port conflicts between the two.


  • Manoj

    ManojManoj

    Author Reply

    Thanks for such a nice article.

    After reading this : Just want to know one more thing – does this setting means we have one “apache server” which acts as reverse proxy server and underlying JBOSS server that really handled the routed URL as configured by APACHE Reverse proxy rules.

    Please validate my understanding.


    • admin

      adminadmin

      Author Reply

      Apache acts as a reverse proxy and it handles and pass the requests to your JBoss.