Tomcat and Apache

From ArchWiki
Revision as of 00:15, 24 July 2005 by Cactus (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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. It does not describe how to write Servlets and JSPs; tutorials can be found on the web.

Install Packages

You need tomcat-4.1.27-1.tar.gz and jk2-2.0.2.tar.bz2 from ftp://ftp.archlinux.org/incoming/ Neither of these is available via pacman yet. Install these packages:

tar xfvz tomcat-4.1.27-1.tar.gz
cd tomcat
pacman -U tomcat-4.1.27-1.pkg.tar.gz
cd ..
tar xfvj jk2-2.0.2.tar.bz2
cd jk2
pacman -U jk2-2.0.2-1.pkg.tar.gz

Edit the /etc/conf.d/tomcat file. Change JAVA_HOME=/opt/j2sdk to JAVA_HOME=/opt/java

Test Tomcat

Run in Terminal (as root)

/etc/rc.d/tomcat start

Tomcat should be running. Test by visiting http://localhost:8080/ in a web browser. You can browse the JSP and servlet examples if you like.

This is all that is needed to run Tomcat as a stand-alone server. You can add new webapp directories to the /opt/tomcat/webapps directory. Optionally, if you want to place webapps in a different directory, you can make /opt/tomcat/webapps/ a symbolic link to another directory. For example, if you wanted to place your web applications in /home/httpd/tomcat run these commands (as root):

cd /opt/tomcat
mv webapps /home/httpd/tomcat
ln -s /home/httpd/tomcat webapps

You can also place symbolic links within the webapps directory.

If you wish tomcat to start on bootup:
Edit /opt/tomcat/bin/catalina.sh and add this line at the top:

JAVA_HOME=/opt/java

This is needed because JAVA_HOME is not set when the daemons are started
Edit /etc/rc.conf:

Daemons=(some daemons now add tomcat)

Or add this line to rc.local:

/etc/rc.d/tomcat start

Configure Apache

Do this only if you want to access Tomcat servlets and JSPs from within Apache

Install and configure Apache as in the first part of Apache, PHP, and MySQL. You may install PHP and MySQL at this time if you want them.

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

LoadModule jk2module           lib/apache/modjk2.so

Add these lines below the ~LoadModule section:

<IfModule mod_jk2.c>
  JKSet config:file /etc/httpd/conf/workers2.properties
</IfModule>
</verbatim>

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

[shm]
file=/var/run/shm.file
size=1048576
[channel.socket:localhost:8009]
port=8009
host=127.0.0.1
[ajp13:localhost:8009]
channel=channel.socket:localhost:8009
[uri:/examples/*]


Start Apache. Run in terminal (as root):

/etc/rc.d/httpd start

Only run httpd after tomcat is started.

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

If you want to have URLs other than examples map to tomcat, add lines of the form [uri:/someapp/*] to your workers2.properties file. The someapp will map http://localhost/someapp/ to /opt/tomcat/webapps/someapp/ as interpretted by tomcat. There are more complex workers2.properties configurations; search the web 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: http://www.mysql.com/products/connector-j/

Untar the driver and copy =mysql-connector-java-3.0.11-stable-bin.jar into /opt/j2re/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/j2re/lib/ext

start MySQL if it isn't already running. (as root):

/etc/rc.d/mysql start

Test that the driver can be loaded:
Save this as ~TestMysql.java

    public class TestMysql {
        public static void main(String[[]] args) {
            try {
                Class.forName(\"com.mysql.jdbc.Driver\").newInstance();
            } catch (Exception e) {
                System.out.println(\"The driver couldn't be loaded\");
                return;
            }
            System.out.println(\"The driver was loaded\");
        }
    }

Compile the file:

$ javac TestMysql.java

Run the file

$ java TestMysql

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

  1. 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.