This one is going to be a long post so I will break it up in two parts. My notes on this are brief but I will be more descriptive of this setup for the newbie benefit. Many people in the web host business have asked me how to setup tomcat or multiple instances of tomcat. I’ve given individuals an insight of what I learned years ago, but this is the first time I’ve posted for all to see. It’s actually very simple.
Steps for part 1
- Install Tomcat Connector
- worker.properties
- httpd_jk.conf
- Install Tomcat
- Configure server.xml
- Set JAVA_HOME
- start tomcat
Steps for part 2
- Configure domain’s httpd.conf file
- Add init script and symlink in rc.d
- restart httpd
You can start these steps in any order, but as I’ve always said, “Theres more than one way to skin a tomcat”.
Install Apache Tomcat Connector
Get latest release here: http://tomcat.apache.org/download-connectors.cgi The current version of this writing is tomcat-connectors-1.2.30-src.tar.gz and upload to your /root dir and untar.
tar -xvzf tomcat-connectors-1.2.30-src.tar.gz
cd into the tomcat-connectors-1.2.30-src/native and compile:
./configure --with-apxs=/usr/sbin/apxs (or wherever your apxs is)
make
su -c 'make install';
You will then find mod_jk.so module in /usr/lib64/httpd/modules/
However, we’re not done with this yet. We still need to place a copy of workers.properties in /etc/httpd/conf/ and add mod_jk.so to apache by adding a file called httpd_jk.conf in /etc/httpd/conf.d where apache httpd.conf inclusion.
In the folder tomcat-connectors-1.2.30-src/conf/ you can find a copy of httpd_jk.conf and workers.properties.
workers.properties
cp workers.properties /etc/httpd/conf/
Vi into workers.properties and update the following:
Add a new worker after the manager worker entry as shown below for worker “tomcat1”:
worker.list=tomcat1 # (1)
worker.tomcat1.port=8007 # (2)
worker.tomcat1.host=localhost # (3)
worker.tomcat1.type=ajp13 # (4)
1. You specify the name of the new worker which in this case is tomcat1. This name can be any name, but must be unique among existing workers and with no spaces or special characters.
2. You specify the AJP port for the connector. 8007 is default. You can change this port here and in tomcat server.xml to utilize multiple instances of tomcat.
3. localhost since apache and tomcat are on the same server. Use IP or Hostname if apache and tomcat are on different servers which is common with clustering/load balancing.
4. ajp1.3 is currently the default worker type. ajp12 is deprecated and as of this writing ajp1.4 hasn’t been release.
Save the file.
httpd_jk.conf File
Now we need to copy the httpd_jk.conf to /etc/httpd/conf.d to enable mod_jk module in apache.
vi /etc/httpd/conf.d/httpd_jk.conf and validate the paths. Modify the path to workers with root path: /etc/httpd/conf/workers.properties and change logs/ to /var/log/httpd/. You may change them to whatever suits you best, but the above is my habit.
Now restart httpd and fix any errors that show up. This is end of installation of the apache connector.
Next step is to install Apache Tomcat Container
Download apache tomcat 6 from here: http://tomcat.apache.org/download-60.cgi
Untar it in the directory you wish to host it from. Should not be in /root or owned by root. I usually install it in /home/username/ and have it owned by that user.
tar -xvzf apache-tomcat-6.0.29.tar.gz
After you extract, create a symbolic link ‘tomcat’ to make things easier. Saves you from having to update other config if you ever upgrade tomcat.
ln -s apache-tomcat-6.0.29 tomcat
Configure server.xml
Update HTTP connector port if different from 8080. I usually defer to something else for security reasons.
Update the AJP 1.3 connector port if different from 8009 in workers.properties worker entry. This is where we match our entries in workers.properties so apache and tomcat know which worker to deal with when rendering pages.
Update defaultHost= from localhost to your domain (without www)
Update Host name to your domain name (without www).
Add alias to include www.domain.com
<Alias>www.domainX.com</Alias>
Add context path before closing host tag if you plan to use webapp other than ROOT:
<Context path=""
docBase="/home/username/tomcat/webapps/webappName" crossContext="false" reloadable="true">
</Context>
Save file. Its good practice to make a backup of server.xml when you know this one is working.
Set JAVA_HOME
Did you install java? no? Best we do this now. Download the latest jdk 1.6 bin file wherever. I prefer the bin over the rpm installation because it allows me to setup multiple JDK versions. You can download and setup as user root in opt. Lets create folder in /opt/ called java and extract the file there.
First make jdk executable
chmod +x jdk-6u21-linux-i586.bin
Then expand. It will prompt you to agree to terms for it completes.
./jdk-6u21-linux-i586.bin
You will end up with a folder jdk-1.6_21
Now we need to set JAVA_HOME for your tomcat installation. There are 2 ways to do this, one is by setting export JAVA_HOME in your .bash_profile or you can set it up in catalina.sh file along with JAVA_OPTS. I prefer to use catalina.sh as sometimes I will have different jdk requirements for different containers on the same user. I find the latter method provides for better flexibility. Newer tomcat installations now have setclasspath.sh where JAVA_HOME can be defined. Also if you’re using the java service wrapper, then you will want to define JAVA_HOME in the wrapper.conf file in the conf directory of tomcat.
Add following after the comments in catalina.sh in the bin/ directory:
JAVA_HOME=/opt/java/jdk-1.6_21
export JAVA_HOME
At this point, you should be able to start your container.
cd tomcat/bin/
./catalina.sh run (if you want to watch the output - same as catalina.out log)
./startup.sh (or start it up and forget about it)
View tomcat/logs/catalina.out for any errors.
Now you can access your tomcat webpage or webapp using the HTTP port 8080 if its open.
Next post I will finish the rest of the httpd configuration so you can run your application on port 80.