Tomcat and Apache

From ArchWiki
Jump to: navigation, search

Merge-arrows-2.pngThis article or section is a candidate for merging with Tomcat.Merge-arrows-2.png

Notes: please use the second argument of the template to provide more detailed indications. (Discuss in Talk:Tomcat and Apache#)

This document describes the steps needed to install Apache Tomcat. It also optionally describes how to integrate Tomcat with the Apache Web Server, and how to configure MySQL to work with Tomcat Servlets and JSPs.


Install and configure Apache as in the Apache HTTP Server.

Install and configure Tomcat as described in Tomcat.

Configure Apache

Without mod_jk

If you have mod_proxy installed (default since Apache 2.0) you only need to include two directives in your httpd.conf file for each web application that you wish to forward to Tomcat 5. For example, to forward an application at context path /myapp:

ProxyPass         /myapp  http://localhost:8080/myapp
ProxyPassReverse  /myapp  http://localhost:8080/myapp

Note: starting from Apache 2.2 included mod_proxy supports the AJP protocol and so it's a viable alternative to mod_jk (this package). It's far easier to configure in httpd.conf or inside a <VirtualHost>:

ProxyPass / ajp://
ProxyPassReverse / ajp://

Instead of / you can map APPNAME to an arbitrary web path. mod_jk (described below) should be used only if its advanced features are needed.

Using mod_jk

Install the mod_jkAUR package.

Edit /etc/httpd/conf/httpd.conf
Add this line to the end of the LoadModule section:

LoadModule jk_module               modules/

Add these lines below the LoadModule section:

<IfModule jk_module>
    JkWorkersFile   /etc/httpd/conf/
    JkShmFile       /var/run/shm.file
    JkShmSize       1048576

Create the file /etc/httpd/conf/ It should contain the following:

# Define some properties
# Define worker's properties
worker.worker2.mount=/jsp-examples /jsp-examples/*

Restart Apache (httpd.service).

Visit http://localhost/jsp-examples The Tomcat JSP examples should be visible.

If you want to have URLs other than examples map to tomcat, modify the .mount attribute like

worker.worker2.mount=/jsp-examples /jsp-examples/* /someapp /someapp/* 

to your file. The someapp will map http://localhost/someapp/ to /opt/tomcat/webapps/someapp/ as interpreted by tomcat. There are more complex configurations; search the website for more info.

Configure MySQL

Do this section only if you want to connect to MySQL from within Tomcat or the Java environment in general.

Review the MySQL documentation and download the driver. 3.0 is a good choice:

Untar the driver and copy =mysql-connector-java-3.0.11-stable-bin.jar into /opt/java/jre/lib/ext

tar xfvz mysql-connector-java-3.0.11-stable.tar.gz
cp mysql-connector-java-3.0.11-stable/mysql-connector-java-3.0.11-stable-bin.jar /opt/java/jre/lib/ext

Restart MySQL (mysqld.service).

Test that the driver can be loaded:
Save this as

    public class TestMysql {
        public static void main(String[] args) {
            try {
            } catch (Exception e) {
                System.out.println("The driver couldn't be loaded");
            System.out.println("The driver was loaded");

Compile the file:

$ javac

Run the file

$ java -classpath :/opt/java/jre/lib/ext TestMysql

It will output "The driver was loaded" if the driver is available, otherwise "The driver couldn't be loaded"

You should be able to use the driver using DriverManager.getConnection() in Java programs now. It should also automatically be available to Tomcat servlets and JSPs. See The Mysql Connector/J documentation for more information.