Archive for the ‘Linux’ Category.

Multiple Tomcat instances on Ubuntu

I’m spent about 6 hours total working on getting multiple instance of Tomcat to run on Ubuntu and finally wrote some scripts that I’d like to share with folks. These scripts do the following:

1. Setup the box to host multiple instances (JVMs) running tomcat

This process cleans the old layout and sets up a new layout that allows for multiple instances. This includes a number of “instances” directories in /var/lib/tomcat5.5, /etc/defaults/tomat5.5 (a new directory) and /var/log/tomcat5.5.

2. Create new instances

This allows you to setup a new instance quickly. It creates all the necessary directories and also installs the new instance into the init scripts an run levels.

Things the scripts don’t do:

- Setup multiple mod_jk configurations and apache virtual hosts
- Any SSL or security management

Here’s how to use the scripts:

1. First make sure you have Java and Tomcat installed!

bash$ sudo apt-get install sun-java6-jdk tomcat5.5

2. Grab ALL of the scripts and place them in some directory (anywhere is fine)

3. Run the multiple-instances.sh script as root from the directory you put it in

bash$ sudo ./multiple-instances.sh

4. Run the new-instance.sh script to create a new instance

bash$ new-instance.sh <instance-name>

5. Configure the new server by editing the files in /var/lib/tomcat5.5/instances/<instance-name> (mainly conf/server.xml)

6. Make sure each instance has a unique port for everything including the AJP, all listeners and the container

7. Fire it up!

bash$ sudo /etc/init.d/tomcat5.5_<instance-name> start

8. Check the logs in /var/log/tomcat5.5/instances/<instance-name>/catalina.out

Due to security considerations, I’ve included the scripts inline. Any comments are welcome:

multiple-instances.sh

#!/bin/bash

# Verify we are root
if [ "$USER" != "root" ]; then
  echo "You must run this script as root"
  exit 1
fi

# Shutdown any running instace
if ! /etc/init.d/tomcat5.5 stop; then
  echo "Unable to stop the running tomcat instance. Please stop it before running this script."
  exit 1
fi

# Copy over the tomcat.sh file before we change directories
if [ ! -f tomcat.sh ]; then
  echo "The tomcat.sh file is missing. It must be in the current directory so it can be placed"
  echo "into the new layout"
  exit 1
fi
cp tomcat.sh /etc/tomcat5.5
chmod go-wx /etc/tomcat5.5/tomcat.sh

# Now, head out and clean up
if ! cd /var/lib/tomcat5.5; then
  echo "You must first install tomcat from the apt repository"
  exit 1
fi

# Make the instances layout
mkdir instances
chown -R tomcat55:nogroup instances
chmod -R o-rwx instances
chmod -R g+w instances

# Clean up old layout
rm work
rm -rf webapps
rmdir temp
rmdir shared/classes
rmdir shared/lib
rmdir shared
rm logs
rm -rf conf

cd /usr/share/tomcat5.5
rm conf
rm logs
rm shared
rm temp
rm work

# Clean up defaults for the template script
cd /etc/default/
mv tomcat5.5 tomcat-bak
mkdir -p tomcat5.5/instances tomcat5.5/template
mv tomcat-bak tomcat5.5/template/tomcat5.5
chown -R root:root tomcat5.5
chmod -R go-w tomcat5.5
chmod -R g+r tomcat5.5

# Clean up the logs
cd /var/log/tomcat5.5
mkdir old
mv * old
mkdir instances
chown -R tomcat55:nogroup *
chmod -R o-rwx old instances
chmod -R g+w old instances

# Remove the old init script and turn off all script links for the run levels
rm /etc/init.d/tomcat5.5
update-rc.d -f tomcat5.5 remove

echo "Successfully setup the machine for multiple tomcat instances and cleaned up the single instance layout"

new-instance.sh

#!/bin/bash

if [ "$USER" != "root" ]; then
  echo "You must run this script as root"
  exit 1
fi

