In this post you will learn how to configure access to Tomcat trough an Apache virtual host on port 80 with mod_proxy_ajp.
The Tomcat webapp will be available on http://www.mybestapp.com/
The Apache static content (css, javascript, etc.) will be available on http://static.mybestapp.com/

First install Java JRE 7. You can download it on http://www.oracle.com/technetwork/java/javase/downloads/index.html

rpm -ivh jre-7u10-linux-x64.rpm
java -version

The following errors are « normal » and have no consequences:

Error: Could not open input file: /usr/java/jre1.7.0_10/lib/rt.pack
Error: Could not open input file: /usr/java/jre1.7.0_10/lib/jsse.pack
Error: Could not open input file: /usr/java/jre1.7.0_10/lib/charsets.pack
Error: Could not open input file: /usr/java/jre1.7.0_10/lib/ext/localedata.pack

 

Install Tomcat 7 (You can download it on http://tomcat.apache.org/download-70.cgi

cd /opt
wget http://mir2.ovh.net/ftp.apache.org/dist/tomcat/tomcat-7/v7.0.34/bin/apache-tomcat-7.0.34.zip
unzip apache-tomcat-7.0.34.zip
rm apache-tomcat-7.0.34.zip
chmod -R +x apache-tomcat-7.0.34/bin/

 

Configure Tomcat 7 as a service

cd /etc/init.d
vi tomcat

Put the following content in the tomcat init file. Change JAVA_HOME and CATALINA_HOME with your version number:

#!/bin/bash
# description: Tomcat Start Stop Restart
# processname: tomcat
# chkconfig: 234 20 80
JAVA_HOME=/usr/java/jre1.7.0_10
export JAVA_HOME
PATH=$JAVA_HOME/bin:$PATH
export PATH
CATALINA_HOME=/opt/apache-tomcat-7.0.34

case $1 in
start)
sh $CATALINA_HOME/bin/startup.sh
;;

stop)
sh $CATALINA_HOME/bin/shutdown.sh
;;

restart)
sh $CATALINA_HOME/bin/shutdown.sh
sh $CATALINA_HOME/bin/startup.sh
;;
esac

exit 0

Add the init script and activate it on startup for runlevel 2, 3 and 4:

chmod 755 tomcat
chkconfig --add tomcat
chkconfig --level 234 tomcat on

 

Configure Tomcat access manager (if needed)

vi /opt/apache-tomcat-7.0.34/conf/tomcat-users.xml
<tomcat-users>
<role rolename="manager-script"/>
<role rolename="manager-gui"/>
<role rolename="admin-gui"/>
<user username="admin" password="mybestpassword" roles="manager-gui,admin-gui,manager-script"/>
</tomcat-users>

 

Start Tomcat

service tomcat start

Open port 8080 in iptables:

vi /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT

Now try http://www.mybestapp.com:8080/ in your browser : Tomcat is ready to work on port 8080.

 

Install and configure Apache

yum install httpd
chkconfig httpd on
service httpd start

Activate name-based virtual hosting:

vi /etc/httpd/conf/httpd.conf
#
# Use name-based virtual hosting.
#
NameVirtualHost *:80
#
# NOTE: NameVirtualHost cannot be used without a port specifier
# (e.g. :80) if mod_ssl is being used, due to the nature of the
# SSL protocol.
#

 

Configure mod_proxy_ajp (Should already be enabled by default)

vi /etc/httpd/conf/httpd.conf
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so

 

Create a virtual host for tomcat throught mod_proxy_ajp

cd /etc/httpd/conf.d/
rm welcome.conf
vi tomcat.conf
<VirtualHost *:80>
       ServerName mybestappp.com
       ServerAlias www.mybestapp.com

       ProxyRequests Off
       ProxyPreserveHost On

       ErrorLog /var/log/httpd/tomcat.error.log
       CustomLog /var/log/httpd/tomcat.log combined

       <Proxy *>
               Order deny,allow
               Allow from all
       </Proxy>

       ProxyPass / ajp://localhost:8009/
       ProxyPassReverse / ajp://localhost:8009/
</VirtualHost>

Restart Apache:

service httpd restart

Open port 80 in iptables:

vi /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT

Try http://www.mybestapp.com/ in your browser : You can access tomcat through Apache on port 80.

tomcat-manager

 

Create a virtual host for static content in Apache

cd /etc/httpd/conf.d
vi static.conf
<VirtualHost *:80>
    ServerName static.mybestapp.com
    DocumentRoot /var/www/html/static

       <Directory />
               Options FollowSymLinks
               AllowOverride None
               deny from all
       </Directory>

       <Directory /var/www/html/static>
               Options Indexes FollowSymLinks MultiViews
               AllowOverride None
               Order allow,deny
               allow from all
       </Directory>
</VirtualHost>

Now create a directory with your static content:

mkdir /var/www/html/static

And put your static content inside, like style.css:

vi style.css

Restart Apache:

service httpd restart

Try http://static.mybestapp.com/style.css in your browser : That’s a good way to access your static content like pictures, css, javascript, etc. through an Apache virtual host.

You can disable TCP 8080 in your iptables firewall.

That’s all folk

Source 1 (fr) : http://haveacafe.wordpress.com/2008/09/21/how-to-serveur-tomcat-sur-debian-apache-en-proxy-avec-ajp/
Source 2 (en) : http://ryanhnelson.com/mod_proxy_ajp
Source 3 (en) : http://httpd.apache.org/docs/2.2/mod/mod_proxy_ajp.html
Source 4 (en) : http://www.vodori.com/blog/multiple-sites-with-apache-vhosts-and-ajp.html