Tomcat

From GeilThings

Jump to: navigation, search
Tomcat
General
Version 7.0.41
Stable 7.0.42
Function Web Server
Files
Config File /opt/tomcat/bin/*, /opt/tomcat/conf/*
Log File /opt/tomcat/logs/*
Scripts
Version Script /opt/tomcat/bin/version.sh
Start /opt/tomcat/bin/startup.sh
Stop /opt/tomcat/bin/shutdown.sh

Contents

General

  • Reduce memory: Either modify JAVA_OPTS as follows or add a line to /opt/tomcat/bin/catalina.sh:
    • JAVA_OPTS="$JAVA_OPTS -Xms32m -Xmx64m"
  • Version: /opt/tomcat/bin/version.sh

Install Tomcat

RAWNEWVERSION=`curl -d "action=ask&query=[[Tomcat]]|%3FStable&format=json" http://www.geilthings.com/api.php` 
NEWVERSION=`echo "$RAWNEWVERSION" | grep -Po '^.*?\K(?<=Stable\":\[\").*?(?=\")'`  
echo $NEWVERSION
 
NEWVERSIONMAJOR=`echo "$NEWVERSION" | grep -Po '^.*?\K(?<=^).*?(?=\.)'`
echo $NEWVERSIONMAJOR
 
cd /opt
wget http://apache.lehtivihrea.org/tomcat/tomcat-${NEWVERSIONMAJOR}/v${NEWVERSION}/bin/apache-tomcat-${NEWVERSION}.tar.gz
tar -zxvf apache-tomcat-${NEWVERSION}.tar.gz
 
# Create softlink to facilitate administration.
cd /opt
ln -s apache-tomcat-${NEWVERSION} /opt/tomcat
 
# Also set CATALINA_HOME:
export CATALINA_HOME=/opt/tomcat
 
# set CATALINA_HOME in my bashrc profile and/or /etc/profile:
# edit /username/.bashrc
# add line:
# export CATALINA_HOME=/opt/tomcat
 
# Test.
cd /opt/tomcat/bin/
./startup.sh

Configure Tomcat

Install APR

  • Install the Apache Portable Runtime (APR) based Native library for Tomcat for better scalability and performance.
  • http://tomcat.apache.org/tomcat-7.0-doc/apr.html
  • If it is not installed, the log file catalina.out will contain:
    • INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path:
yum install apr apr-devel
 
cd /opt/tomcat/bin
tar -zxvf tomcat-native.tar.gz
# Check which version is currently in the new tomcat directory before upgrading!!!
cd tomcat-native-1.1.27-src/jni/native/
./configure --with-apr=/usr/bin/apr-1-config
make
make install
# Success. Message:
# ...
# Libraries have been installed in:
#   /usr/local/apr/lib
# If you ever happen to want to link against installed libraries
# ... 
# - add LIBDIR to the 'LD_LIBRARY_PATH' environment variable
# - add LIBDIR to the 'LD_RUN_PATH' 
 
# Added /usr/local/apr/lib to the end of LD_LIBRARY_PATH in /etc/profile
# Same for the console:
# CentOS 5.6, 5.7: 
# LD_LIBRARY_PATH=/usr/lib/oracle/xe/app/oracle/product/10.2.0/server/lib:/usr/lib/oracle/10.2.0.3/client64/lib:/usr/local/pgsql/lib:/usr/local/apr/lib
# export LD_LIBRARY_PATH
# CentOS 6.2:
# LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/apr/lib
# export LD_LIBRARY_PATH
 
# Restart Tomcat.
/opt/tomcat/bin/shutdown.sh
/opt/tomcat/bin/startup.sh
 
# In /opt/tomcat/logs/catalina.out:
 
Nov 27, 2011 3:04:05 PM org.apache.catalina.core.AprLifecycleListener init
INFO: Loaded APR based Apache Tomcat Native library 1.1.22.
Nov 27, 2011 3:04:05 PM org.apache.catalina.core.AprLifecycleListener init
INFO: APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
 
# Success.

Memory

  • Reduce memory: Either modify JAVA_OPTS as follows or add a line to /opt/tomcat/bin/catalina.sh:
    • JAVA_OPTS="$JAVA_OPTS -Xms32m -Xmx64m"

Apache and Tomcat

  • See archlinux: https://wiki.archlinux.org/index.php/Tomcat_and_Apache
  • From above: "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>:
  • Add to the vhost config file:
# e.g. url http://www.geilthings.com/helloworld_java/* should be redirected to Tomcat's app myjava:
ProxyPass /helloworld_java ajp://localhost:8009/myjava
ProxyPassReverse /helloworld_java ajp://localhost:8009/myjava
  • Under /opt/tomcat/conf/Catalina/localhost, create the .xml file(s) with the description of the application, e.g., in a file named helloworld_java.xml, insert:
<Context path="/helloworld_java" docBase="/path/to/myjava" reloadable="true" >
</Context>
/opt/tomcat/bin/shutdown.sh
/opt/tomcat/bin/startup.sh
httpd -k restart

Upgrade Tomcat

APPNAME=tomcat
 
RAWOLDVERSION=`/opt/tomcat/bin/version.sh | grep "Server version"`
OLDVERSION=${RAWOLDVERSION:30}
echo $OLDVERSION
 
RAWNEWVERSION=`curl -sd "action=ask&query=[[Tomcat]]|%3FStable&format=json" http://www.geilthings.com/api.php` 
NEWVERSION=`echo "$RAWNEWVERSION" | grep -Po '^.*?\K(?<=Stable\":\[\").*?(?=\")'`  
echo $NEWVERSION
NEWVERSIONMAJOR=`echo "$NEWVERSION" | grep -Po '^.*?\K(?<=^).*?(?=\.)'`
echo $NEWVERSIONMAJOR
 
# Stop Tomcat.
/opt/tomcat/bin/shutdown.sh
 
# Download new version.
cd /opt
wget http://apache.lehtivihrea.org/tomcat/tomcat-${NEWVERSIONMAJOR}/v${NEWVERSION}/bin/apache-tomcat-${NEWVERSION}.tar.gz
tar -zxvf apache-tomcat-${NEWVERSION}.tar.gz
 
# Backup interesting files.
DATE=`date +%Y-%m-%d`
# The script breaks if variable names are not enclosed with {}
BACKUPDIR=/home/backup/${APPNAME}/${APPNAME}_${OLDVERSION}_${DATE}
 
# Create backup directory if it does not exist.
if [ ! -d "${BACKUPDIR}" ]
then
    mkdir -m 755 -p ${BACKUPDIR}
fi
 
cp -p /opt/tomcat/bin/catalina.sh $BACKUPDIR/catalina.sh
cp -pr /opt/apache-tomcat-${OLDVERSION}/conf/Catalina $BACKUPDIR/
 
# Edit any changes in the configuration files.
# e.g. in /opt/tomcat/bin/catalina.sh, to use less memory add (uncommented)
# JAVA_OPTS="$JAVA_OPTS -Xms32m -Xmx64m"
# right after the JAVA_OPTS block.
# ...
 
# Add applications' .xml files to the localhost directory.
# /opt/apache-tomcat-${NEWVERSION}/conf/Catalina/localhost
cp -pr /opt/apache-tomcat-${OLDVERSION}/conf/Catalina /opt/apache-tomcat-${NEWVERSION}/conf
 
# Copy all non-tomcat-native libraries from the old Tomcat installation to the new one.
# First make a copy just in case.
cp -pr /opt/apache-tomcat-${NEWVERSION}/lib /opt/apache-tomcat-${NEWVERSION}/lib_original
# Now do it with cp -pru. Option u (from cp --help): copy only when the SOURCE file is newer
#                                 than the destination file or when the
#                                 destination file is missing
cp -pru /opt/apache-tomcat-${OLDVERSION}/lib /opt/apache-tomcat-${NEWVERSION}
# A tomcat native library got a new version number (ecj, eclipse java compiler, the file should be called ejc).
# Meaning: It is probably not a good practice to add libraries to the tomcat lib directory.
 
# Copy all applications from the old tomcat installation to the new one.
 
# First make a copy just in case.
cp -pr /opt/apache-tomcat-${NEWVERSION}/webapps /opt/apache-tomcat-${NEWVERSION}/webapps_original
 
# Delete all new applications (I don't use the manager or ROOT).
rm -fr /opt/apache-tomcat-${NEWVERSION}/webapps/*
# Copy old applications.
cp -pr /opt/apache-tomcat-${OLDVERSION}/webapps /opt/apache-tomcat-${NEWVERSION}
 
# Modify the tomcat soft link.
rm -f /opt/tomcat
ln -s /opt/apache-tomcat-${NEWVERSION} /opt/tomcat
 
# Install the Apache Portable Runtime (APR); see above.
# ...
 
# Start Tomcat.
/opt/tomcat/bin/startup.sh
 
# Test Tomcat.
ps aux | grep tomcat
# A service should be running.
 
# Check version.
/opt/tomcat/bin/version.sh
 
# Check that Tomcat is running with APR; in /opt/tomcat/logs/catalina.out:
# ... org.apache.catalina.core.AprLifecycleListener init
# INFO: Loaded APR based Apache Tomcat Native library 1.1.24 using APR version 1.3.9.
# ... org.apache.catalina.core.AprLifecycleListener init
# INFO: APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
 
# Test applications.
 
# Rename the old version directory and delete it some weeks later.
mv /opt/apache-tomcat-${OLDVERSION} /opt/old_apache-tomcat-${OLDVERSION}

Versions

Software name Version number Version date
Tomcat 7.0.34
7.0.33
7.0.35
7.0.37
7.0.39
7.0.40
7.0.41
14 December 2012
22 November 2012
17 January 2013
19 February 2013
29 March 2013
18 May 2013
22 June 2013

Comments

blog comments powered by Disqus