Wednesday, January 1, 2014

Placing Glassfish behind Apache HTTP to redirect domain:port to domain

Glassfish can be configured to run behind Apache HTTP server and configure apache to point external requests to Glassfish. Therefore HTTP requests are handled by Apache and all www.example.com requests are pointed to www.example.com:8080 internally. Below is a brief explanation of how to do this from scratch:

  • Install Glassfish 3+ (make sure your listener is created and activated on 8009 and your jk is enabled)
  • Get the mod_jk connector and place it into apache/modules.
  • Create a worker.properties file and place it into apache/conf. It should contain the following properties:
worker.list=worker1
worker.worker1.type=ajp13
worker.worker1.host=localhost
worker.worker1.port=8009

Open httpd.conf file in apache/conf folder and place the following commands:

#load jk modules from modules folder
LoadModule jk_module modules/mod_jk.so 
#location of the worker file
JkWorkersFile conf/worker.properties
#where to put jk logs
JkLogFile logs/mod_jk.log 
#log level [deug/error/info]
JkLogLevel debug
#Log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y]"
# Indicate to send SSL KEY SIZE
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
# Set the request format
JkRequestLogFormat "%w %V %T"
# Send all jsp requests to Glassfish
JkMount /*.jsp worker1
# Send all webapp requests to Glassfish
JkMount /* worker1

You need also to add a VirtualHost section in your conf file. This maps your domain with the path in Glassfish so Apache will be able to see it. The following tells apache to map all /myapp/* links to glassfish (you can also use /* instead of /myapp/* to link all request to Glassfish explicitly, however this is not recommended as it gives full external access to Glassfish server)

<virtualhost 111.111.111.111:80>
  ServerAdmin admin@domain
  ServerName domain
  JkMount /myapp/* worker1
</virtualhost>


Note: if your Glassfish listener is not created you can created from shell command using glassfish asadmin with the following command (Path your command to asadmin file which is located in glassfish/glassfish/bin:


asadmin create-network-listener --protocol http-listener-1 --listenerport 8009 --jkenabled true jk-connector

  


Restart Apache and Glassfish for the new configurations to be updated. Apache should now see your Glassfish on port 80. So your www.example.com:8080 will be served on www.example.com.