if [ $# != 1 ]; then
  echo "Usage: new-instance.sh "
  exit 1
fi

# Setup the instance
cd /var/lib/tomcat5.5/instances
if [ -d $1 ]; then
  echo "Instance $1 already created."
  exit 1
fi
mkdir $1

cd $1
mkdir conf webapps bin temp
if ! cp /etc/tomcat5.5/web.xml conf; then
  echo "Unable to create new instance $1 because /etc/tomcat5.5/web.xml doesn't appear to exist"
  exit 1
fi

if ! cp /etc/tomcat5.5/server.xml conf; then
  echo "Unable to create new instance $1 because /etc/tomcat5.5/server.xml doesn't appear to exist"
  exit 1
fi

if ! sed "s/@INSTANCE_NAME@/$1/g" /etc/tomcat5.5/tomcat.sh > bin/tomcat.sh; then
  echo "Unable to find the custom tomcat.sh file in /etc/tomcat5.5. This file must exist."
  exit 1
fi
chown -R tomcat55:nogroup /var/lib/tomcat5.5/instances/$1
chmod -R o-rwx /var/lib/tomcat5.5/instances/$1
chmod -R g+w /var/lib/tomcat5.5/instances/$1
chmod ug+rx bin/tomcat.sh

# Setup the logs
mkdir /var/log/tomcat5.5/instances/$1
chown -R tomcat55:nogroup /var/log/tomcat5.5/instances/$1
chmod -R o-rwx /var/log/tomcat5.5/instances/$1
chmod -R g+w /var/log/tomcat5.5/instances/$1
ln -s /var/log/tomcat5.5/instances/$1 logs

# Create catalina.policy (for the security manager)
echo "// This file is an example of a policy file. You can edit this file for the specific instance" > conf/catalina.policy.example
echo ""  >> conf/catalina.policy.example
cat /etc/tomcat5.5/policy.d/*.policy >> conf/catalina.policy.example
chown tomcat55:nogroup conf/catalina.policy.example
chmod o-rwx conf/catalina.policy.example
chmod g+w conf/catalina.policy.example

# Setup auto start
ln -s /var/lib/tomcat5.5/instances/$1/bin/tomcat.sh /etc/init.d/tomcat5.5_$1
update-rc.d tomcat5.5_$1 defaults 90

tomcat.sh

#!/bin/sh
#
# /etc/init.d/tomcat5.5 -- startup script for the Tomcat 5 servlet engine
#
# Written by Miquel van Smoorenburg .
# Modified for Debian GNU/Linux	by Ian Murdock .
# Modified for Tomcat by Stefan Gybas .
#
### BEGIN INIT INFO
# Provides:          tomcat
# Required-Start:    $local_fs $remote_fs $network
# Required-Stop:     $local_fs $remote_fs $network
# Should-Start:      $named
# Should-Stop:       $named
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start Tomcat.
# Description:       Start the Tomcat servlet engine.
### END INIT INFO

set -e

PATH=/bin:/usr/bin:/sbin:/usr/sbin
NAME=tomcat5.5
DESC="Tomcat servlet engine"
DAEMON=/usr/bin/jsvc
CATALINA_HOME=/usr/share/$NAME
INSTANCE_NAME=@INSTANCE_NAME@
DEFAULT=/etc/default/$NAME/instances/$INSTANCE_NAME

. /lib/lsb/init-functions
. /etc/default/rcS

# The following variables can be overwritten in $DEFAULT

# Run Tomcat 5 as this user ID
TOMCAT5_USER=tomcat55

# The first existing directory is used for JAVA_HOME (if JAVA_HOME is not
# defined in $DEFAULT)
JDK_DIRS="/usr/lib/jvm/java-6-sun /usr/lib/jvm/java-1.5.0-sun /usr/lib/j2sdk1.4-sun /usr/lib/j2sdk1.4-blackdown /usr/lib/j2se/1.4 /usr/lib/j2sdk1.5-sun /usr/lib/j2sdk1.3-sun /usr/lib/j2sdk1.3-blackdown /usr/lib/j2sdk1.5-ibm /usr/lib/j2sdk1.4-ibm /usr/lib/jvm/java-gcj /usr/lib/kaffe"

# Directory for per-instance configuration files and webapps
CATALINA_BASE=/var/lib/tomcat5.5/instances/$INSTANCE_NAME

# Use the Java security manager? (yes/no)
TOMCAT5_SECURITY=no

# Timeout in seconds for the shutdown of all webapps
TOMCAT5_SHUTDOWN=30

# End of variables that can be overwritten in $DEFAULT

# overwrite settings from default file
if [ -f "$DEFAULT" ]; then
	. "$DEFAULT"
fi

test -f $DAEMON || exit 0

[ -z "$TOMCAT5_USER" ] && TOMCAT5_USER=tomcat55

# Look for the right JVM to use
for jdir in $JDK_DIRS; do
	if [ -r "$jdir/bin/java" -a -z "${JAVA_HOME}" ]; then
		JAVA_HOME_TMP="$jdir"
		# checks for a real JDK like environment, needed to check if
		# really the java-gcj-compat-dev package is installed
		if [ -r "$jdir/bin/jdb" ]; then
			JAVA_HOME="$JAVA_HOME_TMP"
		fi
	fi
done
export JAVA_HOME

# Set java.awt.headless=true if JAVA_OPTS is not set so the
# Xalan XSL transformer can work without X11 display on JDK 1.4+
# It also looks like the default heap size of 64M is not enough for most cases
# se the maximum heap size is set to 128M
if [ -z "$JAVA_OPTS" ]; then
	JAVA_OPTS="-Djava.awt.headless=true -Xmx128M"
fi

JAVA_OPTS="$JAVA_OPTS -Djava.endorsed.dirs=$CATALINA_HOME/common/endorsed -Dcatalina.base=$CATALINA_BASE -Dcatalina.home=$CATALINA_HOME -Djava.io.tmpdir=$CATALINA_BASE/temp"

# Set the JSP compiler if set in the tomcat5.5.default file
if [ -n "$JSP_COMPILER" ]; then
	JAVA_OPTS="$JAVA_OPTS -Dbuild.compiler=$JSP_COMPILER"
fi

if [ "$TOMCAT5_SECURITY" = "yes" ]; then
	JAVA_OPTS="$JAVA_OPTS -Djava.security.manager -Djava.security.policy=$CATALINA_BASE/conf/catalina.policy"
fi

# juli LogManager disabled if running under libgcj (see bug #395167)
gcj=no
"$JAVA_HOME/bin/java" -version 2>&1 | grep -q "^gij (GNU libgcj)" && gcj=yes
if [ "$gcj" != "yes" ]; then
  JAVA_OPTS="$JAVA_OPTS -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.util.logging.config.file=$CATALINA_BASE/conf/logging.properties"
fi

# Define other required variables
CATALINA_PID="/var/run/$NAME-$INSTANCE_NAME.pid"
LOGFILE="$CATALINA_BASE/logs/catalina.out"
BOOTSTRAP_CLASS=org.apache.catalina.startup.Bootstrap
JSVC_CLASSPATH="/usr/share/java/commons-daemon.jar:$CATALINA_HOME/bin/bootstrap.jar"

# Look for Java Secure Sockets Extension (JSSE) JARs
if [ -z "${JSSE_HOME}" -a -r "${JAVA_HOME}/jre/lib/jsse.jar" ]; then
    JSSE_HOME="${JAVA_HOME}/jre/"
fi
export JSSE_HOME

case "$1" in
  start)
	if [ -z "$JAVA_HOME" ]; then
		log_failure_msg "no JDK found - please set JAVA_HOME"
		exit 1
	fi

	if [ ! -d "$CATALINA_BASE/conf" ]; then
		log_failure_msg "invalid CATALINA_BASE specified"
		exit 1
	fi

	log_daemon_msg "Starting $DESC" "$NAME"
	if start-stop-daemon --test --start --pidfile "$CATALINA_PID" \
		--user $TOMCAT5_USER --startas "$JAVA_HOME/bin/java" \
		>/dev/null; then

		# Clean up and set permissions on required files
		rm -rf "$CATALINA_BASE"/temp/*
		chown --dereference "$TOMCAT5_USER" "$CATALINA_BASE/conf" \
			"$CATALINA_BASE/conf/tomcat-users.xml" \
			"$CATALINA_BASE/logs" "$CATALINA_BASE/temp" \
			"$CATALINA_BASE/webapps" "$CATALINA_BASE/work" \
			"$CATALINA_BASE/logs/catalina.out" || true

		$DAEMON -user "$TOMCAT5_USER" -cp "$JSVC_CLASSPATH" \
		    -outfile "$LOGFILE"  -errfile '&1' \
		    -pidfile "$CATALINA_PID" $JAVA_OPTS "$BOOTSTRAP_CLASS"
	else
	        log_progress_msg "(already running)"
	fi
	log_end_msg 0
	;;
  stop)
	log_daemon_msg "Stopping $DESC" "$NAME"
        if start-stop-daemon --test --start --pidfile "$CATALINA_PID" \
		--user "$TOMCAT5_USER" --startas "$JAVA_HOME/bin/java" \
		>/dev/null; then
		log_progress_msg "(not running)"
	else
		$DAEMON -cp "$JSVC_CLASSPATH" -pidfile "$CATALINA_PID" \
		     -stop "$BOOTSTRAP_CLASS"
	fi
	log_end_msg 0
	;;
   status)
        if start-stop-daemon --test --start --pidfile "$CATALINA_PID" \
		--user $TOMCAT5_USER --startas "$JAVA_HOME/bin/java" \
		>/dev/null; then

		if [ -f "$CATALINA_PID" ]; then
		    log_success_msg "$DESC is not running, but pid file exists."
		    exit 1
		else
		    log_success_msg "$DESC is not running."
		    exit 3
		fi
	else
		log_success_msg "$DESC is running with pid `cat $CATALINA_PID`"
		exit 0
	fi
        ;;
  restart|force-reload)
        if start-stop-daemon --test --stop --pidfile "$CATALINA_PID" \
		--user $TOMCAT5_USER --startas "$JAVA_HOME/bin/java" \
		>/dev/null; then
		$0 stop
		sleep 1
	fi
	$0 start
	;;
  try-restart)
        if start-stop-daemon --test --start --pidfile "$CATALINA_PID" \
		--user $TOMCAT5_USER --startas "$JAVA_HOME/bin/java" \
		>/dev/null; then
		$0 start
	fi
        ;;
  *)
	log_success_msg "Usage: $0 {start|stop|restart|try-restart|force-reload|status}"
	exit 1
	;;
esac

exit 0

My latest Ubuntu setup

I’ll keep editing as I go:

Install LaunchBox

bash$ sudo apt-get install gnome-launch-box
bash$ gconf-editor

Open apps->gnome-launch-box->bindings and set activate to

<Super>l

This allows gnome-launch-box to be activated using the Windows-L shortcut.

Next setup gnome-launch-box to start automatically. Open System->Preferences->Sessions and create a new Startup Program. I use this command line:

/usr/bin/gnome-launch-box -t -n

Install Compiz/Beryl merge latest

In progress.

Ubuntu VPN issues

I figured out how to get Ubuntu to correctly connect to a Windows style PPTP VPN. Apparently there are a number of large issues with NetworkManager that make it all but unusable for VPN. The fix is enormously simple, so I’m extremely confused as to why on earth they haven’t patched it yet. Okay, so here’s what I have done:

1. Create a shell script that you will run after you connect to the VPN via NetworkManager applet. The contents are like this:

#!/bin/bash

echo "search your-domain-name.com" > /etc/resolv.conf
echo "nameserver ip-address-of-your-companies-dns-server" >> /etc/resolv.conf
sudo route add default gw 10.10.30.1

2. Connect to the VPN using NetworkManager

3. Run this script as root:

bash$ sudo ./vpn

What this does is first it updates my resolv.conf so that I can find the DNS server that my company provides and also setups up my search domain for easy access to stuff like http://wiki. Next it adds a new route to the kernel IP routing table. The issue with NetworkManager is that it not only completely clears out resolv.conf (eeck!) but it also doesn’t always setup a route to get to the VPN network. In my script I just setup a simple default route that will direct all traffic to my VPNs gateway. This procedure assumes that NetworkManager is setting up a host route to the VPN gateway on the ppp network device. If this doesn’t happen, well your routing table could get all screwed up, but I think for now NetworkManager isn’t that stupid (however it might be).

Good luck and happy VPNing.

Dell and Ubuntu!

and

Dell is starting to ship computers pre-installed with Ubuntu. I would assume that these computers have been specifically built to ensure compatibility, which isn’t all that difficult these days, except for the video cards. I feel like this is one of the first major steps for Linux into the mainstream.

http://www.dell.com/content/topics/segtopic.aspx/linux_3x?c=us&cs=19&l=en&s=dhs

Linux looking good, behaving badly

I’ve been trying to get my Linux machine to look nice and this has been causing some amount (read LOTS) of headaches. I just wanted to start a post about all the things that Linux folks need to fix in order for Linux to look nice and work well.

Compiz
- Focusing in compiz is pretty much horrible. If you have a window that laps over onto another desktop, that window will retain focus for both desktops rather than the desktop it’s center exists on. This causes major focus headaches unless all you windows are maximized or they don’t lap over

- Compiz breaks Java. I’ve updated to the latest version of everything and even patched the rt.jar with some compiz hacks and things still don’t work correctly. This really needs to be fixed in a more general manner for Linux/Unix distributions that are going to be using compositing. Perhaps the composite manager needs to provide a hook method that allows Java native to get at the constraints and properties of the manager. This would have to be standardized across all managers on all *nix flavors, but would make life much better for VMs.

- Windows don’t draw the decorations correctly sometimes when switching desktops. This makes windows seem unfocused but they are in fact focused.

- gDesklets and other desktop daemons (these are windows that are shown on every desktop) steal focus for all desktops because they exist across all desktops, which makes it impossible to use Compiz/Beryl with anything of this nature. Major bummer because they make Linux look good and are great tools.

Dock
- I’m using avant-window-manager (AVM) and it is pretty rough thus far and looks like it might die. Gnome-dock looks dead, Cairo looks dead, and the other docks are for KDE (not a fan).

- AVM lacks support for applets/desklets which forces me to run a single gnome panel with the time. This makes Linux look ugly.

- AVM doesn’t allow for menus to be nested. This would be nice because it would remove the need to keep a panel around just so I can get to less commonly used programs. This can be fixed using a launcher, but most of those are pretty rough.

- AVM doesn’t support grouping and really lacks a lot of configuration and features.

- AVM sorta loses its mind with applications that go into the notification area like GAIM. Now I’ve seen folks fix this, so if someone knows how, let me know what you did.

Launchers
- Thus far I’ve only got gnome-launch-box to work and it is pretty rough. It doesn’t integrate well into gnome and should provide a method for auto-start and much more configuration. I think it uses beagle or something else behind it, but I couldn’t figure out how to control it or configure it. This needs a control dialog that is seamlessly integrated into the gnome configuration system.

- Deskbar is too verbose and not fast enough. Plus it doesn’t have enough options when you do find the file you are looking for. Plus, it doesn’t look nice enough to really be cool. It also jumps around the screen way too much. Not sure why it does this.

- KDE launchers just plain don’t like Gnome for some reason and I couldn’t figure out how to make them work correctly. Plus, they have that KDE feel and it clashes with the gnome stuff.

General
- I would guess that 99% of the applications that provide a transparency setting are hacking it via some background image magic. Most gnome stuff (panels, desklets, etc) don’t really work with a composite manager and need to be fixed to check for one and then fall back to their hacking if it isn’t running. If one is running though, they should use it.