Difference between revisions of "Tomcat"

From Blue-IT.org Wiki

(Administration interface)
 
(7 intermediate revisions by the same user not shown)
Line 13: Line 13:
 
  #> vim /etc/tomcat6/tomcat-users.xml
 
  #> vim /etc/tomcat6/tomcat-users.xml
 
  [...]
 
  [...]
 +
 +
<role rolename="manager-gui"/>
 +
<role rolename="manager-script"/>
 
  <role rolename="manager"/>
 
  <role rolename="manager"/>
 +
<role rolename="admin-gui"/>
 +
<role rolename="admin-script"/>
 
  <role rolename="admin"/>
 
  <role rolename="admin"/>
  <user username="tomcat" password="123-qwe" roles="manager,admin"/>
+
  <user username="tomcat" password="123-qwe" roles="manager-gui,admin-gui,manager,admin,manager-script,admin-script"/>
 
+
 
  </tomcat-users>
 
  </tomcat-users>
 
  [...]
 
  [...]
  
 
== Administration interface ==
 
== Administration interface ==
When running on prot 8088 (! standard is 8080) e.g.:
+
When running on port 8088 (! standard is 8080) e.g.:
  [http://localhost:8088/manager/html|http://localhost:8088/manager/html]
+
  [http://localhost:8088/manager/html http://localhost:8088/manager/html]
 +
 
 +
== Log4j ==
 +
# Put ''log4j-xx.jar'' to ''/var/lib/tomcat6/common/classes''
 +
# Put ''log4j.properties'' in ''/var/lib/tomcat6/common/lib''
 +
 
 +
See [[Grails]]
 +
 
 +
= Integration Tomcat6 / Eclipse =
 +
'''DO NOT DO THIS ON THE SERVER - only for testing on a local PC!!!'''
 +
 
 +
See: [http://connectedweb.wordpress.com/2010/05/18/how-to-make-tomcat6-work-with-ubuntu-10-04-and-eclipse/ http://connectedweb.wordpress.com/2010/05/18/how-to-make-tomcat6-work-with-ubuntu-10-04-and-eclipse/]
 +
 
 +
cd /usr/share/tomcat6
 +
sudo ln -s /var/lib/tomcat6/conf conf
 +
sudo ln -s /etc/tomcat6/policy.d/03catalina.policy conf/catalina.policy
 +
sudo ln -s /var/log/tomcat6 log
 +
sudo chmod -R 777 /usr/share/tomcat6/conf
 +
 +
sudo /etc/init.d/tomcat6 stop
 +
 +
 
 +
In Eclipse, click on Window/Show View/Servers If there is a tomcat6 server in there, delete it. Create a new one by right clicking in that Servers view. If you were in the middle of developing an application, you should (if it asks) associate the application with tomcat6.
 +
 
 +
== Configure Tomcat6 ==
 +
/etc/tomcat6/
 +
├── Catalina
 +
│&nbsp;&nbsp; └── localhost
 +
│&nbsp;&nbsp;    ├── host-manager.xml
 +
│&nbsp;&nbsp;    ├── manager.xml
 +
│&nbsp;&nbsp;    └── ROOT.xml
 +
├── catalina.properties
 +
├── context.xml
 +
├── logging.properties
 +
├── policy.d
 +
│&nbsp;&nbsp; ├── 01system.policy
 +
│&nbsp;&nbsp; ├── 02debian.policy
 +
│&nbsp;&nbsp; ├── 03catalina.policy
 +
│&nbsp;&nbsp; ├── 04webapps.policy
 +
│&nbsp;&nbsp; └── 50local.policy
 +
├── server.xml
 +
├── tomcat-users.xml
 +
└── web.xml
 +
 +
 
 +
==== tomcat-users.xml ====
 +
<nowiki><tomcat-users></nowiki>
 +
<nowiki><role rolename="admin"/></nowiki>
 +
<nowiki><role rolename="admin-gui"/></nowiki>
 +
<nowiki><role rolename="admin-script"/></nowiki>
 +
<nowiki><role rolename="manager"/></nowiki>
 +
<nowiki><role rolename="manager-gui"/></nowiki>
 +
<nowiki><role rolename="manager-script"/></nowiki>
 +
<nowiki><role rolename="manager-jmx"/></nowiki>
 +
<nowiki><role rolename="manager-status"/></nowiki>
 +
<nowiki><user username="username" password="mypass" roles="admin,admin-gui,admin-script,manager,manager-gui,manager-script,manager-jmx,manager-status"/></nowiki>
 +
<nowiki></tomcat-users></nowiki>
 +
 +
 
 +
Now you have acces to the tomcat management interface on [http://yourdomain/manager/html http://yourdomain/manager/html] . But, if you like to deploy applications within the root context you probably never won't use it for managing your tomcat!
 +
 
 +
== Tomcat and PHP - mixed environment ==
 +
Given scenario:
 +
 
 +
* Jsp application on [http://yourdomain/ http://yourdomain]
 +
* PHP application on [http://yourdomain/myphpapp http://yourdomain/myphpapp]
 +
 
 +
Drawbacks:
 +
 
 +
* PHP application is deployed in the same directory (physically on the server) as the jsp application.
 +
* PHP application cannot be allocated using symlinks (apache's „Follow symlinks“)
 +
 
 +
=== Tomcat 6 worker ===
 +
This will make a php page accessible under the ROOT directory of a tomcat installation under the following prerequesites:
 +
 
 +
* The php application must be installed under this ROOT dir (no symlinks)
 +
* Usage of mod_jk
 +
* The GrailsApp is deployed within a ROOT context
 +
 
 +
vim /etc/libapache2-mod-jk/workers.properties
 +
worker.list=ajp13_worker,myapp_worker
 +
<nowiki>[...]</nowiki>
 +
 +
worker.myapp_worker.port=8009
 +
worker.myapp_worker.host=myapp.v2201109100236260.yourvserver.net
 +
worker.myapp_worker.type=ajp13
 +
<nowiki>[...]</nowiki>
 +
 +
 
 +
'''NOTE:''' ajp13_worker is the default worker on ubuntu / debian
 +
 
 +
=== Apache virtual host ===
 +
vim /etc/apache2/sites-available/demo_jk.conf
 +
 
 +
<nowiki><VirtualHost IP:80></nowiki>
 +
 +
  ServerName subdomain.domain.tld
 +
 +
  DocumentRoot "/var/lib/tomcat6/webapps/myapp/ROOT"
 +
 +
  JkMount /** demo_worker
 +
  JkUnmount /myphpapp/** demo_worker
 +
 +
  <nowiki># JK.conf</nowiki>
 +
  Alias /myapp "/var/lib/tomcat6/webapps/myapp/ROOT"
 +
 +
  <nowiki><Directory "/var/lib/tomcat6/webapps/myapp/ROOT"></nowiki>
 +
        Options Indexes FollowSymLinks
 +
        <nowiki># This is for grails, use .jsp for java server pages</nowiki>
 +
        AddHandler jakarta-servlet2 .gsp
 +
  <nowiki></Directory></nowiki>
 +
 +
  <nowiki><Location "/myapp/WEB-INF"></nowiki>
 +
        Deny from all
 +
  <nowiki></Location></nowiki>
 +
 +
  <nowiki><Location "/myapp/META-INF"></nowiki>
 +
        Deny from all
 +
  <nowiki></Location></nowiki>
 +
 
 +
  <nowiki># Optional</nowiki>
 +
  <nowiki># SetEnvIf Request_URI \.php no-jk</nowiki>
 +
 +
<nowiki></VirtualHost></nowiki>
 +
 +
 
 +
The important line for your php application root is
 +
 
 +
JkUnmount /myphpapp/** demo_worker
 +
 +
 
 +
A php installation (e.g. TYPO 3) has to be installed in ''/var/lib/tomcat6/webapps/myapp/ROOT/'''myphpapp''''' and then accessible under [http://yourdomain/myphpapp http://yourdomain/myphpapp] .
 +
 
 +
=== Root Context ===
 +
Application Developer's Guide
 +
 
 +
* [http://tomcat.apache.org/tomcat-6.0-doc/config/context.html The Context Container]
 +
 
 +
Apache Tomcat Configuration Reference:
 +
 
 +
* [http://tomcat.apache.org/tomcat-6.0-doc/appdev/deployment.html Deployment]
 +
 
 +
Deploying „myapp“ as ROOT:
 +
 
 +
'''1. Creating a virtual host in Tomcat:'''
 +
 
 +
/etc/tomcat6/server.xml
 +
 +
<nowiki> <Engine ...</nowiki>
 +
<nowiki> <Host ...</nowiki>
 +
<nowiki> </Host></nowiki>
 +
<nowiki> <Host name="subdomain.domain.tld" </nowiki> appBase="webapps/myapp"
 +
            unpackWARs="true" autoDeploy="true"
 +
            xmlValidation="false" xmlNamespaceAware="false">
 +
            <nowiki><alias>www.subdomain.domain.tld</alias></nowiki>
 +
<nowiki> </Host></nowiki>
 +
 +
 
 +
'''2. Creating a new ROOT directory'''
 +
 
 +
MYAPP="myapp"
 +
mkdir /var/lib/tomcat6/webapps/${MYAPP}/
 +
 +
 
 +
This is the place where the ROOT.war file will be placed for deploying the wepapp.
 +
 
 +
'''3. Creating a new ROOT.xml and directory for the tomcat virtual host'''
 +
 
 +
mkdir -p /etc/tomcat6/conf/Catalina/${MYAPP}/ROOT.xml
 +
vim /etc/tomcat6/conf/Catalina/${MYAPP}/ROOT.xml
 +
 +
<nowiki><Context path="/" </nowiki>
 +
        antiResourceLocking="false" />
 +
 +
 
 +
=== Deploy the app "ROOT.war" to it ===
 +
/var/lib/tomcat6# tree -L 4
 +
.
 +
├── common
 +
│&nbsp;&nbsp; └── classes
 +
├── conf -> /etc/tomcat6
 +
├── logs -> ../../log/tomcat6
 +
├── server
 +
│&nbsp;&nbsp; └── classes
 +
├── shared
 +
│&nbsp;&nbsp; └── classes
 +
├── webapps
 +
│&nbsp;&nbsp; └── myapp
 +
│&nbsp;&nbsp;    ├── ROOT
 +
│&nbsp;&nbsp;    │&nbsp;&nbsp; ├── css
 +
│&nbsp;&nbsp;    │&nbsp;&nbsp; ├── home
 +
│&nbsp;&nbsp;    │&nbsp;&nbsp; ├── images
 +
│&nbsp;&nbsp;    │&nbsp;&nbsp; ├── js
 +
│&nbsp;&nbsp;    │&nbsp;&nbsp; ├── META-INF
 +
│&nbsp;&nbsp;    │&nbsp;&nbsp; ├── plugins
 +
│&nbsp;&nbsp;    │&nbsp;&nbsp; └── WEB-INF
 +
│&nbsp;&nbsp;    └── ROOT.war
 +
└── work -> ../../cache/tomcat6
 +
 +
 
 +
Instead of ''-Dgrails.env=myEnv'' for a custom environment you can use the common grails environments ''dev'', ''test'' or ''prod''.
 +
 
 +
The created war '''MUST''' be named '''ROOT.war'''. In most cases you will configure a special environment for the deployment ( e.g. ''-Dgrails.env'') when creating the ''war'' file. Definitions for the named environment in ''Config.groovy'' or ''DataSource.groovy'' (or at other places) are the same as for the standard environments ''test'', ''development'' or ''producton''.
 +
 
 +
grails -Dgrails.env=myEnv war ROOT.war
 +
 +
 
 +
Save an existing php app (!) and remove the old version of your app on the server
 +
 
 +
ssh myserver "mv /var/lib/tomcat6/webapps/myapp/ROOT/myphpapp /var/lib/tomcat6/webapps/myapp/."
 +
ssh myserver "rm -rf /var/lib/tomcat6/webapps/myapp/ROOT*"
 +
 +
 
 +
Deploy it and restart the server, so it will be rolled out:
 +
 
 +
scp ROOT.war myserver:/var/lib/tomcat6/webapps/myapp/.
 +
ssh myserver "service tomcat6 restart"
 +
ssh myserver "mkdir /var/lib/tomcat6/webapps/myapp/ROOT"
 +
ssh myserver "chown -R tomcat6:tomcat6 /var/lib/tomcat6/webapps/myapp/ROOT"
 +
ssh myserver "mv /var/lib/tomcat6/webapps/myapp/myphpapp var/lib/tomcat6/webapps/myapp/ROOT/."
 +
ssh myserver "service apache2 restart"
 +
 +
 
 +
For production environment I would script this on the server and do a backup (zip with timestamp) of the ROOT directory first.
 +
 
 +
=== Restart tomcat and apache ===
 +
Always restart '''tomcat6 first''' and '''then apache2.'''
 +
 
 +
Now both apps should be up and running.
 +
 
 +
== mod_proxy and mod_ajp ==
 +
'''Go up to mod_jk above and configure your apache virtual host. mod_ajp doesn't work for unknown reasons yet! Also working with mod_proxy as described in the following part might be tricky with some applications'''
 +
 
 +
[[Image:]] '''Mod_ajp'''
 +
 
 +
'''Creating a virtual host with mod_proxy (http) in apache2:'''
 +
 
 +
apt-get install libapache2-mod-proxy-html
 +
 +
 
 +
'''Deploy via Tomcat-Manager'''
 +
 
 +
Context Path (required): /myapp   
 +
XML Configuration file URL:  myapp/ROOT.xml
 +
WAR or Directory URL: /where/ever/on/server/ROOT.war
 +
 +
<nowiki><VirtualHost *:80></nowiki>
 +
 +
  ServerName subdomain.domain.tld
 +
 
 +
  <nowiki># a2enmod proxy_http</nowiki>
 +
  <nowiki># Security:</nowiki>
 +
  ProxyRequests Off
 +
 
 +
  <nowiki><Proxy *></nowiki>
 +
    AddDefaultCharset off
 +
    Order deny,allow
 +
    Allow from .domain.tld
 +
  <nowiki></Proxy></nowiki>
 +
 +
  <nowiki><Location /></nowiki>
 +
        ProxyPass http://subdomain.domain.tld:8080/
 +
        ProxyPassReverse http://subdomain.domain.tld:8080/
 +
  <nowiki></Location></nowiki>
 +
 +
<nowiki></VirtualHost></nowiki>
 +
 +
 
 +
[[Image:]]''' The Role of a context.xml in the META-INF direcotory of your app'''
 +
 
 +
vim web-app/META-INF/context.xml
 +
 +
[[Category:Java, Groovy and Grails]]
 +
[[Category:Web Development]]

Latest revision as of 17:24, 12 January 2012

Tomcat6 / Ubuntu 10.04 LTS

Alter listening port

#> vim /etc/tomcat/server.xml
[...]
   <Connector port="8088" protocol="HTTP/1.1" 
              connectionTimeout="20000" 
              URIEncoding="UTF-8"
              redirectPort="8443" />
[...]

Add administrative user

#> vim /etc/tomcat6/tomcat-users.xml
[...]
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="manager"/>
<role rolename="admin-gui"/>
<role rolename="admin-script"/>
<role rolename="admin"/>
<user username="tomcat" password="123-qwe" roles="manager-gui,admin-gui,manager,admin,manager-script,admin-script"/>

</tomcat-users>
[...]

Administration interface

When running on port 8088 (! standard is 8080) e.g.:

http://localhost:8088/manager/html

Log4j

  1. Put log4j-xx.jar to /var/lib/tomcat6/common/classes
  2. Put log4j.properties in /var/lib/tomcat6/common/lib

See Grails

Integration Tomcat6 / Eclipse

DO NOT DO THIS ON THE SERVER - only for testing on a local PC!!!

See: http://connectedweb.wordpress.com/2010/05/18/how-to-make-tomcat6-work-with-ubuntu-10-04-and-eclipse/

cd /usr/share/tomcat6
sudo ln -s /var/lib/tomcat6/conf conf
sudo ln -s /etc/tomcat6/policy.d/03catalina.policy conf/catalina.policy
sudo ln -s /var/log/tomcat6 log
sudo chmod -R 777 /usr/share/tomcat6/conf

sudo /etc/init.d/tomcat6 stop

In Eclipse, click on Window/Show View/Servers If there is a tomcat6 server in there, delete it. Create a new one by right clicking in that Servers view. If you were in the middle of developing an application, you should (if it asks) associate the application with tomcat6.

Configure Tomcat6

/etc/tomcat6/
├── Catalina
│   └── localhost
│       ├── host-manager.xml
│       ├── manager.xml
│       └── ROOT.xml
├── catalina.properties
├── context.xml
├── logging.properties
├── policy.d
│   ├── 01system.policy
│   ├── 02debian.policy
│   ├── 03catalina.policy
│   ├── 04webapps.policy
│   └── 50local.policy
├── server.xml
├── tomcat-users.xml
└── web.xml

tomcat-users.xml

<tomcat-users>
<role rolename="admin"/>
<role rolename="admin-gui"/>
<role rolename="admin-script"/>
<role rolename="manager"/>
<role rolename="manager-gui"/>
<role rolename="manager-script"/>
<role rolename="manager-jmx"/>
<role rolename="manager-status"/>
<user username="username" password="mypass" roles="admin,admin-gui,admin-script,manager,manager-gui,manager-script,manager-jmx,manager-status"/>
</tomcat-users>

Now you have acces to the tomcat management interface on http://yourdomain/manager/html . But, if you like to deploy applications within the root context you probably never won't use it for managing your tomcat!

Tomcat and PHP - mixed environment

Given scenario:

Drawbacks:

  • PHP application is deployed in the same directory (physically on the server) as the jsp application.
  • PHP application cannot be allocated using symlinks (apache's „Follow symlinks“)

Tomcat 6 worker

This will make a php page accessible under the ROOT directory of a tomcat installation under the following prerequesites:

  • The php application must be installed under this ROOT dir (no symlinks)
  • Usage of mod_jk
  • The GrailsApp is deployed within a ROOT context
vim /etc/libapache2-mod-jk/workers.properties
worker.list=ajp13_worker,myapp_worker
[...]

worker.myapp_worker.port=8009
worker.myapp_worker.host=myapp.v2201109100236260.yourvserver.net
worker.myapp_worker.type=ajp13
[...]

NOTE: ajp13_worker is the default worker on ubuntu / debian

Apache virtual host

vim /etc/apache2/sites-available/demo_jk.conf

<VirtualHost IP:80>

  ServerName subdomain.domain.tld

  DocumentRoot "/var/lib/tomcat6/webapps/myapp/ROOT"

  JkMount /** demo_worker
  JkUnmount /myphpapp/** demo_worker

  # JK.conf
  Alias /myapp "/var/lib/tomcat6/webapps/myapp/ROOT"

  <Directory "/var/lib/tomcat6/webapps/myapp/ROOT">
       Options Indexes FollowSymLinks
       # This is for grails, use .jsp for java server pages
       AddHandler jakarta-servlet2 .gsp
  </Directory>

  <Location "/myapp/WEB-INF">
       Deny from all
  </Location>

  <Location "/myapp/META-INF">
       Deny from all
  </Location>
  
  # Optional
  # SetEnvIf Request_URI \.php no-jk

</VirtualHost>

The important line for your php application root is

JkUnmount /myphpapp/** demo_worker

A php installation (e.g. TYPO 3) has to be installed in /var/lib/tomcat6/webapps/myapp/ROOT/myphpapp and then accessible under http://yourdomain/myphpapp .

Root Context

Application Developer's Guide

Apache Tomcat Configuration Reference:

Deploying „myapp“ as ROOT:

1. Creating a virtual host in Tomcat:

/etc/tomcat6/server.xml

 <Engine ...
 <Host ...
 </Host>
 <Host name="subdomain.domain.tld"  appBase="webapps/myapp"
            unpackWARs="true" autoDeploy="true"
            xmlValidation="false" xmlNamespaceAware="false">
            <alias>www.subdomain.domain.tld</alias>
 </Host>

2. Creating a new ROOT directory

MYAPP="myapp"
mkdir /var/lib/tomcat6/webapps/${MYAPP}/

This is the place where the ROOT.war file will be placed for deploying the wepapp.

3. Creating a new ROOT.xml and directory for the tomcat virtual host

mkdir -p /etc/tomcat6/conf/Catalina/${MYAPP}/ROOT.xml
vim /etc/tomcat6/conf/Catalina/${MYAPP}/ROOT.xml

<Context path="/" 
        antiResourceLocking="false" />

Deploy the app "ROOT.war" to it

/var/lib/tomcat6# tree -L 4
.
├── common
│   └── classes
├── conf -> /etc/tomcat6
├── logs -> ../../log/tomcat6
├── server
│   └── classes
├── shared
│   └── classes
├── webapps
│   └── myapp
│       ├── ROOT
│       │   ├── css
│       │   ├── home
│       │   ├── images
│       │   ├── js
│       │   ├── META-INF
│       │   ├── plugins
│       │   └── WEB-INF
│       └── ROOT.war
└── work -> ../../cache/tomcat6

Instead of -Dgrails.env=myEnv for a custom environment you can use the common grails environments dev, test or prod.

The created war MUST be named ROOT.war. In most cases you will configure a special environment for the deployment ( e.g. -Dgrails.env) when creating the war file. Definitions for the named environment in Config.groovy or DataSource.groovy (or at other places) are the same as for the standard environments test, development or producton.

grails -Dgrails.env=myEnv war ROOT.war

Save an existing php app (!) and remove the old version of your app on the server

ssh myserver "mv /var/lib/tomcat6/webapps/myapp/ROOT/myphpapp /var/lib/tomcat6/webapps/myapp/."
ssh myserver "rm -rf /var/lib/tomcat6/webapps/myapp/ROOT*"

Deploy it and restart the server, so it will be rolled out:

scp ROOT.war myserver:/var/lib/tomcat6/webapps/myapp/.
ssh myserver "service tomcat6 restart"
ssh myserver "mkdir /var/lib/tomcat6/webapps/myapp/ROOT"
ssh myserver "chown -R tomcat6:tomcat6 /var/lib/tomcat6/webapps/myapp/ROOT"
ssh myserver "mv /var/lib/tomcat6/webapps/myapp/myphpapp var/lib/tomcat6/webapps/myapp/ROOT/."
ssh myserver "service apache2 restart"

For production environment I would script this on the server and do a backup (zip with timestamp) of the ROOT directory first.

Restart tomcat and apache

Always restart tomcat6 first and then apache2.

Now both apps should be up and running.

mod_proxy and mod_ajp

Go up to mod_jk above and configure your apache virtual host. mod_ajp doesn't work for unknown reasons yet! Also working with mod_proxy as described in the following part might be tricky with some applications

[[Image:]] Mod_ajp

Creating a virtual host with mod_proxy (http) in apache2:

apt-get install libapache2-mod-proxy-html

Deploy via Tomcat-Manager

Context Path (required): /myapp    
XML Configuration file URL:  myapp/ROOT.xml
WAR or Directory URL: /where/ever/on/server/ROOT.war

<VirtualHost *:80>

  ServerName subdomain.domain.tld
 
  # a2enmod proxy_http
  # Security:
  ProxyRequests Off
  
  <Proxy *>
    AddDefaultCharset off
    Order deny,allow
    Allow from .domain.tld
  </Proxy>

  <Location />
        ProxyPass http://subdomain.domain.tld:8080/
        ProxyPassReverse http://subdomain.domain.tld:8080/
  </Location>

</VirtualHost>

[[Image:]] The Role of a context.xml in the META-INF direcotory of your app

vim web-app/META-INF/context.xml