Skip to main content

Running GlassFish 3.1.2 with Apache HTTP Server

Posted by amyroh on February 15, 2012 at 2:45 PM PST

With GlassFish 3.1.2 release around the corner, I've decided to update the outdated blog to cover how to front GlassFish 3.1.2 with Apache HTTP Server.  The instructions have changed since v3 and it might lead to some confusion to the users who might be looking at the old blog post. 

GlassFish, Apache HTTP Server, and mod_jk

GlassFish 3.1.2 (as well as 3.1.1) supports AJP protocol for each network listener using the jk-enabled attribute as described below.  The instructions are the same for 3.1.1 and 3.1.2.

1.  Install Apache HTTP Server and mod_jk.

2.  Configure Apache

apache/conf/workers.properties

# Define 1 real worker using ajp13
worker.list=worker1
# Set properties for worker1 (ajp13)
worker.worker1.type=ajp13
worker.worker1.host=localhost
worker.worker1.port=8009

apache/conf/httpd.conf 

LoadModule jk_module /apache/modules/mod_jk-1.2.25-httpd-2.2.4.so
JkWorkersFile /apache/conf/worker.properties
# Where to put jk logs
JkLogFile /apache/logs/mod_jk.log
# Set the jk log level [debug/error/info]
JkLogLevel debug
# Select the log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
# JkOptions indicate to send SSL KEY SIZE,
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
# JkRequestLogFormat set the request format
JkRequestLogFormat "%w %V %T"
# Send all jsp requests to GlassFish
JkMount /*.jsp worker1
# Send all webapp requests to GlassFish
JkMount /webapp/* worker1

3.  Start Apache HTTP Server.

4.  Start GlassFish Server and deploy your webapp.

5.  Create a jk-enabled network listener either using asadmin command or the admin console.

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

6.  The jk-configuration-file attribute of the network listener can be used to configure non-default attributes for the Connector.  Use asadmin set to configure the jk-configuration-file attribute of the network listener to the fully-qualified filename of the jk-configuration-file.  This step is optional.

asadmin> set server-config.network-config.network-listeners.network-listener.jk-connector.jk-configuration-file=domain-dir/config/glassfish-jk.properties   

7.  Increase the maximum threads in the http-thread-pool pool if necessary.

asadmin> set configs.config.server-config.thread-pools.thread-pool.http-thread-pool.max-thread-pool-size=value

The same GlassFish configuration can be used for mod_proxy_ajp also.  GlassFish 3.1.2 will be using Grizzly for ajp implementation.

Load balance using GlassFish, Apache HTTP Server, and mod_jk

GlassFish, Apache HTTP Server, and mod_jk can be used for load balancing and the following describes how to load balance using two instances.

1.  Install Apache HTTP Server and mod_jk.

2.  Configure Apache

apache/conf/workers.properties

worker.list=loadbalancer
worker.worker1.type=ajp13
worker.worker1.host=localhost
worker.worker1.port=8009
worker.worker1.lbfactor=1
worker.worker1.socket_keepalive=1
worker.worker1.socket_timeout=300
worker.worker2.type=ajp13
worker.worker2.host=localhost
worker.worker2.port=8010
worker.worker2.lbfactor=1
worker.worker2.socket_keepalive=1
worker.worker2.socket_timeout=300
worker.loadbalancer.type=lb
worker.loadbalancer.balance_workers=worker1,worker2

apache/conf/httpd.conf 

LoadModule jk_module /apache/modules/mod_jk-1.2.25-httpd-2.2.4.so
JkWorkersFile /apache/conf/worker.properties
# Where to put jk logs
JkLogFile /apache/logs/mod_jk.log
# Set the jk log level [debug/error/info]
JkLogLevel debug
# Select the log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
# JkOptions indicate to send SSL KEY SIZE,
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
# JkRequestLogFormat set the request format
JkRequestLogFormat "%w %V %T"
# Send all jsp requests to GlassFish
JkMount /*.jsp loadbalancer
# Send all webapp requests to GlassFish
JkMount /webapp/* loadbalancer

3.  Start Apache HTTP Server.

4.  Start GlassFish Server and deploy your webapp.

5.  Create a jk-enabled network listener targeted to the cluster either using asadmin create-network-listener command or the admin console. 

asadmin> create-network-listener --protocol http-listener-1 --target cluster1 --listenerport ${AJP_PORT} --jkenabled true jk-connector

6.  Create the jvmRoute property targeted to the cluster using the create-jvm-options command.

asadmin> create-jvm-options --target cluster1 "-DjvmRoute=\${AJP_INSTANCE_NAME}"  

7.  Define the AJP_PORT and AJP_INSTANCE_NAME properties for each of the instances of the cluster using the create-system-properties command.  The port values need to match the values specified in the  workers.properties.

asadmin> create-system-properties --target instance1 AJP_PORT=8009
asadmin> create-system-properties --target instance1 AJP_INSTANCE_NAME=instance1
asadmin> create-system-properties --target instance2 AJP_PORT=8010
asadmin> create-system-properties --target instance2 \AJP_INSTANCE_NAME=instance2
asadmin> create-system-properties --target instance3 AJP_PORT=8011

That's it!  Send questions to users@glassfish.java.net.

FAQ -

1.  "No class name for worker.worker1 worker" in the server.log. 

Make sure you have the workers.properties file and configured correctly as JkWorkersFile in httpd.conf

2.  "threadpool.max_threads_too_low" in the server.log.

Try increasing maximum threads in the http-thread-pool pool via asadmin> set configs.config.server-config.thread-pools.thread-pool.http-thread-pool.max-thread-pool-size=value or use jk-configuration-file for Connector.maxThreads.

3.  "WARNING JK properties file ... does not exist"

This is a warning message that you have jk-configuration-file defined but the file does not exist.

3.  How should I write the jk-configuration-file?

See Properties.load for key/element examples.

4.  How do I create a jk-enabled connector with a new thread pool with a minimum size of 5 and mazimum size of 150 (Apache MaxClients) for example?

asadmin> create-threadpool --minthreadpoolsize 5 --maxthreadppplsize 150  jk-thread-pool
asadmin> create-network-listener --protocol http-listener-1 --listenerport 8009 --jkenabled true --threadpool jk-thread-pool jk-connector

 

5.  Truncated downloads

See http://java.net/jira/browse/GLASSFISH-18446

Related Topics >>

Comments

What about apache+mod_shib+ajp as a solution for integrating ...

What about apache+mod_shib+ajp as a solution for integrating Shibboleth with a Glassfish application?

This was suggested to me at http://irclog.perlgeek.de/shibboleth/2013-02-13#i_6447092

Alternatives I'm considering are http://openam.forgerock.org (formerly http://en.wikipedia.org/wiki/OpenSSO ) or something along the lines of http://code.google.com/p/websso/ which uses http://www.opensaml.org

With regards to port 8009, I have a question. When using ...

With regards to port 8009, I have a question. When using Netbeans, it uses Glassfish port 8009 in order to debug the running app. Will this cause a conflict with this if by any chance they are used in the same development environment? That is, Apache and Netbeans bumping heads at port 8009?

Is 8009 a requirement or what was the reason for using that port.

Thank you very much for the article. It was just what I was looking for, but I do have this one question (for now).

Regards,

BillR

Hi Amyroh, I'm using Glassfish v3.1 and mod_jk and ...

Hi Amyroh,

I'm using Glassfish v3.1 and mod_jk and Glassfish doesn't seem to be killing my mod_jk connections that were established even though the default Glassfish timeouts haven't been changed. I've tried playing with the mod_jk settings socket_keepalive, connection_pool_timeout and socket_timeout but to no avail so far.

This is a production system so I've been scouring all the information I could find to try to make this work.

If you have any idea off the top of your head, it would be appreciated!

Cheers!

Thanks,
Charles

Hi Amyroh, I followed above step to integrate apache with ...

Hi Amyroh,
I followed above step to integrate apache with glassfish.But i'm unable to integrate.

myglassfish version is GF 3.1 and apache 2.2. I'm using fedora 11.Apache installation dir =/usr/local/apache2/../ and glassfish @ /glassfish3/../../.

In this case how to configure and integrate apache and GF.I followed above steps upto 5 and for six i used ::>
"asadmin set configs.config.server-config.network-config.network-listeners.network-listener.jk-connector.jk-enabled=true "

Becos my i created workers.properties file at /../apache2/conf/workers.properties.
At first i referred http://weblogs.java.net/blog/amyroh/archive/2009/06/running_glassfi.html.

In my http.conf, what should be my JkMount point.All my glassfish applications are in
/glassfish3/glassfish/domains/domain1/applications/app1/
And my apache web apps rootdir = /usr/local/apache2/htdocs/

my http.conf is :
JkWorkersFile /usr/local/apache2/conf/workers.properties
# Where to put jk logs
JkLogFile /usr/local/apache2/logs/mod_jk.log
# Set the jk log level [debug/error/info]
JkLogLevel debug
# Select the log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
# JkOptions indicate to send SSL KEY SIZE,
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
# JkRequestLogFormat set the request format
JkRequestLogFormat "%w %V %T"
# Send all jsp requests to GlassFish
jkMount /*.jsp worker1
# Send all webapp requests to GlassFish
JkMount /glassfish3/glassfish/domains/domain1/applications/* worker1

workers.properties:
# Define 1 real worker using ajp13
worker.list=worker1
# Set properties for worker1 (ajp13)
worker.worker1.type=ajp13
worker.worker1.host=localhost
worker.worker1.port=8009

I'm not sure my JkMount point is correct or wrong.If wrong what should me my jkmount ?

JkMount
# Send all webapp requests to GlassFish
JkMount

my mod-jk.log is :
[Tue Apr 10 20:09:36 2012] [10226:3078326128] [debug] map_uri_to_worker_ext::jk_uri_worker_map.c (1036): Attempting to map URI '/favicon.ico' from 2 maps
[Tue Apr 10 20:09:36 2012] [10226:3078326128] [debug] find_match::jk_uri_worker_map.c (850): Attempting to map context URI '/glassfish3/glassfish/domains/domain1/applications/D-MobileWebService-dev/*=worker1' source 'JkMount'
[Tue Apr 10 20:09:36 2012] [10226:3078326128] [debug] find_match::jk_uri_worker_map.c (850): Attempting to map context URI '/*.jsp=worker1' source 'JkMount'
[Tue Apr 10 20:09:36 2012] [10226:3078326128] [debug] jk_translate::mod_jk.c (3605): no match for /favicon.ico found
[Tue Apr 10 20:09:36 2012] [10226:3078326128] [debug] map_uri_to_worker_ext::jk_uri_worker_map.c (1036): Attempting to map URI '/favicon.ico' from 2 maps
[Tue Apr 10 20:09:36 2012] [10226:3078326128] [debug] find_match::jk_uri_worker_map.c (850): Attempting to map context URI '/glassfish3/glassfish/domains/domain1/applications/D-MobileWebService-dev/*=worker1' source 'JkMount'
[Tue Apr 10 20:09:36 2012] [10226:3078326128] [debug] find_match::jk_uri_worker_map.c (850): Attempting to map context URI '/*.jsp=worker1' source 'JkMount'
[Tue Apr 10 20:09:36 2012] [10226:3078326128] [debug] jk_map_to_storage::mod_jk.c (3671): no match for /favicon.ico found

Help me to fix this.Thanks in advance.

Data Truncation Issue I faced an issue when upgrading from ...

Data Truncation Issue

I faced an issue when upgrading from a working ajp13 config with GF3.1.1 to a non-working config with GF3.1.2.

Fortunately, the patches provided here resolved my issue:

http://java.net/jira/browse/GLASSFISH-18446

recently I had to upgrade glassfish to 3.1.2 - and I could ...

recently I had to upgrade glassfish to 3.1.2 - and I could not get mod_jk to work (I had a working config with GF 3.0.2)

I have setup everything as instructed, and got no errors. but when I go to url, I get a bad gateway.

In mod_jk.log, I can see the web server receiving and sending data, (debug data like: [Tue Mar 06 05:09:29 2012] [13852:1368479552] [debug] ajp_connection_tcp_get_message::jk_ajp_common.c (1259): 0360 20 20 20 20 20 20 20 20 3C 6C 69 3E 3C 61 20 74 - ........<li><a.t)

[Tue Mar 06 05:09:29 2012] [13852:1368479552] [debug] ws_write::mod_jk.c (506): written 3046 out of 3046
[Tue Mar 06 05:09:29 2012] [13852:1368479552] [debug] ajp_connection_tcp_get_message::jk_ajp_common.c (1259): received from ajp13 pos=0 len=6 max=8192
[Tue Mar 06 05:09:29 2012] [13852:1368479552] [debug] ajp_connection_tcp_get_message::jk_ajp_common.c (1259): 0000 03 00 02 0D 0A 00 00 00 00 00 00 00 00 00 00 00 - ................
[Tue Mar 06 05:09:29 2012] [13852:1368479552] [debug] ws_write::mod_jk.c (506): written 2 out of 2
[Tue Mar 06 05:09:29 2012] [13852:1368479552] [debug] ajp_connection_tcp_get_message::jk_ajp_common.c (1259): received from ajp13 pos=0 len=9 max=8192
[Tue Mar 06 05:09:29 2012] [13852:1368479552] [debug] ajp_connection_tcp_get_message::jk_ajp_common.c (1259): 0000 03 00 05 30 0D 0A 0D 0A 00 00 00 00 00 00 00 00 - ...0............
[Tue Mar 06 05:09:29 2012] [13852:1368479552] [debug] ws_write::mod_jk.c (506): written 5 out of 5
[Tue Mar 06 05:09:29 2012] [13852:1368479552] [debug] ajp_connection_tcp_get_message::jk_ajp_common.c (1259): received from ajp13 pos=0 len=2 max=8192
[Tue Mar 06 05:09:29 2012] [13852:1368479552] [debug] ajp_connection_tcp_get_message::jk_ajp_common.c (1259): 0000 05 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 - ................
[Tue Mar 06 05:09:29 2012] [13852:1368479552] [debug] ajp_process_callback::jk_ajp_common.c (1846): AJP13 protocol: Reuse is OK
[Tue Mar 06 05:09:29 2012] [13852:1368479552] [debug] ajp_reset_endpoint::jk_ajp_common.c (743): (worker1) resetting endpoint with sd = 24
[Tue Mar 06 05:09:29 2012] [13852:1368479552] [debug] ajp_done::jk_ajp_common.c (2905): recycling connection pool slot=0 for worker worker1
[Tue Mar 06 05:09:29 2012] worker1 www.mysite.com 0.323120
[Tue Mar 06 05:09:29 2012] [13852:1368479552] [debug] jk_handler::mod_jk.c (2599): Service finished with status=200 for worker=worker1

I have tried many things, such as changing to IP address from workers.properties to my laptop containing glassfish 3.0.2, so I am kind of confident it is not the mod_jk side but glassfish.

Any input on this is wellcome.

Hi, we also had a JK problem after upgrading to 3.1.2 ...

Hi,

we also had a JK problem after upgrading to 3.1.2 (from 3.0.1). In our Apache/GlassFish setup I had to disable chunking in the HTTP tab of the JK network listener to get it to work.

But on another server I run into another problem (still looking into it):

[info] ajp_send_request::jk_ajp_common.c (1574): (myinstance) all endpoints are disconnected, detected by connect check (1), cping (0), send (0)
[info] ajp_connection_tcp_get_message::jk_ajp_common.c (1223): (myinstance) can't receive the response header message from tomcat, tomcat (127.0.0.1:25010) has forced a connection close for socket 40
[error] ajp_get_reply::jk_ajp_common.c (2055): (myinstance) Tomcat is down or refused connection. No response has been sent to the client (yet)
[info] ajp_service::jk_ajp_common.c (2540): (myinstance) sending request to tomcat failed (recoverable),  (attempt=1)