Showing posts with label Apache Tomcat. Show all posts
Showing posts with label Apache Tomcat. Show all posts

Wednesday, December 9, 2009

Application Server HOW TO

http://confluence.atlassian.com/display/DOC/Configuration+Guide

Using Apache with mod_proxy

This page describes how to integrate Confluence into an Apache website, using mod_proxy. There are some common situations where you might do this:

* You have an existing Apache-based website, and want to add Confluence to the mix (eg. http://www.example.com/confluence).
* You have two or more Java applications, each running in their own application server on different ports, eg. http://localhost:8080/confluence and http://localhost:8081/jira. By setting up Apache with mod_proxy, you can have both available on the regular HTTP port (80), eg. at http://www.example.com/confluence and http://www.example.com/jira. If you are running JIRA and Confluence, we recommend this setup. It allows each app to be restarted, managed and debugged separately.

This page describes how to configure mod_proxy. We describe two options:

* If you want a URL like http://www.example.com/confluence/, go to the simple configuration.
* If you want a URL like http://confluence.example.com/, go to the complex configuration.

Simple configuration
Set the context path

First, set your Confluence application path (the part after hostname and port) correctly. Say you want Confluence available at http://www.example.com/confluence/, and you currently have it running at http://localhost:8080/. The first step is to get Confluence available at http://localhost:8080/confluence/.

To do this in Tomcat (bundled with Confluence), edit conf/server.xml, locate the "Context" definition:
1.

and change it to:
1.

Then restart Confluence, and ensure you can access it at http://localhost:8080/confluence/
Configure mod_proxy

Now enable mod_proxy in Apache, and proxy requests to the application server by adding the example below to your Apache httpd.conf (note: the files may be different on your system; the JIRA docs describe the process for Ubuntu/Debian layout):
01.# Put this after the other LoadModule directives
02.LoadModule proxy_module /usr/lib/apache2/modules/mod_proxy.so
03.LoadModule proxy_http_module /usr/lib/apache2/modules/mod_proxy_http.so
04.
05.# Put this in the main section of your configuration (or desired virtual host, if using Apache virtual hosts)
06.ProxyRequests Off
07.ProxyPreserveHost On
08.
09.
10. Order deny,allow
11. Allow from all
12.

13.
14.ProxyPass /confluence http://localhost:8080/confluence
15.ProxyPassReverse /confluence http://localhost:8080/confluence
16.
17. Order allow,deny
18. Allow from all
19.

Note to Windows Users

It is recommended that you specify the absolute path to the mod_proxy.so and mod_proxy_http.so files.
Set the URL for redirection

You will need to modify the server.xml file in your tomcat's conf directory and set the URL for redirection.

Locate this code segment
1.

And append the following segment:
1.

Replace www.example.com with the URL you wish to be redirected to.

Complex configuration

A complex configuration involves using the mod_proxy_html filter to modify the proxied content en-route. This is required if the Confluence path differs between Apache and the application server. For example:
Externally accessible (Apache) URL http://confluence.example.com/
Application server URL http://app-server.internal.example.com:8080/confluence/

Notice that the application path in the URL is different in each. On Apache, the path is /, and on the application server the path is /confluence.
For this configuration, you need to install the mod_proxy_html module, which is not included in the standard Apache distribution.

Alternative solutions are discussed below.
01.# Put this after the other LoadModule directives
02.LoadModule proxy_module modules/mod_proxy.so
03.LoadModule proxy_http_module modules/mod_proxy_http.so
04.LoadModule proxy_html_module modules/mod_proxy_html.so
05.
06.
07. ServerName confluence.example.com
08.
09. # Put this in the main section of your configuration (or desired virtual host, if using Apache virtual hosts)
10. ProxyRequests Off
11. ProxyPreserveHost On
12.
13.
14. Order deny,allow
15. Allow from all
16.

17.
18. ProxyPass / http://app-server.internal.example.com:8080/confluence
19. ProxyPassReverse / http://app-server.internal.example.com:8080/confluence
20.
21. ProxyHTMLURLMap / /confluence/
22.
23.
24. Order allow,deny
25. Allow from all
26.

27.


The ProxyHTMLURLMap configuration can become more complex if you have multiple applications running under this configuration. The mapping should also be placed in a Location block if the web server URL is a subdirectory and not on a virtual host. The Apache Week tutorial has more information how to do this.
More information

* The mod_proxy_html site has documentation and examples on the use of this module in the complex configuration.
* Apache Week has a tutorial that deals with a complex situation involving two applications and ProxyHTMLURLMap.
* Using Apache with virtual hosts and mod_proxy shows how to configure the special case where you want JIRA and Confluence running on separate application servers on virtual host subdomains.

Alternatives

If Tomcat is your application server, you have two options:

* use mod_jk to send the requests to Tomcat
* use Tomcat's virtual hosts to make your Confluence application directory the same on the app server and the web server, removing the need for the URL mapping.

If your application server has an AJP connector, you can:

* use mod_jk to send the requests to your application server.

Wednesday, September 9, 2009

Setup mod_jk on Ubuntu (Dapper) for Ofbiz

Install a few Ubuntu packages required prior to building mod_jk:

#apt-get install libtool automake autoconf apache2-threaded-dev

Download the latest version of tomcat-connectors
Build and install the mod_jk libraries with the following commands:

#tar xvzf tomcat-connectors-1.2.23-src.tar.gz
#cd tomcat-connectors-1.2.23-src/native
#./buildconf.sh
#./configure --with-apxs=/usr/bin/apxs2
#make
#make install


Short cut
#sudo apt-get install libapache2-mod-jk

This installed the libraries in /usr/lib/apache2/modules.
Create /etc/apache2/mods-enabled/jk.load:
equal to-

LoadModule jk_module /usr/lib/apache2/modules/mod_jk.so
Create /etc/apache2/mods-enabled/jk.conf:

JkWorkersFile /etc/apache2/workers.properties
JkLogFile /var/log/apache2/jk.log
JkMount /ecommerce/* worker1
JkMount /images/* worker1
JkMount /content/* worker1

Create /etc/apache2/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

There are no extras defined here. This is only to get things up and running. There are extra steps that you should take to ensure that things are secure.

Integrate Apache & Apache Tomcat in Debian How to?

What is Apache Tomcat?

Apache Tomcat is the servlet container that is used in the official Reference Implementation for the Java Servlet and JavaServer Pages technologies.
Apache Tomcat powers numerous large-scale, mission-critical web applications across a diverse range of industries and organizations

What is mod_jk?

mod_jk is a replacement to the elderly mod_jserv. It is a completely new Tomcat-Apache plug-in that handles the communication between Tomcat and Apache.

Why mod_jk?

Several reasons

mod_jserv was too complex. Because it was ported from Apache/JServ, it brought with it lots of JServ specific bits that aren't needed by Apache.

mod_jserv supported only Apache. Tomcat supports many web servers through a compatibility layer named the jk library. Supporting two different modes of work became problematic in terms of support, documentation and bug fixes. mod_jk should fix that.

The layered approach provided by the jk library makes it easier to support both Apache1.3.x and Apache2.xx.
Better support for SSL. mod_jserv couldn't reliably identify whether a request was made via HTTP or HTTPS. mod_jk can, using the newer Ajpv13 protocol.

Apache2 Installation and Configuration

For apache2 Installation click here and we will see now some configuration

Edit /etc/apache2/apache2.conf. Change

DirectoryIndex index.html index.cgi index.pl index.php index.xhtml

to

DirectoryIndex index.html index.htm index.shtml index.cgi index.php index.php3 index.pl index.xhtml

Edit /etc/mime.types and comment out the following lines:

#application/x-httpd-php phtml pht php#application/x-httpd-php-source phps
#application/x-httpd-php3 php3#application/x-httpd-php3-preprocessed php3p
#application/x-httpd-php4 php4

Edit /etc/apache2/mods-enabled/php4.conf and comment out the following lines:


# AddType application/x-httpd-php .php .phtml .php3
# AddType application/x-httpd-php-source .phps


Edit /etc/apache2/ports.conf and add Listen 443

Listen 80
Listen 443

Now we have to enable some Apache modules (SSL, rewrite and suexec):

#a2enmod ssl
#a2enmod rewrite
#a2enmod suexec
#a2enmod include

Restart Apache:

#/etc/init.d/apache2 restart

Installing JDK in Debian

In order to run Tomcat, you will need to install JDK and set the JAVA_HOME environment variable to identify the location of the JDK environment on your system.

1. You can download JDK 5.0 at http://java.sun.com/j2se/1.5.0/download.jsp.

2. Click on Download JDK 5.0 Update 6 to go to the download page.

3. Click Accept to accept the license agreement.

4. Next choose the Linux self-extracting file. This is the download for the self-extracting binary file rather than the rpm.

5. Download to your preferred download directory. Change to that directory and make it executable by executing the following command

#chmod +x jdk-1_5_0_06-linux-i586.bin

Now execute the file:

#./jdk-1_5_0_06-linux-i586.bin

You should now have a new directory called jdk1.5.0_06. Now move this directory to the location where it should be run. We chose /usr/lib/.

#mv jdk1.5.0_06 /usr/lib

Now create a symbolic link called jdk to JAVA_HOME by the following command. This allows you to easily switch back and forth between different jvms should you ever need to.

#cd /usr/lib

#ln -s jdk1.5.0_06 jdk

Now we need to set the JAVA_HOME environment variable. Add the following at the end of /etc/profile just after export PATH.

JAVA_HOME="/usr/lib/jdk"
export JAVA_HOME

/etc/profile is executed at startup and when a user logs into the system. In order to update the environment you will need to log out and log back in to the system.

Check to make sure JAVA_HOME is defined correctly by executing the command below. This should report the location of the Java SDK which should be /usr/lib/jdk.

echo $JAVA_HOME

Installing Tomcat in Debian Linux

In this section you will download and install Apache Tomcat 5.5.16. For this particular setup, there is no need to build the package from source, we will download the binary version.

1. Download the binary version to your preferred download directory from here: http://tomcat.apache.org/download-55.cgi. Choose the tar.gz from the core section for 5.5.16.

2. Now change to that directory and extract the files using the following command:

#cd /downloads #(be sure to change to your download directory)

#unp apache-tomcat-5.5.16.tar.gz

You should now have a new directory called apache-tomcat-5.5.16. Now move this directory to the location where it should be installed. Again, We chose /usr/lib/. Note that this location will be referred to as CATALINA_HOME in the Tomcat documentation.

#mv apache-tomcat-5.5.16 /usr/lib

3. Next change to the /usr/lib/ directory.

#cd /usr/lib

4. Now create a symbolic link called apache-tomcat to the CATALINA_HOME by the following command.

#ln -s apache-tomcat-5.5.16 apache-tomcat

This will save you from having to make changes to startup and shutdown scripts each time you upgrade Tomcat and if you so desire, it also allows you to keep several versions of Tomcat on your system and easily switch amongst them.

You should now be able to start and stop Tomcat from the CATALINA_HOME/bin directory. If you are using another shell other than the bash shell you will nee to add sh to the beginning of the command. You should now be able to test that Tomcat is installed by starting it and opening your browser and entering http://localhost:8080 into your browser. Port 8080 is the default port for Tomcat and can be easily changed in the /usr/lib/apache-tomcat/conf/server.xml file. (We will work with this file later on.) If you plan to access this page remotely, be sure to forward the respective port to your server's IP address within your router. You should now see the Tomcat welcome page that contains links to Tomcat documentation as well as sample JSP/Servlet scripts. Verify that Tomcat is running by executing some of the examples found on the welcome page.

#cd /usr/lib/apache-tomcat/bin

#./startup.sh

To shutdown the server, you will need to execute the following command. Feel free to try it, but for now we will leave Tomcat running.

#./shutdown.sh

Installing and configuring mod_jk

In order to make the connection between Tomcat and Apache, we will need to download and install mod_jk connector. You will find that the Apache documentation recommends that you install the packaged version of mod_jk if it is available for your particular Linux distribution. Many outdated resources recommend installing the mod_jk2 connector, but we have found that it has been deprecated and although mod_jk was developed before mod_jk2, it is still fully supported and is very stable.

1. Download the current source from the Apache archives:

http://archive.apache.org/dist/jakarta/tomcat-connectors/jk/source/jk-1.2.15/.

Download the jakarta-tomcat-connectors-1.2.15-src.tar.gz file to your /usr/src/ directory.

2. Change to the /usr/src directory.

#cd /usr/src

3. Next, extract the contents to create the /usr/src/jakarta-tomcat-connectors-1.2.15-src directory.

#unp jakarta-tomcat-connectors-1.2.15-src.tar.gz

4. Change to the /usr/src/jakarta-tomcat-connectors-1.2.15-src/jk/native directory.

#cd jakarta-tomcat-connectors-1.2.15-src/jk/native

5. Now you are ready to create the custom configure file for your system. Execute the following:

#./buildconf.sh

This will create a configure file in the /usr/src/jakarta-tomcat-connectors-1.2.15-src/jk/native directory.

6. Execute the following command in order to configure mod_jk for your system.
Important note: You will need to have apxs2 (APache eXtension tool) installed and configured with Apache. If you do not have it, as was my case, you can download and install the apache2-threaded-dev package (which replaced the former apache-dev package) from www.debian.org. At the time of this writing, the Debian package archive at www.debian.org was down and they referred me to their temporary site until they resolved their issues pdo.debian.net. I found the apache2-threaded-dev package and was able to install it successfully.

Be sure to include the correct location apxs2 on your system in the path of the command.

#./configure --with-apxs=/usr/bin/apxs2

7. Now build the mod_jk with the following:

#make

8. Finally, if you were successful with the previous commands, copy the newly created mod_jk.so to your Apache2 modules directory. My modules were located at /usr/lib/apache2/modules.

#cd apache-2.0

#cp /usr/src/jakarta-tomcat-connectors-1.2.15-src/jk/native/apache-2.0/mod_jk.so /usr/lib/apache2/modules

You now are ready to move to the next stage which is to begin configuring Apache and Tomcat. You can find more information about the mod_jk connector here http://tomcat.apache.org/connectors-doc/howto/apache.html.

Configuring Tomcat and Apache

Create the workers.properties file.

Important note: Be sure to make a backup copy of your config files before modifying.
The workers.properties file contains the details about how each process is linked to Tomcat by defining workers that communicate through the ajpv13 protocol. Refer to the Workers HowTo for more detail.

1. First create the workers.properties file in your Apache2 root directory.

#touch /etc/apache2/workers.properties

2. Next, open the workers.properties file and add the following. You can find many other examples of the workers.properties file on the internet, but this is the one that I created and it seems to work fine with the other portions that have already been configured in this tutorial.

workers.tomcat_home=/usr/lib/apache-tomcat
workers.java_home=/usr/lib/jdk
ps=/
worker.list=worker1
worker.default.port=8009
worker.default.host=localhost
worker.default.type=ajp13
worker.default.lbfactor=1

3. Save and close the file.

4. Now we need to open the /etc/apache2/apache2.conf file and add the following lines at the bottom. (httpd.conf is just for backward compatibility):

LoadModule jk_module /usr/lib/apache2/modules/mod_jk.so
# Where to find workers.properties
JkWorkersFile /etc/apache2/workers.properties
# Where to put jk
logsJkLogFile /var/log/apache2/mod_jk.log
# Set the jk log level
[debug/error/info]JkLogLevel info
# 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 servlet for context / jsp-examples to worker named worker1
JkMount /jsp-examples worker1
# Send JSPs for context /jsp-examples/* to worker named worker1
JkMount /jsp-examples/* worker1

Save and close the file.Now a final security point.

We will create a group and user tomcat tomcat like that:

#groupadd tomcat

#useradd -g tomcat tomcat

Then change the user and group of the Tomcat path:

#chown -R tomcat:tomcat /usr/lib/apache-tomcat-5.5.16

To change the password of tomcat user, with root type:

#passwd tomcat

and follow the instructions.

Then to start and stop the Tomcat server you should use the tomcat user.

#su - tomcat

Now stop and start Tomcat:

#cd /usr/lib/apache-tomcat/bin

#./shutdown.sh

#./startup.sh

And restart Apache:

#/etc/init.d/apache2 restart

Testing Your Installation

In this test, we are directing all URLs that begin with "/jsp-examples" to Tomcat.
In a real world situation, we might only direct JSPs or servlets to the JK worker.

Make sure no other server is running on the default Tomcat ports of 8005, 8009 and 8080.
Make sure no other server is running on the Apache port, which is normally 8080 or 80.

Start Tomcat first: Always start Tomcat first and then start Apache.
If you have to bounce Tomcat, remember to take down Apache first and restart it after Tomcat restarts.
Start Apache: Point your browser to http://localhost and verify that you get the default Apache page. Substitute "localhost" for the actual machine name/IP if necessary.

Point your browser to http://localhost:8080 and verify that you get the default Tomcat page.

Point your browser to http://localhost/jsp-examples/ and verify that you get the index page for the Tomcat examples.

This will be served by Apache and will indicate that you have completed your integration of Apache and Tomcat successfully.

Important References and paths

Tomcat conf

/usr/lib/apache-tomcat/conf/server.xml

Tomcat stop and start

cd /usr/lib/apache-tomcat/bin
./shutdown.sh
./startup.sh

Apache modules

/usr/lib/apache2/modules

Apache conf

/etc/apache2/workers.properties
/etc/apache2/apache2.conf
/etc/apache2/httpd.conf

Apache2

/etc/init.d/apache2 restart
/etc/init.d/apache2 stop
/etc/init.d/apache2 start

Note:Some of you might have noticed we are using unp package for extracting tar.gz file.Iy you want to know how to install this check here

Integrate Apache, Apache-Tomcat


Obtaining mod_jk






mod_jk can be obtained in two formats - binary and source.  Depending on the platform you are running your web server on, a binary version of mod_jk may be available.  It is recommended to use the binary version if one is available.  If the binary is not available, follow the instructions for building mod_jk from source.  Notes at the end of this section offer recommendations for specific platforms.
Building mod_jk

Apache

  1. Make sure your Apache has DSO support. You can check this with $APACHE_HOME/bin/httpd -l. If you see "mod_so.c" in the output, DSO support is available. If it's missing, you may have to recompile or reinstall Apache.
  2. Find out whether your Apache has EAPI support. If you compiled it yourself from source, EAPI is probably not compiled in, unless you added it yourself (perhaps with mod_ssl). You need to build mod_jk.so with or without EAPI to match your Apache configuration. If you install a mismatched mod_jk.so, $APACHE_HOME/bin/apachectl configtest will warn you.
  3. Make sure you have Perl 5 installed. The apxs script used to build the module is written in Perl.
  4. Change directory to TOMCAT_HOME/native/mod_jk/apache1.3 (or apache2.0).
  5. Build mod_jk.so. Following are three techniques you can try, in order of simplicity:
    1. Run the build script for your platform.  If a build script is not available for your platform, you may be able to build mod_jk using ./build-unix.sh. This script will set some variables, call apxs as below, and try to copy mod_jk.so to $APACHE_HOME/libexec. If it fails, you need to do the following manually:
      • set JAVA_HOME in your shell, e.g. "set JAVA_HOME=/usr/local/jdk1.3.1; export JAVA_HOME"
      • set APACHE_HOME in your shell, e.g. "set APACHE_HOME=/usr/local/apache; export APACHE_HOME"
      • uncomment the following line in the build-unix.sh file, replacing "linux" with the name of your platform as specified in the Java include directory for your installation
      • # JAVA_INCLUDE="-I ${JAVA_HOME}/include -I ${JAVA_HOME}/include/linux"
    2. If build-unix.sh fails, you may have better luck with the Makefiles in the same directory, e.g. "make -f Makefile.linux mod_jk.so"
    3. Finally, you can try to build it manually. Run the apxs command that came with your apache distribution (hint: look in /usr/local/apache/bin, /usr/sbin, or wherever you installed apache). Type the command all on one line.
      For Linux:
      apxs -o mod_jk.so -I../jk -I/usr/local/jdk/include -I/usr/local/jdk/include/linux -c *.c ../jk/*.c
      Your build may fail because the object files from the ../jk directory have been compiled to the current directory, rather than their source directory. Running gcc -shared -o mod_jk.so *.o should finish the build. 
       

Configuring Apache

This section details the configuration that is required for the Apache Web Server to support mod_jk.

Removing mod_jserv directives

If you've previously configured Apache to use mod_jserv, remove any ApJServMount directives from your httpd.conf. If you're including tomcat-apache.conf or tomcat.conf, you'll want to remove them as well - they are specific to mod_jserv.  The mod_jserv configuration directives are not compatible with mod_jk!

Configure Apache to use mod_jk

The simplest way to configure Apache to use mod_jk is to use Tomcat to generate the mod_jk configuration file and put the following include directive at the end of your Apache httpd.conf file (make sure you replace TOMCAT_HOME with the correct path for your Tomcat installation: Include TOMCAT_HOME/conf/auto/mod_jk.conf
Example:
Include /usr/local/jakarta-tomcat/conf/auto/mod_jk.conf
This will tell Apache to use directives in the mod_jk.conf file in the Apache configuration.  This file is created by starting Tomcat with the "jkconf" option. Tomcat will initialize, write the configuration file and then exit. This may be done while an instance of Tomcat is running. Options for controlling how the mod_jk configuration file is generated are described in the configuring Tomcat section below [Configuring Tomcat].
NOTE:  If you plan to use the Tomcat generated configuration, skip the rest of this section and continue with the Configuring Tomcat section.
Custom configurations can be created by enabling the auto-configuration and copying the TOMCAT_HOME/conf/auto/mod_jk.conf file to your own configuration file, such as TOMCAT_HOME/conf/jk/mod_jk.conf.
The basic configuration is as follows:
  • You will need to instruct Apache to load Tomcat. This can be done with Apache's LoadModule and AddModule configuration directives.
  • You must inform mod_jk the location of your workers.properties file. Use mod_jk's JkWorkersFile configuration directive.
  • You should specify a location where mod_jk is going to place its log file and a log level to be used. Use the JkLogFile and JkLogLevel configuration directives. Possible log levels are debug, info, error and emerg. If the JkLogLevel is not specified, no log is generated.
  • The directive JkLogStampFormat will configure the date/time format found on mod_jk logfile. Using strftime() format string it's set by default to "[%a %b %d %H:%M:%S %Y] "
A simple example would be to include the following lines in your httpd.conf file:
LoadModule    jk_module  libexec/mod_jk.so
AddModule     mod_jk.c
JkWorkersFile /usr/local/jakarta-tomcat/conf/workers.properties
JkLogFile     /usr/local/apache/logs/mod_jk.log
JkLogLevel    info
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "

Assigning URLs to Tomcat

If you have created a custom or local version of mod_jk.conf as noted above, you can change settings such as the workers or URL prefix. Use mod_jk's JkMount directive to assign specific URLs to Tomcat. In general the structure of a JkMount directive is:
JkMount  
For example the following directives will send all requests ending in .jsp or beginning with /servlet to the "ajp13" worker, but jsp requests to files located in /otherworker will go to "remoteworker".
JkMount /*.jsp ajp13
JkMount /servlet/* ajp13
JkMount /otherworker/*.jsp remoteworker
You can use the JkMount directive at the top level or inside sections of your httpd.conf file.
 

 Table 4 - Excerpt from Apaches httpd.conf showing JK directives.

# Load mod_jk
#
LoadModule    jk_module  libexec/mod_jk.so
AddModule     mod_jk.c

# Configure mod_jk
#
JkWorkersFile /usr/local/jakarta-tomcat/conf/jk/workers.properties
JkLogFile     /usr/local/apache/logs/mod_jk.log
JkLogLevel    info

# First Virtual Host.
#

  DocumentRoot /web/host1
  ServerName host1.apache.org
  JkMount /*.jsp ajp13
  JkMount /servlet/* ajp13


# Second Virtual Host. Also accessible via HTTPS
#

  DocumentRoot /web/host2
  ServerName host2.apache.org
  JkMount /*.jsp ajp13
  JkMount /servlet/* ajp13



  DocumentRoot /web/host2
  ServerName host2.apache.org
  SSLEngine On
  JkMount /*.jsp ajp13
  JkMount /servlet/* ajp13