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:
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

35 commenti:

  1. thanks! nice write-up!

    RispondiElimina
  2. When I write www.genetic.it 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?

    RispondiElimina
  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?

    regards

    RispondiElimina
  4. Got same prob as in the second comment

    RispondiElimina
  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!

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

    I created my application's file as follows:

    NameVirtualHost www.Test.com:80

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


    When I type www.Test.com in url box, the program run well. But I see http://www.test.com/Test/ in url box.
    I really want to see it as http://www.Test.com only. How can I do, please?

    RispondiElimina
  7. 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!

    RispondiElimina
  8. 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

    RispondiElimina
  9. 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..

    RispondiElimina
  10. 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 www.genetic.it 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?

    RispondiElimina
  11. 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:"
    java.io.FileNotFoundException: genetic.war (No such file or directory)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.(FileInputStream.java:128)
    at java.io.FileInputStream.(FileInputStream.java:87)
    at sun.tools.jar.Main.run(Main.java:259)
    at sun.tools.jar.Main.main(Main.java:1177)
    "
    i don't know what shall i do
    please i need your help.
    Thanks

    RispondiElimina
  12. 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.

    RispondiElimina
  13. 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

    RispondiElimina
  14. 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 site.by MGT 311 Final Exam

    RispondiElimina
  15. You made certain good points there.
    thank you for your generous post...by ETH 316 Week 5 Individual Assignment provider

    RispondiElimina
  16. 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

    RispondiElimina
  17. 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 post.by ETH 316 Final provider



    RispondiElimina
  18. 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

    RispondiElimina
  19. 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

    RispondiElimina
  20. 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


    RispondiElimina
  21. 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

    RispondiElimina
  22. 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.

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


    RispondiElimina
  24. 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

    RispondiElimina
  25. Thank you so much for providing this valuable information. It is very important to us. You Posted a Good Stuff... by SchoolsList India

    RispondiElimina
  26. Thank you for sharing valuable information. Nice post. I enjoyed reading this post.
    Signature:
    al3ab banat 01

    RispondiElimina
  27. Questo commento è stato eliminato dall'autore.

    RispondiElimina
  28. I am searching for methods to Connect Tomcat 7 with Apache2 (mod_jk) and install Virtual Host part of my apache tomcat training and i found very useful methods here.thank you.

    RispondiElimina
  29. Virtual hosting is the hosting of multiple domain on single server. But, anyways thanks for detail structuring about virtual domain.
    Web Hosting Service

    RispondiElimina