| Author | 
              Topic: Integrate Apache httpd with Tomcat on AWS EC2 instance  |  
           
         | 
        
          
            
              
                
                	
                  
                    
                      Linux member offline     |  
                    
                      |   |  
                    
                      
                        
                          
                            | posts: | 
                            120 |  
                          
                            | joined: | 
                            01/24/2011 |  
                          
                            | from: | 
                            San Jose, CA |  
                         
                       |  
                    | 
                  | 
                
                  
                    
                       |  
                    
                       |  
                    
                      
                        
                          | Integrate Apache httpd with Tomcat on AWS EC2 instance |  
                        
                           Now that we have Apache httpd and Tomcat, it's time to put them work together. The whole point here is to:
  1) Apache httpd to handle multiple websites.  2) Apache httpd to handle static contents. 3) Tomcat server to handle dynamical contents.
 
 
                        -------              --------
   www.exmaple1.com ---|       |            |        |
                       |       |            |        |
                       | httpd |------------| tomcat |
   www.exmaple2.com ---|       |            |        |
                       |_______|            |________|
                           ^                    ^
                           |                    |
                           |                    |
                        static contents    dynamical contents      
 
 
  |  
                        
                           |  
                        |  
                    
                       |  
                    
                       |  
                    |  
                | 
        
          
            
              
                
                	
                  
                    
                      Linux member offline     |  
                    
                      |   |  
                    
                      
                        
                          
                            | posts: | 
                            120 |  
                          
                            | joined: | 
                            01/24/2011 |  
                          
                            | from: | 
                            San Jose, CA |  
                         
                       |  
                    | 
                  | 
                
                  
                    
                       |  
                    
                       |  
                    
                      
                        
                          | Install Apache mod_jk |  
                        
                          
                        -------              --------
   www.exmaple1.com ---|       |            |        |
                       |       |  mod_jk    |        |
                       | httpd |------------| tomcat |
   www.exmaple2.com ---|       |            |        |
                       |_______|            |________|
                           ^                    ^
                           |                    |
                           |                    |
                        static contents    dynamical contents      
  1. Install httpd-devel
 
# yum install httpd-devel
 
 
  2. Install all c/c++ complier and builder
 
# yum install gcc gcc-c++ libtool libxml2-devel
 
  3. Download, make and install mod_jk
 
$ cd /home/ec2-user
$ mkdir mod_jk
$ cd mod_jk
$ curl http://apache.osuosl.org//tomcat/tomcat-connectors/jk/
tomcat-connectors-1.2.32-src.tar.gz > tomcat-connectors-1.2.32-src.tar.gz
$ tar xzf tomcat-connectors-1.2.32-src.tar.gz
$ cd tomcat-connectors-1.2.32-src/native
$ ./configure --with-apxs=/usr/sbin/apxs
$ make
$ sudo make install
 
 
  |  
                        
                           |  
                        |  
                    
                       |  
                    
                       |  
                    |  
                | 
        
          
            
              
                
                	
                  
                    
                      Linux member offline     |  
                    
                      |   |  
                    
                      
                        
                          
                            | posts: | 
                            120 |  
                          
                            | joined: | 
                            01/24/2011 |  
                          
                            | from: | 
                            San Jose, CA |  
                         
                       |  
                    | 
                  | 
                
                  
                    
                       |  
                    
                       |  
                    
                      
                        
                          | Configure Apache mod_jk |  
                        
                          1. Configure connector
 
$ sudo vi /etc/httpd/conf/workers.properties
 # Define the list of workers that will be used
 worker.list=worker1
 # Define worker1
 worker.worker1.port=8009
 worker.worker1.host=localhost
 worker.worker1.type=ajp13	
  Save and close the file.
  2. Configure httpd 
 
