twitter
    Find out what I'm doing, Follow Me :)

Connect Tomcat 7 with Apache2 (mod_jk) and install Virtual Host

Virtual hosting is a method for hosting multiple domain names on a computer using a single IP address. This allows one machine to share its resources, such as memory and processor cycles, to use its resources more efficiently.

One widely used application is shared web hosting. Shared web hosting prices are lower than a dedicated web server because many customers can be hosted on a single server.

Installa Apache Tomcat 7:
http://diegobenna.blogspot.com/2011/01/install-tomcat-7-in-ubuntu-1010.html

and install Apache2:
http://diegobenna.blogspot.com/2011/01/install-apache2-with-php5-and-mysql.html

Install the connector:
sudo apt-get install libapache2-mod-jk

Now we move on to configure it to communicate with Apache Tomcat.
The first step is to create a file folder worker.properties apache like this:
sudo gedit /etc/apache2/workers.properties

End insert this text:
# Define 1 real worker using ajp13
worker.list=worker
# Set properties for worker (ajp13)
worker.worker.type=ajp13
worker.worker.host=localhost
worker.worker.port=8009

If you want add a new application in Virtual Web Host is not necessary to add a new workerX

Edit this file for connect tomcat's application with apache2:
sudo gedit /etc/apache2/httpd.conf

Past and copy this text:

ServerAdmin webmaster@localhost

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

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
</Directory>

ErrorLog /var/log/apache2/error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog /var/log/apache2/access.log combined

Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>

Now we just have to disable the default configuration file for apache with:
sudo a2dissite default

We must also remove the line NameVirtualHost *: 80 as follows:
sudo gedit /etc/apache2/ports.conf

Reload e restart apache2
sudo /etc/init.d/apache2 reload
sudo /etc/init.d/apache2 restart

Edit file jk.load for load tomcat webapps in apache2 avaible web site

cd /etc/apache2/mods-available
sudo gedit jk.load

Past and copy this text:


LoadModule jk_module /usr/lib/apache2/modules/mod_jk.so
# Where to find workers.properties
# Update this path to match your conf directory location (put workers.properties next to httpd.conf)
JkWorkersFile /etc/apache2/workers.properties
# Where to put jk shared memory
# Update this path to match your local state directory or logs directory
JkShmFile     /var/log/apache2/mod_jk.shm
# Where to put jk logs
# Update this path to match your logs directory location (put mod_jk.log next to access_log)
JkLogFile     /var/log/apache2/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] "

And now edit proxy.conf


sudo gedit proxy.conf

This is new text:

<IfModule mod_proxy.c>
#turning ProxyRequests on and allowing proxying from all may allow
#spammers to use your proxy to send email.

ProxyRequests Off

<Proxy *>
AddDefaultCharset off
Order deny,allow
Allow from all
#Deny from all
#Allow from .example.com
</Proxy>

# Enable/disable the handling of HTTP/1.1 "Via:" headers.
# ("Full" adds the server version; "Block" removes all outgoing Via: headers)
# Set to one of: Off | On | Full | Block

ProxyVia On
</IfModule>

Create the symbolik link in apache web site avaible:
cd /etc/apache2/mods-enabled 
sudo ln -s ../mods-available/jk.load ./jk.lo

We add in mods-enabled also links to the following modules
rewrite.load
proxy.load
proxy.conf
proxy_ajp.load


sudo ln -s ../mods-available/rewrite.load ./rewrite.load
sudo ln -s ../mods-available/proxy.load ./proxy.load
sudo ln -s ../mods-available/proxy.conf ./proxy.conf
sudo ln -s ../mods-available/proxy_ajp.load ./proxy_ajp.load


Create and edit the application's file (genetic is my web application name):
sudo gedit /etc/apache2/sites-available/genetic

Copy and past this text:

NameVirtualHost www.genetic.it:80
<VirtualHost www.genetic.it:80>
ServerName www.genetic.it
JKMount /genetic/* worker
JKMount /genetic worker

  RewriteEngine on
  RewriteRule ^/$ /genetic/ [R=permanent]
  RewriteRule ^/genetic/(.*)$ ajp://localhost:8009/genetic/$1 [P]
</VirtualHost>

Create a symbolic link in enabled directory:

cd /etc/apache2/sites-enabled
sudo ln -s ../sites-available/genetic ./001-genetic

Let's create an application to be addressed
sudo gedit /usr/share/tomcat7/conf/server.xml

Add this text to hosts:

...
<Host name="www.genetic.it" appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="true" xmlNamespaceAware="false">
<Context path="" docBase="genetic/" reloadable="true" privileged="true" antiResourceLocking="false" anitJARLocking="false">
</Context>
</Host>
...

I create the folder for the application if the application does not exist

sudo mkdir /usr/share/tomcat7/webapps/genetic
cd /usr/share/tomcat7/webapps/genetic

Install application
sudo jar xvf genetic.war

Now your virtualhost in internet is actived. If you want virtualhost in localhost, edit hosts file:

sudo gedit /etc/hosts

Add in the line starting with 127.0.0.1

www.genetic.it

Install module rewrite
sudo a2enmod rewrite

Restart tomcat and reload Apache2

sudo /etc/init.d/apache2 reload
sudo /etc/init.d/apache2 restart
sudo /etc/init.d/tomcat7 restart

Write in your browser:
www.genetic.it
and see your tomcat webapp in apache

0 comments:

Post a Comment