• Entries (RSS)
  • Comments (RSS)

Integrating GlassFish Application server with Apache web server

Posted by | Posted in GlassFish | Posted on 14-10-2008

Tagged Under : , , , ,

A typical production topology for GlassFish will be a front ending GlassFish with Apache for serving the static files. To integrate GlassFish Application Server with Apache web server follow the below steps. My Apache version was Apache/2.2.4 (Win32).

1. Download Mod JK from http://tomcat.apache.org/download-connectors.cgi. I downloaded mod_jk-1.2.26-httpd-2.2.4.so from there.

2. Rename mod_jk-1.2.26-httpd-2.2.4.so to mod_jk.so

3. Copy mod_jk.so file to modules directory of your Apache web server.

4. Create workers.properties file. I have attached my workers.properties file for reference. Most of the cases you need to change only workers.tomcat_home (Point to your GlassFish Application server installation directory) and workers.java_home (change it to your java home directory).

# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
 
# workers.properties -
#
# This file provides jk derived plugins with the needed information to
# connect to the different tomcat workers.  Note that the distributed
# version of this file requires modification before it is usable by a
# plugin.
#
# As a general note, the characters $( and ) are used internally to define
# macros. Do not use them in your own configuration!!!
#
# Whenever you see a set of lines such as:
# x=value
# y=$(x)\something
#
# the final value for y will be value\something
#
# Normaly all you will need to do is un-comment and modify the first three
# properties, i.e. workers.tomcat_home, workers.java_home and ps.
# Most of the configuration is derived from these.
#
# When you are done updating workers.tomcat_home, workers.java_home and ps
# you should have 3 workers configured:
#
# - An ajp12 worker that connects to localhost:8007
# - An ajp13 worker that connects to localhost:8009
# - A jni inprocess worker.
# - A load balancer worker
#
# However by default the plugins will only use the ajp12 worker. To have
# the plugins use other workers you should modify the worker.list property.
#
#
 
# OPTIONS ( very important for jni mode ) 
 
#
# workers.tomcat_home should point to the location where you
# installed tomcat. This is where you have your conf, webapps and lib
# directories.
#
workers.tomcat_home=D:/Java/glassfish
 
#
# workers.java_home should point to your Java installation. Normally
# you should have a bin and lib directories beneath it.
#
workers.java_home=D:/Java/J2SE
 
#
# You should configure your environment slash... ps=\ on NT and / on UNIX
# and maybe something different elsewhere.
#
ps=\
 
#
#------ ADVANCED MODE ------------------------------------------------
#---------------------------------------------------------------------
#
 
#
#------ DEFAULT worker list ------------------------------------------
#---------------------------------------------------------------------
#
#
# The workers that your plugins should create and work with
#
# Add 'inprocess' if you want JNI connector
#worker.list=ajp12, ajp13
# , inprocess
 
#
#------ DEFAULT LOAD BALANCER WORKER DEFINITION ----------------------
#---------------------------------------------------------------------
#
 
#
# The loadbalancer (type lb) workers perform wighted round-robin
# load balancing with sticky sessions.
# Note:
#  ----> If a worker dies, the load balancer will check its state
#        once in a while. Until then all work is redirected to peer
#        workers.
worker.loadbalancer.type=lb
#worker.loadbalancer.balance_workers=ajp12, ajp13
worker.loadbalancer.balance_workers=ajp13
 
#
#------ DEFAULT JNI WORKER DEFINITION---------------------------------
#---------------------------------------------------------------------
#
 
#
# Defining a worker named inprocess and of type jni
# Note that the name and the type do not have to match.
#
worker.inprocess.type=jni
 
# Define 1 real worker using ajp13
worker.list=worker1,jkstatus	
 
# Set properties for worker1 (ajp13)
worker.worker1.type=ajp13
worker.worker1.host=localhost
worker.worker1.port=8009
worker.worker1.lbfactor=50
worker.worker1.socket_keepalive=1
worker.worker1.socket_timeout=300
worker.worker1.connection_pool_timeout=600

5. Edit the Apache configuration file (httpd.conf).

6. Add the following entries to your httpd.conf file.

LoadModule jk_module modules/mod_jk.so
JkWorkersFile D:/Apache22/conf/workers.properties
 
# Where to put jk logs
JkLogFile 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 %U %q" 
 