# Load mod_jk module
LoadModule    jk_module  /usr/lib/httpd/modules/mod_jk.so
# Where to find workers.properties
JkWorkersFile /etc/httpd/conf/workers.properties
# Where to put jk shared memory
JkShmFile     /var/log/httpd/mod_jk.shm
# Where to put jk logs
JkLogFile     /var/log/httpd/mod_jk.log
# Set the jk log level [debug/error/info]
JkLogLevel    info
# Select the timestamp log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
# Send servlet for context /examples to worker named worker1
JkMount  /examples/servlets/* worker1
# Exclude the static files                      
JkUnMount /examples/servlets/*.jpg  worker1
JkUnMount /examples/servlets/*.gif  worker1
# Send JSPs  for context /examples to worker named worker1
JkMount  /examples/*.jsp worker1
 
  3. Configure httpd -- VirtualHost 
 
<VirtualHost *:80>
  ServerName example2.com
  ServerAlias www.example2.com
  DocumentRoot /home/ec2-user/example2.com/html
  SuexecUserGroup ec2-user ec2-user
  <Directory /home/ec2-user/example2.com/html>
    Options Indexes Includes FollowSymLinks
    AllowOverride All
  </Directory>
# Send servlet for context /examples to worker named worker1
JkMount  /examples/servlets/* worker1
# Send JSPs  for context /examples to worker named worker1
JkMount  /examples/*.jsp worker1
</VirtualHost>
  |  
                        
                           |  
                        |  
                    
                       |  
                    
                       |  
                    |  
                | 
        
          
            
              
                
                	
                  
                    
                      Linux member offline     |  
                    
                      |   |  
                    
                      
                        
                          
                            | posts: | 
                            120 |  
                          
                            | joined: | 
                            01/24/2011 |  
                          
                            | from: | 
                            San Jose, CA |  
                         
                       |  
                    | 
                  | 
                
                  
                    
                       |  
                    
                       |  
                    
                      
                        
                          | Who should go online first, Apache httpd or Tomcat? |  
                        
                          | Always start or restart Tomcat first, then Apache httpd.  |  
                        
                           |  
                        |  
                    
                       |  
                    
                       |  
                    |  
                | 
        
          
            
              
                
                	
                  
                    
                      Linux member offline     |  
                    
                      |   |  
                    
                      
                        
                          
                            | posts: | 
                            120 |  
                          
                            | joined: | 
                            01/24/2011 |  
                          
                            | from: | 
                            San Jose, CA |  
                         
                       |  
                    | 
                  | 
                
                  
                    
                       |  
                    
                       |  
                    
                      
                        
                          | Integrate Apache httpd with Tomcat plus failover |  
                        
                          
 
 
                        -------              ----------
   www.exmaple1.com ---|       |            |          |
                       |       |  mod_jk    |  tomcat  |
                       | httpd |------------|(primary) |
   www.exmaple2.com ---|       |         |  |          |
                       |_______|         |  |__________|
                                         |
                                         |   ----------
                                         |  |          | 
                                         |  |  tomcat  |
                                         |--|(failover)|
                                            |          |
                                            |__________|
  A load balancer is a worker that does not directly communicate with Tomcat. Instead it is responsible for the management of several "real" workers, called members or sub workers of the load balancer. 
  A practical example given here is to use loadbalancer to provide high-availability service with a failover tomcat server.
 
 
  # The dvanced router LB worker
  worker.list=router
  worker.router.type=lb
  worker.router.balance_workers=worker1,worker2
  # Define the first member worker
  worker.worker1.type=ajp13
  worker.worker1.host=myhost1
  worker.worker1.port=8009
  # Define preferred failover node for worker1
  worker.worker1.redirect=worker2
  # Define the second member worker
  worker.worker2.type=ajp13
  worker.worker2.host=myhost2
  worker.worker2.port=8009
  # Disable worker2 for all requests except failover
  worker.worker2.activation=disabled
 
  The redirect flag on worker1 tells the load balancer to redirect the requests to worker2 in case that worker1 has a problem. In all other cases worker2 will not receive any requests, thus acting like a hot standby.
 
  |  
                        
                           |  
                        |  
                    
                       |  
                    
                       |  
                    |  
                | 
        
          
            
              
                
                	
                  
                    
                      AwsEC2 member offline     |  
                    
                      |   |  
                    
                      
                        
                          
                            | posts: | 
                            39 |  
                          
                            | joined: | 
                            08/28/2012 |  
                          
                            | from: | 
                            CA |  
                         
                       |  
                    | 
                  | 
                
                  
                    
                       |  
                    
                       |  
                    
                      
                        
                          | Side notes for mod_jk installation 64-bit |  
                        
                          1) Always get the latest version
 
$ cd /home/ec2-user
$ mkdir mod_jk
$ cd mod_jk
$ curl http://apache.osuosl.org/tomcat/tomcat-connectors/jk/
tomcat-connectors-1.2.37-src.tar.gz > tomcat-connectors-1.2.37-src.tar.gz
$ tar xzf tomcat-connectors-1.2.37-src.tar.gz
$ cd tomcat-connectors-1.2.37-src/native
$ ./configure --with-apxs=/usr/sbin/apxs
$ make
$ sudo make install
 
 
  2) $ make may return 'make not found' 
  You may have to install first by
 
# yum install make
 
 
  3) The install may change the location depending on your OS 
  For 32-bit:  /usr/lib/httpd/modules/mod_jk.so For 64-bit:  /usr/lib64/httpd/modules/mod_jk.so
 
 
  4) Accordingly, a working httpd.conf 
 
# vi /etc/httpd/conf/httpd.conf
 
 
# Load mod_jk module
LoadModule    jk_module  /usr/lib64/httpd/modules/mod_jk.so
# Where to find workers.properties
JkWorkersFile /etc/httpd/conf/workers.properties
# Where to put jk shared memory
JkShmFile     /var/log/httpd/mod_jk.shm
# Where to put jk logs
JkLogFile     /var/log/httpd/mod_jk.log
# Set the jk log level [debug/error/info]
JkLogLevel    info
# Select the timestamp log format
JkLogStampFormat "[%a %b %d %H:%M:%S %Y] "
<VirtualHost *:80>
  ServerName example1.com
  ServerAlias www.example1.com
  DocumentRoot /home/ec2-user/example1.com/html
  SuexecUserGroup ec2-user ec2-user
  <Directory /home/ec2-user/example1.com/html>
    Options Indexes Includes FollowSymLinks
    AllowOverride All
  </Directory>
# Send servlet for context /examples to worker named worker1
  JkMount  /examples/servlets/* worker1
# Send JSPs  for context /examples to worker named worker1
  JkMount  /examples/*.jsp worker1
</VirtualHost>
 
 
# /etc/init.d/httpd start
Starting httpd:                                            [  OK  ]
 
  |  
                        
                           |  
                        |  
                    
                       |  
                    
                       |  
                    |  
                | 
        
          
            
              
                
                	
                  
                    
                      AwsEC2 member offline     |  
                    
                      |   |  
                    
                      
                        
                          
                            | posts: | 
                            39 |  
                          
                            | joined: | 
                            08/28/2012 |  
                          
                            | from: | 
                            CA |  
                         
                       |  
                    | 
                  | 
                
                  
                    
                       |  
                    
                       |  
                    
                      
                        
                          | Sundomain mapping with Apache httpd and Tomcat |  
                        
                          Here comes the requirement
  Two or more subdomains:
      
                        -------              ----------
   news.exmaple.com ---|       |            |          |
                       |       |  mod_jk    |  tomcat  |
    www.exmaple.com ---| httpd |------------|          |
                       |       |            |          |
   blog.exmaple.com ---|_______|            |----------|
                                                 |
                                                 |           
                                                 |-[+] webapps                             
                                                 |---[+] news
                                                 |---[+] blog
                                                 |---[+] main
  Desired Mapping:
 
main domain:
   www.exmaple.com/main -->  /webapps/main
   www.exmaple.com/blog -->  /webapps/blog
   www.exmaple.com/news -->  /webapps/news
subdomain news:
   news.exmaple.com     -->  /webapps/news
subdomain blog:
   blog.exmaple.com     -->  /webapps/blog
 
  Here comes the solution
  httpd.conf
 
<VirtualHost *:80>
  ServerName exmaple.com
  ServerAlias www.exmaple.com
  <Directory /home/ec2-user/exmaple.com/html>
    Options Indexes Includes FollowSymLinks
    AllowOverride All
  </Directory>
  JkMount  /main worker1
  JkMount  /news worker1
  JkMount  /blog worker1
  JkUnMount  /*.html worker1
</VirtualHost>
<VirtualHost *:80>
  ServerName news.example.com
  <Directory /home/ec2-user/news.exmaple.com/html>
    Options Indexes Includes FollowSymLinks
    AllowOverride All
  </Directory>
  JkMount  / worker1
  JkMount  /* worker1
  JkUnMount  /*.html worker1
</VirtualHost>
<VirtualHost *:80>
  ServerName blog.example.com
  <Directory /home/ec2-user/blog.exmaple.com/html>
    Options Indexes Includes FollowSymLinks
    AllowOverride All
  </Directory>
  JkMount  / worker1
  JkMount  /* worker1
  JkUnMount  /*.html worker1
</VirtualHost>
  Tomcat server.xml
 
 <Host name="news.example.com"  appBase="webapps"
        unpackWARs="true" autoDeploy="true">
   <Context docBase="/env/.../webapps/news" path="/" reloadable="true"/>
 </Host>
 <Host name="blog.example.com"  appBase="webapps"
        unpackWARs="true" autoDeploy="true">
   <Context docBase="/env/.../webapps/blog" path="/" reloadable="true"/>
 </Host>
  |  
                        
                           |  
                        |  
                    
                       |  
                    
                       |  
                    |  
                |