Tomcat stuff

How to connect apache and tomcat

mod_proxy_ajp is an Apache module which can be used to forward a client HTTP request to an internal Tomcat application server using the AJP protocol. Make sure this line is in the httpd.conf:

LoadModule proxy_ajp_module modules/mod_proxy_ajp.so

Add this info into a VirtualHost:

Listen 1989
NameVirtualHost *:1989

ServerName localhost
ErrorLog /var/log/apache2/ajp.error.log
CustomLog /var/log/apache2/ajp.log combined

AddDefaultCharset Off
Order deny,allow
Allow from all

ProxyPass / ajp://localhost:8009/
ProxyPassReverse / ajp://localhost:8009/

Make sure that Tomcat is listening on port 8009 (will show as java listening):

netstat -ntlp

should show a line like:

tcp        0      0 :::8009                     :::*                        LISTEN      1329/java

A web client will now be able to connect through HTTP to http://IPADDDR:1989/, the mod_proxy_ajp will forward your request transparently using the AJP protocol to the tomcat application server on port 8009. Remember to open 1989 on the firewall if appropriate.

At this point, the servlet container is ready to start processing the request. Tomcat can send the following messages back to the web server:

SEND_HEADERS
Send a set of headers back to the browser.
SEND_BODY_CHUNK
Send a chunk of body data back to the browser.
GET_BODY_CHUNK
Get further data from the request if it hasn’t all been transferred yet. This is necessary because the packets have a fixed maximum size and arbitrary amounts of data can be included the body of a request (for uploaded files, for example). (Note: this is unrelated to HTTP chunked transfer).
END_RESPONSE
Finish the request-handling cycle.

How to deploy a war file

Detemine webapps location
Drop sample.war in that location (sample war file is available at http://tomcat.apache.org/tomcat-6.0-doc/appdev/sample/sample.war )
Check that it deploys by checking for a directory call sample in your webapps directory
Browse to http://ServerIP:8080/sample
Should see ‘Sample “Hello, World” Application’

Note: Any web application archive file within the application base (appBase) directory that does not have a corresponding directory of the same name (without the “.war” extension) will be automatically expanded, unless the unpackWARs property is set to false. If you redeploy an updated WAR file, be sure to delete the expanded directory when restarting Tomcat, so that the updated WAR file will be re-expanded (note that the auto deployer, if enabled, will automatically expand the updated WAR file once the previously expanded directory is removed).

Troubleshooting

Tomcat logs to $TOMCAT_HOME/logs/catalina.out

How to increase java heap size

If you are experiencing performance issues with Tomcat, a common cause is the lack of JVM (Java Virtual Machine) memory allocation. The default setting of the maximum heap size is 64MB or 128MB. You can increase the maximum heap size of applications by setting the -Xmx JVM parameter.

For example -Xmx512m allows maximum 512MB heap to be allocated for the JVM.
Check both CATALINA_OPTS and JAVA_OPTS environment variables. Set the environment variables to a higher value. For example if the JAVA_OPTS variable is containing a minimum of 64MB and 128MB maximum heap size, increase it to 128MB and 256MB values.

OLD: JAVA_OPTS=”-Xms64m -Xmx128m -Dsun.rmi.dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000″

OLD: JAVA_OPTS=”-Xms128m -Xmx256m -Dsun.rmi.dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000″

Make sure not to use so high a value that exceeds your physical RAM’s size – otherwise it will be paged to the harddisk which could cause more performance issues.

How to rotate Tomcat’s catalina.out log

Create:
/etc/logrotate.d/tomcat

/var/log/tomcat/catalina.out {
copytruncate
daily
rotate 7
compress
missingok
size 5M
}

Run the following command to run the cron job manually

/usr/sbin/logrotate /etc/logrotate.conf

Tomcat uses Log4j for everything else, but catalina.out gets a copy of everything printed to stdout and stderr.

About vicki

Welcome to the Sovereign Republic of Vickistan. I am the President here. Lucky me! No taxes or laws yet. Lucky you!
This entry was posted in Linux. Bookmark the permalink.