# Send all jsp requests to GlassFish
JkMount /*.jsp worker1
 
# Send all /TestWeb/ requests to GlassFish
JkMount /TestWeb/* worker1

Change line ‘LoadModule jk_module modules/mod_jk.so ‘ to point to your mod_jk.so path. If you have copied mod_jk.so to modules directory of Apache web server you don’t need to change this line. JkWorkersFile should point to the path where you have created your workers.properties file in step 4. JkLogFile will be the log file for mod_jk. JkLogLevel set to error for production environments.

JkMount /TestWeb/* worker1, here /TestWeb/ is the GlassFish context root of your GlassFish web application.

The documentation for the above options are :-
JkWorkersFile – the name of a worker file for the Tomcat servlet containers
JkLogFile – Full path to the Tomcat module log file
JkLogLevel – The Tomcat module log level, can be debug, info, error or emerg
JkLogStampFormat – The Tomcat module log format, follow strftime syntax
JkOptions – Set one of more options to configure the mod_jk module
JkRequestLogFormat – The mod_jk module request log format string
JkMount – A mount point from a context to a Tomcat worker

7. All the configurations required at Apache web server side is over. Now you can start your Apache web server.

8. Copy tomcat-ajp.jar from Tomcat version 5.5.23 to GlassFish installation directory/ lib folder. For me all other versions gave some kind of errors and tomcat-ajp-jar from Tomcat 5.5.23 worked fine.

9. Copy commons-logging.jar, commons-modeler.jar and log4j.jar to GlassFish installation directory/lib folder. I used commons-logging.jar, commons-modeler-2.0.1.jar and log4j-1.2.7.jar.

10. Now open you domain.xml file. The domain.xml file will be inside GlassFish_HOME\domains\domain1\config folder.

11. Search for <jvm-options> in your domain.xml file and add an entry there

<jvm-options>-Dcom.sun.enterprise.web.connector.enableJK=8009</jvm-options>

12. That’s it. You are done with integrating Apache web server with GlassFish Application Server. Now access your jsp page through Apache web server.

http://localhost/TestWeb/test.jsp.

Share

Read More

Comments

32 comments posted onIntegrating GlassFish Application server with Apache web server

  1. I am not able to find the workers.properties file.

  2. Sorry I missed the attachment. Post updated with workers.properties file.

  3. [...] was trying to integrate GlassFish Application server with Apache in a Linux box. I was following my Integrating Apache with GlassFish tutorial which I tried in my local windows box. When I restarted my Apache web server, the server [...]

  4. Very helpful post. Thank you for your efforts.

    Step 8 is clear regarding where one ought to copy files, but with the different wording in step 9, one might infer to copy the logging libs instead to one’s server instance lib, rather than the glassfish/lib directory.

    In my case, the server instance failed until I put the logging libs in the same /lib directory.

    Could you re-write Step 9 to use the same clear wording as in Step 8?

    Thanks again for your post.

    Torin…

  5. Thanks Torin. I have updated the post. Now the wording of step 9 is same as step 8. Sorry for the confusion.

  6. Where do you copy those files from, in step 9?

  7. @Thomas,

    These files can be downloaded from the following locations
    commons-logging.jar – http://commons.apache.org/logging/
    commons-modeler.jar – http://commons.apache.org/downloads/download_modeler.cgi
    log4j.jar – http://logging.apache.org/log4j/1.2/download.html

  8. Thanks for the fast reply. I downloaded those jar files.

    I must have a different version of GlassFish than you because in the next step, step 10, I do not have a domain directory in my glassfish home. I installed glassfish-installer-v2.1-b60e-windows.jar. Any hints? I have domain.xml files in two other places: lib\install\templates\enterprise and lib\install\templates\cluster. Maybe either would work.

  9. I believe the GlassFish version I have is 2.1 only. Anyways I will try to download the version you have mentioned and let me try these steps (somewhere in evening).

    Meanwhile you can try a different command to get the same result of step 10 and 11. So in your case instead of step 10 and 11, just execute the following command.

    $GLASSFISH_HOME/bin/asadmin create-jvm-options -Dcom.sun.enterprise.web.connector.enableJK=8009

    The create-jvm options command creates command-line options that are passed to the Java application launcher when Enterprise Server is started. These options are stored in the Java configuration element or profiler element of the domain.xml file.

    Please let me know if this works.

  10. I must be using an odd version of GlassFish. I installed it again and did it a little differently (making sure I didn’t change the installation directory name part-way through the process and also using the setup-cluster.xml instead of the setup.xml file). This time I do get the “…/domains/domain1/” part of its directory structure.

    But it still doesn’t quite make sense. All the documentation I find online and within the installation itself says to “Start the Application Server by clicking the Start Admin Server option within the Programs menu.” There was nothing installed in my Programs menu. I assume this means “All Programs” in the Start menu. I’m doing this on Windows Vista, 64 bit. This documentation also assumes that I was able to pick an admin username, but there was no such option. All of that information was provided by the setup-cluster.xml given during the ant build.

    This is just weird that the same instructions that tell me to run the ant build also seem to assume that I’ve run some kind of Windows installer requestion information such as the admin username. I bet whatever should be in the start menu would just run some kind of command-line program to start the service, but so far I cannot find any information online telling me how to do it other than by this item in the Programs menu.

    Any hints out there?

  11. To start your GlassFish Application server issue the command ‘asadmin.bat start-domain domain1′ from GLASS_FISH_INSTALL_DIR\bin directory. As you feel there will not be any entries in the programs menu. Once the server is started to access the admin console url use http://localhost:4848/login.jsf with default username admin and password adminadmin.

  12. With Albin Joseph’s help I added a jar file (sunpkcs11.jar) to my JDK and now I can start GlassFish.

    I then mentioned to him that there was no map for the test file http://localhost/TestWeb/test.jsp. Before waitting for his response, I added the JkMount lines (in step 6) to my virtual host section of httpd.conf (based on reading elsewhere). That helped, but there’s still more to do.

    Now I get a Service Temporarily Unavailable error when trying to access test.jsp in a browser. The error logs indicated that the port number to “tomcat” might be wrong, so I changed the port number at the end of step 4, workers.properites, to match the default http port used in the GlassFish installation (8080). I’m not sure that’s the right thing to do, but it seems to have changed the error. Now I get “Bad Gateway” in the browser and “(worker1) Tomcat is down or refused connection. No response has been sent to the client (yet)”. Now that I look at step 11 again, changing the port was probably the wrong thing to do.

    Any hints?

  13. Going back to using port 8009, here is the error I get in mod_jk.log:

    [Mon Feb 16 16:22:24 2009] [10772:10740] [debug] jk_connect.c (540): timeout 300 set for socket=780
    [Mon Feb 16 16:22:24 2009] [10772:10740] [debug] jk_connect.c (575): trying to connect socket 780 to 127.0.0.1:8009
    [Mon Feb 16 16:22:25 2009] [10772:10740] [info] jk_connect.c (593): connect to 127.0.0.1:8009 failed (errno=61)
    [Mon Feb 16 16:22:25 2009] [10772:10740] [info] jk_ajp_common.c (922): Failed opening socket to (127.0.0.1:8009) (errno=61)
    [Mon Feb 16 16:22:25 2009] [10772:10740] [error] jk_ajp_common.c (1467): (worker1) connecting to backend failed. Tomcat is probably not started or is listening on the wrong port (errno=61)
    [Mon Feb 16 16:22:25 2009] [10772:10740] [info] jk_ajp_common.c (2407): (worker1) sending request to tomcat failed (recoverable), because of error during request sending (attempt=2)
    [Mon Feb 16 16:22:25 2009] [10772:10740] [error] jk_ajp_common.c (2426): (worker1) connecting to tomcat failed.
    [Mon Feb 16 16:22:25 2009] [10772:10740] [debug] jk_ajp_common.c (743): (worker1) resetting endpoint with sd = 4294967295 (socket shutdown)
    [Mon Feb 16 16:22:25 2009] [10772:10740] [debug] jk_ajp_common.c (2850): recycling connection pool slot=0 for worker worker1

  14. I replaced the tomcat-ajp.jar mentioned in step 8 with the one in tomcat version 5.5.16, then restarted glassfish. (I also re-issued the command:

    ./asadmin.bat create-jvm-options -Dcom.sun.enterprise.web.connector.enableJK=8009

    although I don’t think this should have done anything, since I had already entered the corresponding information into the xml file.)

    Seems to be working fine now.

    Thanks.

  15. Glad to hear that it worked.

  16. Could I ask how to create a second domain in the same way?

    I have created a new domain, but when I try to wire it up to work with Apache by issuing the command:

    -Dcom.sun.enterprise.web.connector.enableJK=53720

    for the new port, it places this line

    -Dcom.sun.enterprise.web.connector.enableJK=54053

    in the file for the first domain, domain1, instead of the new domain. Manually editing the domain.xml file doesn’t work, and I’m not sure it even worked the first time.

  17. That second line should have been this:

    -Dcom.sun.enterprise.web.connector.enableJK=53720

  18. Sorry, I’m an idiot. This web form is stripping off the jvm-options in angle brackets.

  19. Alright, since I’m so brilliant, I’ll answer my own question again. I think I just needed to stop the domain, add the line in the xml file, and then start again, and be patient. It works now.

  20. I have my static content in C:\work\myProj\web and is being served up by Apache 2.2.11. I have Glassfish 2.1 as the Appserver and the context root is xxx.

    When I am trying to access using the following URL: http://myservername/xxx/index.html, it servers up the page without the static stuff.

    My httpd-vhosts.conf has the following :
    DocumentRoot “C:/work/myProj/web”
    JkMount /xxx/* worker1.

    The access log shows it is trying to access the static stuff in the following way: “GET /xxx/scripts/home.js HTTP/1.1″ 404 1018 – so what is happening is it is looking for C:\work\myProj\web\xxx\scripts\home.js. However, I believe it should be looking for C:\work\myProj\web\scripts\home.js

    Can you help me with this.

    TIA.

  21. @TIA, If you are accessing the static content with context root, it will always look for scripts directory within your context root. So I would suggest you to create an Alias in your Apache pointing to C:\work\myProj\web. Now use this Alias name to call the static content. Try this, it should work.

  22. Thanks Dude, this saved me alot of trouble. I appreciate it. Keep rocking!

    It would really help if you could write a similar article for a glassfish cluster. Thanks!

  23. I have not done clustering on GlassFish yet. But let me try on that. I feel it will be just adding a new worker in workers.properties file only.

  24. I’ve installed Apache HTTP Server 2.2.14, Glassfish (Sun Java System Application Server 9.1_01 (build local)) on Ubuntu 9.04, followed the steps but ist still doesn’t work. I think this depends on a setting in the domain.xml-file. In my file there are two possibilities to add the metioned in step 11. If i add the option at both positions, i get following message: CLI156 Could not start the domain domain1. But if i add the option only a the last position, glassfish starts without any problems. If i type in my domain http://localhost/MeinWebAppProjektnametest/ still nothing happens an i get an Apache 5.5 HTTP 404 error. It would be great if someone could help my solving this problem. Thanks for any advice.

  25. I am not sure what exactly is the problem. I hope you have added

    <jvm-options>-Dcom.sun.enterprise.web.connector.enableJK=8009</jvm-options>

    . If this is not what you have entered, please modify. If still its not working may be the port is wrong. Create an http listener using the following command

    asadmin create-http-listener --listenerport 8009 --listeneraddress 0.0.0.0 --defaultvs server jk-connector

    Hope this helps

  26. Thanks alot for you answer Albin.
    I don’t forget to add .
    But if iy try to use your command, i get following message:
    CLI020 Operand is required.

    I think there’s a problem with the port?
    If i create a listener with the Admin-Console of Glassfish (should be the same like using the comand), i get the same error: CLI156 Could not start the domain domain1

  27. After searching, i found another instruction, how to use Apache with Glassfish an tried to use it: http://technology.amis.nl/blog/5973/making-glassfish-v3-available-using-apache2-and-mod_jk . But i’ve always the same problems like CLI156 with the port…

  28. I was going through some blogs about integrating GF 3 with Apache. Seems like it is different from GF V2. The AJP support comes built in in GF. So follow the below steps to make GF V3 communicate to Apache.

    1. Follow till step 7
    2. No need of step 8, 9, 10, 11 and 12.
    3. Enable JK using the following command

    asadmin create-http-listener --listenerport 8009 --listeneraddress 0.0.0.0 --defaultvs server my-connector
     
    asadmin set configs.config.server-config.network-config.network-listeners.network-listener.jk-connector.jk-enabled=true

    If this is not working your GlassFish version might be the problem. Get the latest version.

    Btw, I have not tried these steps yet :-)

  29. Thanks alot for your answer, Albin :grin:
    Well- i tried to follow step 3 and recieved following answer- after adding the second command: asadmin set configs.config.server-config.network-config.network-listeners.network-listener.jk-connector.jk-enabled=true No object matches the specified name “configs.config.server-config.network-config.network-listeners.network-listener.jk-connector.jk-enabled” CLI137 Command set failed.

    I’ve another questions to: step 4: workers.tomcat_home=D:/Java/glassfish

    My index.html file is in this directory:
    /home/newglassfishuser/glassfishv2/domain1/applications/j2ee-modules/MeinWebAppProjektname . lib and in directories are here: /home/newglassfishuser/glassfishv2/domain1
    How do i have to set the path 1 (where index.html is) or 2. (where lib an bin are)??? (just to check if i added the correct path…)
    And one question to step 6- the httpd.conf: Is it correct to set the path JkMount /MeinWebAppProjektname/* worker1??

  30. So i’m new to these technologies, and I’ve been tasked with writing some web services for a Java app that I’ve been designing with a team. Along with the web services, we’ve also decided to create a web server to act as a central hub for all users of our software. I downloaded MAMP as it had all the things we needed to get the web server up and running, but I have been having problems integrating the glassfish app server so that I can start developing the web services.

    This tutorial is one of many that I have found, but after going through this one to step 7, I managed to hose the Apache Web Server that was running on port 80. The only information in the Apache error logs was a caught SIGTERM that fired right after I made the changes to the httpd.conf file and created the worker.properties file, and continued onto restarting apache.

    I know my problem is vague, but my limited experience with these technologies has left me clueless. :oops:

    Any opinions? :cry:

  31. Just curious— why not use Glassfish for serving static files? Unless you want some feature such as mod_rewrite, wouldn’t it be easier (and less resource-intensive) to add a virtual server in Glassfish (such as static.domain.com) and set the docroot to your static file directory?

  32. I think apache is faster for serving stating pages.

Post a Comment