Setting up Glassfish behind Apache

I’ve been using Glassfish Open Source 3.1 for a short while now, and I think it’s a great application server.  It has a nice Web Admin Panel and a powerful shell command to manage the server during runtime (without needing to tweak XML files on your own), depending if you have the Web Admin Panel port available or not at your location.

Why set up Glassfish behind Apache?

When started, Glassfish opens two HTTP ports by default: 8080, which is the usual HTTP port where all your WAR applications are available, and also port 4848, which lets you access the Web Admin Panel.  You don’t need an additional Web server like Apache to use your Glassfish server, but eventually you’ll run into one of the following scenarios that will make you consider having your Glassfish server behind a Web Server like Apache:

  • Port 8080 is blocked for your end users.
  • People can become suspicious when accessing an unusual domain like http://www.mysuperapplication.com:8080
  • You need to set up several instances of Glassfish (e.g. dev and live), and need to keep each of them in its own JVM

For this configuration, you’ll need the following:

This configuration was successfully deployed in CentOS 5.5, but should be easily ported to other Linux distributions, or even a Windows-based server.

Installing Glassfish:

Installing Glassfish is just a matter of unzipping the package file (zip or tar.gz) you downloaded in any location in your server.  We recommend you create a low-privilege user in your Linux server and run Glassfish with this user. It is not a good idea to run an application server with root privileges.

After you unzip Glassfish, you can start the server by running this command:

$GLASSFISH_HOME/glassfish/bin/asadmin start-domain

This command will start the default domain included in the server, which is called domain1.  After a few seconds, you should be able to browse to its default page at http://server-ip:8080 or the Web Admin Panel at http://server-ip:4848.

After this, we’ll use the asadmin commnad to to add an http listener called jk-connector-8009, which will listen on port 8009 for AJP connections.  This is the port that Apache will use to redirect requests from port 80 to Glassfish, and back to Apache.

Create the listener:

$GLASSFISH_HOME/glassfish/bin/asadmin --user admin --host localhost --port 4848 create-http-listener --listeneraddress 0.0.0.0 --listenerport 8009 --defaultvs server jk-connector-8009

Activate the listener:

$GLASSFISH_HOME/glassfish/bin/asadmin --user admin --host localhost --port 4848 set configs.config.server-config.network-config.network-listeners.network-listener.jk-connector-8009.jk-enabled=true

Finally, restart the Glassfish Server:

$GLASSFISH_HOME/glassfish/bin/asadmin stop-domain
$GLASSFISH_HOME/glassfish/bin/asadmin start-domain

Configuring Apache

Download the Tomcat mod_jk connector and place the .so file in your modules directory (e.g. /etc/httpd/modules)
Create a file called workers.properties and place it in a safe place (e.g. /etc/httpd/conf.d).  Put the following contents in this file:

worker.list=worker1
worker.worker1.type=ajp13
worker.worker1.host=localhost
worker.worker1.port=8009

Then, open your httpd.conf file and put the following contents in it (outside a VirtualHost section).  Check the path of your workers.properties file

JkWorkersFile /path/to/workers.properties
JkLogFile /var/log/httpd/mod_jk.log
JkLogLevel debug
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
JkRequestLogFormat "%w %V %T"

Also, you need to add a VirtualHost section in your page, in order to map the specific domain/subdomain with the path in Glassfish you want to make visible through Apache:


<VirtualHost 111.111.111.111:80>
  ServerAdmin webmaster@mydomain.com
  ServerName mydomain.com

  ... other stuff

  JkMount /myapp/* worker1

  ... other stuff

</VirtualHost>

The highlighted line tells Apache that all requests that go to http://mydomain.com/myapp/ will be served by worker1, which is connected to port 8009 of localhost, which is the port we opened before in Glassfish.  If you want Glassfish to serve all contents of mydomain.com, change the JKMount to /* worker1.

Finally, restart Apache and you’re all set!  Your Glassfish instance will be visible through Apache by port 80, and therefore you won’t need to open additional ports in your server, and you can use the best of both worlds.

Cheers!

Posted in Blog, IT, Java

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>