How To Install Tomcat with Apache Web Server proxy on CentOS 6.5

Introduction


This guide introduce how to install Tomcat container with apache web server.

We will install following packages for this tutorial.
  1. Java Runtime Environment (CentOS build in repository - OpenJDK)
  2. Apache Web Server (CentOS build in repository)
  3. Compile and load mod_jk (http://www.apache.org/dist/tomcat/tomcat-connectors/jk/tomcat-connectors-1.2.37-src.tar.gz)
  4. Tomcat (http://tomcat.apache.org/index.html)

Install Java

Minimal CentOS installation will not install openjdk package.
Verify the package installation
rpm -qa |grep java
Install Java
yum install java-1.7.0-openjdk.x86_64
Verify with java command
[root@demo-01 ~]# java -version
java version "1.7.0_51"
OpenJDK Runtime Environment (rhel-2.4.4.1.el6_5-x86_64 u51-b02)
OpenJDK 64-Bit Server VM (build 24.45-b08, mixed mode)

Install Apache Web Server

We will install Apache web server as frontend and will proxy to tomcat container.
Install Apache web server
yum install httpd
Make sure bring up upon reboot
chkconfig httpd on

Compile and load mod_jk module

Compilation steps must be on development environment. You just need mod_jk.so module at production server.
Install necessary packages.
yum install httpd-devel

yum install gcc gcc-c++
Download connector source package
yum install wget

wget http://www.apache.org/dist/tomcat/tomcat-connectors/jk/tomcat-connectors-1.2.37-src.tar.gz
Extract tarball and configure
tar -zxvf tomcat-connectors-1.2.37-src.tar.gz

cd tomcat-connectors-1.2.37-src/native

./configure --with-apxs=/usr/sbin/apxs --enable-api-compatibility

make

make install
Copy /etc/httpd/modules/mod_jk.so to production server.

Install Tomcat

This step will install Tomcat container. Create service account
useradd -c "Tomcat Service" tomcat

passwd tomcat
*Make sure you disable SSH access for tomcat service account
Download package
wget http://www.us.apache.org/dist/tomcat/tomcat-8/v8.0.1/bin/apache-tomcat-8.0.1.tar.gz
Check checksum
md5sum apache-tomcat-8.0.1.tar.gz
Extract tarball and copy to /opt/tomcat
tar -zxvf apache-tomcat-8.0.1.tar.gz

mv apache-tomcat-8.0.1 /opt/
Prepare software directory
cd /opt

chown -R tomcat:tomcat apache-tomcat-8.0.1  

ln -s apache-tomcat-8.0.1 tomcat
Configure environment variable
CATALINA_HOME=/opt/tomcat

export CATALINA_HOME
Verify
echo $CATALINA_HOME
Make it permanent
cat >> /etc/profile <<EOF

# Variable for Tomcat container
CATALINA_HOME=/opt/tomcat
export CATALINA_HOME
EOF
*Make sure you copy from code block. Do not type to avoid typo error
Ready to start service
su - tomcat -c /opt/tomcat/bin/startup.sh
Try to browse the service
http://<Server IP Address>:8080
*In order to start service upon reboot, need to write init script

Final configuration

We will finalize configuration at Apache web server.
Create mod_jk configuration at Apache web server
vi /etc/httpd/conf.d/mod_jk.conf

# Load module
LoadModule jk_module modules/mod_jk.so
# Worker file
JkWorkersFile conf.d/workers.properties
# Log file
JkLogFile logs/mod_jk.log
# Log level debug/error/info
JkLogLevel info

JkMount /* worker01
Create worker.properties file
vi /etc/httpd/conf.d/workers.properties

worker.list=worker01
worker.worker1.type=ajp13
worker.worker1.port=8009
worker.worker1.host=localhost
worker.worker1.lbfactor=1
Restart httpd
service httpd restart
Test by browsing port 80. It should proxy to 8080. You should see Tomcat page.
http://<Server IP Address>

Comments