Linux: Release port 8080 after stopping Tomcat – Port 8080 in use by Java

It’s past 3 AM and I finally found out why after stopping my Tomcat server I’m unable to successfully start it again. It seem like Java process won’t go away. To get around that I added the following line to my Tomcat shutdown.sh script:
killall -9 java
and all is well now. Still unorthodox, but works for me. I will write a script to find the process and kill it properly later on.

Tomcat: Redirect default home page to another URL

To create a redirect from default page in Tomcat edit CATALINA_HOME/webapps/ROOT/index.html:
nano /CATALINA_HOME/webapps/ROOT/index.html

Where CATALINA_HOME is your tomcat directory. Delete the content and replace with the following:
<html>

<head>
<meta http-equiv="refresh" content="0;URL=<url>">
</head>

<body>
</body>

</html>
and replace <url> with new destination.

Linux: Run tomcat with apache – mod_jk configuration

Disclaimer: I found an old note with these instructions on it. I’m sure that I’ve taken these from other blog(s) but unfortunately I cannot find the source to give complete credit to. Since this is a very useful how-to I decided to clean it up and publish it for everyone to read, but if author(s) stumble upon this page and provide a link to original document(s) I will be more than happy to give them full credit.

There are a few reasons why we’d want to configure Tomcat to run with Apache. For me the most important is security and the fact that I’d like to run my web servers on port 80. In order to run Tomcat on port 80 it has to run as root which is not safe and absolutely not recommended. I am going to show you how to configure your Apache to run and pass connections to Tomcat.

What I normally like to do in such situation is to compile Apache instead of installing from repositories, install and configure Java and Tomcat, and then complile mod_jk to provide Tomcat/Apache connector. Basically all steps will be manual. Perhaps all this can be done using repositories too, but this way I can keep things updated as they updates come out by the developers, not when they become available in repositories.

To simplify the solution and make it a universal tutorial, I am going to refer to each product by its name. Therefore we will be renaming all extracted folders to their generic names. For instance, latest versions of Apache, Tomcat, Java and mod_jk are 2.2.11, 6.0.18, 6u11 and 1.2.27 respectively (Jan 09), but we are going to refer to them as just Apache, Tomcat, Java and mod_jk. I have actually renamed them right after they were extracted before I placed them onto their permanent directories. I am going to use /usr/local as my installation directory but you can place them in /opt or any other directory you wish.

Most steps are performed under a non-root account. I will let you know when to use the root account.

Installing Apache:

Log on with your non-root account.
Download Apache tar package from http://www.apache.org and extract. This directory will be called “apache” from this point forward.
Change directory to apache ‘cd apache’.

Prepare the product to compile:

In apache directory:
./configure --prefix=/usr/local/apache
make
Change to root user: su
make install

Once it’s complete run apache:
/usr/local/apache/bin/apachectl start
test installation by browsing the target address. If it’s running then stop the process:
/usr/local/apache/bin/apachectl stop

Install Java (I’m using jdk):

Download Java self-extracting package from http://www.java.com. Whatever this file is, we call it java.bin.
Make it executable:
chmod +x java.bin
Execute:
./java.bin
Once you’re done with the installation you will have a directory jre1xxxxx. We will refer to this directory as “java”.
Become root: su (to move directory to /usr/local)
Move the directory to its final resting place. Mine sits in /usr/local:
mv java /usr/local

Install Tomcat:

Download Tomcat from http://tomcat.apache.org and extract the tar ball. We will call this directory “tomcat” from now on.
Become root: su (to move directory to /usr/local)

Move this directory to /usr/local:
mv tomcat /usr/local

Now it’s time to configure our paths. Edit your .bash_profile:

STOP: This is local to the user running Tomcat. Each user who’ll be running Tomcat should have a bash_profile in their own home directory.
nano ~/.bash_profile

and add the following lines:
export JRE_HOME=/usr/local/java
export JAVA_HOME=/usr/local/java
export CATALINA_HOME=/usr/local/tomcat

Save and exit, then execute the following command:
source ~/.bash_profile

Now test your Tomcat: as non-root user:
/usr/local/tomcat/bin/startup.sh

browse to http://localhost:8080. If it’s working then shut it down:
/usr/local/tomcat/bin/shutdown.sh

Installing connector (mod_jk):

Download mod_jk connector from http://tomcat.apache.org/download-connectors.cgi and extract. We will call this new directory “connector”.
Go to connector/native.
To prepare:
./configure --with-apxs=/usr/local/apache/bin/apxs
make
Become root and:
make install
Check to see if mod_jk.so is in /usr/local/apache/modules. If it’s there then you’ve so far been successful.

Create the connector:

Create a file called “connector.conf” in apache/conf directory:
nano /usr/local/apache/conf/connector.conf

and copy/paste the following lines in that file, save and exit:
workers.tomcat_home=/usr/local/tomcat
workers.java_home=/usr/local/java
ps=/
worker.list=myworker
worker.myworker.port=8009
worker.myworker.host=localhost
worker.myworker.type=ajp13
worker.myworker.lbfactor=1

Add the following lines to httpd.conf:
LoadModule jk_module modules/mod_jk.so
JkWorkersFile conf/connector.conf
JkLogFile logs/mod_jk.log
JkLogLevel error
JkLogStampFormat "[%a %b %d %H:%M:%S %Y]"
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
JkRequestLogFormat "%w %V %T"
JkMount /*.jsp myworker
JkMount /* myworker

Now, start both Apache and Tomcat. You should be able to view the Tomcat default page in http://localhost AND http://localhost:8080. This means that the connector is working. To increase security disable http access to Tomcat through port 8080.

Note: Apache will automatically start after reboot, but you will have to add Tomcat to your startup script manually. Here is a startup script you can copy to your /etc/init.d and make it executable to start/stop your server. I don’t remember where I got this script from, but just so you know, it’s not mine. Good thing about this script is that even if you run this as root, it will run your Tomcat server as user specified in line 6.

Note: become root to run Apache. If you use the following script for your Tomcat, you may also run it as root. It will switch to specified user once it’s executed. I don’t use the script, however, what I do is to add it to my root crontab and have run it as non-root user. This is the command I use:
su - <username> -c /usr/local/tomcat/bin/startup.sh

Here is the script to run Tomcat (not mine, I found it in a mailing list):
#!/bin/sh
# Tomcat Startup Script

TOMCAT_OWNER=<user who will run tomcat>; export TOMCAT_OWNER

start() {
echo -n "Starting Tomcat: "
su $TOMCAT_OWNER -c $CATALINA_HOME/bin/startup.sh
sleep 2
}
stop() {
echo -n "Stopping Tomcat: "
su $TOMCAT_OWNER -c $CATALINA_HOME/bin/shutdown.sh
}

# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo $"Usage: tomcat {start|stop|restart}"
exit
esac

Linux: Apache Tomcat tips and tricks

JAVA_HOME or JRE_HOME variables are not defined:

Edit bash_profile for the user running Tomcat:
nano ~/.bash_profile

Add the following line and edit to point to correct directory:
export JRE_HOME=/usr/local/jre

Save and exit, then execute the following command:
source ~/.bash_profile

Running Apache Tomcat on privileged ports (lower then 1024) as non-root user:

The best way is to set an iptables rule to forwards incoming requests from port 80 to 8080:
iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8080
or setup apache with mod_jk connector to pass http requests to tomcat