mercoledì 12 gennaio 2011

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:

and install Apache2:

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 apache like this:
sudo gedit /etc/apache2/

End insert this text:
# Define 1 real worker using ajp13
# Set properties for worker (ajp13)

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 /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all

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

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 ::1/128

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/
# Where to find
# Update this path to match your conf directory location (put next to httpd.conf)
JkWorkersFile /etc/apache2/
# 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

# 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

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

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:

JKMount /genetic/* worker
JKMount /genetic worker

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

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="" appBase="webapps"
unpackWARs="true" autoDeploy="true"
xmlValidation="true" xmlNamespaceAware="false">
<Context path="" docBase="genetic/" reloadable="true" privileged="true" antiResourceLocking="false" anitJARLocking="false">

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

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:
and see your tomcat webapp in apache

31 commenti:

  1. thanks! nice write-up!

  2. When I write in my address bar It appears the Apache web root and not the tomcat webapp in apache, don't know what am I doing wrong or how to check what is wrong ...

    In file /etc/apache2/sites-available/genetic, shouldn't you have port 8080 instead of 80?

  3. Hi buddy.
    I know tomcat and apache2 are connected with ajp and mod_jk.

    But I am wondering how to connect a Tomcat to another tomcat web server on different machines?

    Is this possible?


  4. Got same prob as in the second comment

  5. Hi, I have the same problem when I finished all the steps, help me:
    "It appears the Apache web root and not the tomcat webapp in apache", and I have posrt 8080 instead of 80 in file /etc/apache2/sites-available/genetic.

    Should I can do? thx!

  6. sudo gedit /etc/apache2/sites-available/Test

    I created my application's file as follows:


    JKMount /Test/* worker
    JKMount /Test worker
    RewriteEngine on
    RewriteRule ^/$ /Test/ [R=permanent]
    RewriteRule ^/Test/(.*)$ ajp://localhost:8009/Test/$1 [P]

    When I type in url box, the program run well. But I see in url box.
    I really want to see it as only. How can I do, please?

  7. Your post really helped me to understand this. It has great details and yet it is easy to understand.That's what i was looking for. I will definitely share it with others.Thanks for sharing. web hosting service india

  8. Your blog is amazing! I love it so much, from now on I'm gonna check it every day! thank you so much for being awesome and blog!

  9. I impressed with this post. I rarely found this. I came across this and interesting stuff is present here.I will bookmark your website and share with my friends. I am waiting for your next interesting post.

    send text message from computeryachtcrew

  10. You made certain good points there. I did a search on the subject matter and found the majority of people will have the same opinion with your blog..

  11. Hello guy,

    I' trying to use your tutorial to integrate tomcat7 and apache2, howeverm, after try your full tutorial nothing work. I have to continue to put :8080/myapplication to access myapplication. I say the same Annonimous sad: "When I write in my address bar It appears the Apache web root and not the tomcat webapp in apache, don't know what am I doing wrong or how to check what is wrong ..."

    Could you help me?

  12. Heya¡­my very first comment on your site. ,I have been reading your blog for a while and thought I would completely pop in and drop a friendly note. . It is great stuff indeed. I also wanted to there a way to subscribe to your site via email?

    Web Hosting India

  13. Hello guy,

    I' trying to use your tutorial to integrate tomcat7 and apache2
    At the begening, every thing is Ok but i write this command :
    "sudo jar xvf genetic.war" it appeares this exception:" genetic.war (No such file or directory)
    at Method)
    i don't know what shall i do
    please i need your help.

  14. All this is a load. None of it works. I think the idea is to screw up your appache installation. What some people do for kicks.

  15. Thx man! it works! =)

  16. I’m impressed. You’re truly well informed and very brilliant. You wrote something that people could comprehend and made the subject Bewitching for everybody. I’m saving this for future use........ by OPS 571 Final Exam provider.

  17. Everything is very open with a really clear description of the challenges. It was truly informative. Your site is very helpful. Thanks for sharing!. ..... by BIS 220 Week 5 provider.

  18. I simply scan through the full item of yours and it had been rather smart. this can be an outsized article thanks for distributing this informative information. i will be able to visit your diary oftentimes for a few latest post!!!!!!!!!by
    MGT 311 Week 2 Individual Assignment

  19. I have discover some good stuff here. decisively price bookmarking for revisiting. i ponder however a alallotmentment try you set to make the kind of glorious informative web MGT 311 Final Exam

  20. You made certain good points there.
    thank you for your generous ETH 316 Week 5 Individual Assignment provider

  21. Great content i would be glad if you still post these type of Articles so that i can gain lot of information.For more information please go through
    MGT 498 Week 4

  22. I afflicted with this post. I not often begin this. I came beyond this and absorbing being is present here.I will bookmark your website and allotment with my friends. I am cat-and-mouse for your abutting absorbing ETH 316 Final provider

  23. Your blog is amazing! I adulation it so much, from now on I'm gonna analysis it every day! acknowledge you so abundant for actuality alarming and blog!by LDR 531 Week 3 Assignment

  24. The first thing is that you have to plan your time. It means that you have to share your study load on all of the months of the semester. When doing so you have already acquired the required knowledge before the exam day approaches....for more information you can also go through
    MGT 498

  25. actual aboriginal animadversion on your site. ,I accept been account your blog for a while and anticipation I would absolutely pop in and bead a affable note. . It is abundant being indeed. I additionally capital to ask... by ETH 316 Week 3 Idividual Assignment provider

  26. Your column absolutely helped me to accept this. It has abundant capacity and yet it is accessible to understand.That's what i was attractive for. I will absolutely allotment it with others.Thanks for sharing... by ETH 316 Week 5 Idividual Assignment provider

  27. I am very much pleased with the contents you have mentioned. It contains truly information. I want to thank you for this informative read;by MGT 521 Entire Course provider

  28. Great agreeable i would be animated if you still column these blazon of Articles so that i can accretion lot of information.For added advice amuse go through by
    MGT 420 Week 2 provider.

  29. Thank you so much for providing this valuable information. It is very important to us. You Posted a Good Stuff... by MGT 521 provider

  30. I am very joyous when read this blog posted letters because blog posted letters in writing in good kind and create on good theme.
    by MKT 571 Individual Assignment Provider