wiki/HowtoTomcat.md

151 lines
4.3 KiB
Markdown
Raw Normal View History

2017-02-20 17:06:36 +01:00
---
categories: java
title: Howto Tomcat
...
2016-12-29 11:25:39 +01:00
2017-02-20 17:06:36 +01:00
* Documentation : <http://tomcat.apache.org/tomcat-7.0-doc/>
* Rôle Ansible : <https://forge.evolix.org/projects/ansible-roles/repository/revisions/unstable/show/tomcat-instance>
2016-12-29 11:25:39 +01:00
2017-02-20 17:06:36 +01:00
## Installation
2016-12-29 11:25:39 +01:00
2017-02-20 17:06:36 +01:00
Cette documentation se concentre sur l'installation et la configuration de Tomcat sur Debian Jessie en mode multi-instances.
2016-12-29 11:25:39 +01:00
2017-02-20 17:06:36 +01:00
Le mode multi-instance est gérer via une unité systemd en espace utilisateur et permet de faire tourner plusieurs instances Tomcat de facon complètement indépendantes, avec leurs propres réglages et en permettant des arrêts/redémarrages d'une instance sans impacter les autres tout en mutualisant les binaires.
2016-12-29 11:25:39 +01:00
~~~
2017-02-20 17:06:36 +01:00
# apt install tomcat7 tomcat7-user libpam-systemd
2016-12-29 11:25:39 +01:00
~~~
### Unité systemd utilisateur
2017-02-20 17:06:36 +01:00
Créer l'unité tomcat dans /etc/systemd/user/tomcat.service.
2016-12-29 11:25:39 +01:00
~~~
[Unit]
Description=Tomcat %u.
After=network.target
[Service]
2017-02-20 17:06:36 +01:00
WorkingDirectory=%h
Environment="CATALINA_BASE=%h"
EnvironmentFile=%h/conf/env
2016-12-29 11:25:39 +01:00
UMask=0002
ExecStart=/usr/share/tomcat7/bin/startup.sh
ExecStop=/usr/share/tomcat7/bin/shutdown.sh
Type=forking
[Install]
WantedBy=default.target
~~~
2017-02-20 17:06:36 +01:00
## Création d'une instance
2016-12-29 11:25:39 +01:00
2017-02-20 17:06:36 +01:00
Créer un utilisateur dédié a l'instance et créer son arborescence via tomcat7-instance-create.
2016-12-29 11:25:39 +01:00
~~~
2017-02-20 17:06:36 +01:00
# mkdir -p /srv/tomcat
# tomcat7-instance-create /srv/tomcat/app
# useradd -d /srv/tomcat/app app
# chown -R app:app /srv/tomcat/app
# chmod -R u=rwX,g=rX,o= /srv/tomcat/app
# chmod -R g+ws /srv/tomcat/app
2016-12-29 11:25:39 +01:00
~~~
2017-02-20 17:06:36 +01:00
Créer les variables d'environnement dans /srv/tomcat/app/conf/env
2016-12-29 11:25:39 +01:00
~~~
2017-02-20 17:06:36 +01:00
# Memory allocation options.
# Xmx Max memory allocated to instance.
# Xms Allocated memory at startup.
# XX:MaxPermSize Memory allocated to internal objects.
JAVA_OPTS="-server -Xmx{{ tomcat_instance_ram }}m -Xms{{ tomcat_instance_ram }}m -XX:MaxPermSize={{ tomcat_instance_mps }}m -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+CMSPermGenSweepingEnabled -XX:+CMSClassUnloadingEnabled -Xverify:none"
2016-12-29 11:25:39 +01:00
~~~
2017-02-20 17:06:36 +01:00
Il reste ensuite ajuster les ports HTTP et SHUTDOWN dans /srv/tomcat/app/conf/server.xml
2016-12-29 11:25:39 +01:00
2017-02-20 17:06:36 +01:00
Activer la conservation de la session de l'utilisateur.
2016-12-29 11:25:39 +01:00
~~~
2017-02-20 17:06:36 +01:00
# loginctl enable-linger foo
2016-12-29 11:25:39 +01:00
~~~
### Reverse proxy avec Apache
2017-02-20 17:06:36 +01:00
Activez le mod proxy et proxy_http :
2016-12-29 11:25:39 +01:00
~~~
2017-02-20 17:06:36 +01:00
a2enmod proxy_http
~~~
2016-12-29 11:25:39 +01:00
2017-01-03 11:20:35 +01:00
~~~{.apache}
2016-12-29 11:25:39 +01:00
<VirtualHost *:80>
2017-02-20 17:06:36 +01:00
ServerName SERVER_NAME
CustomLog /var/log/apache2/access.log combined
CustomLog /var/log/apache2/INSTANCE_NAME_access.log combined
ErrorLog /var/log/apache2/INSTANCE_NAME_error.log
Include /etc/apache2/nocache-js.conf
<IfModule mod_proxy_http.c>
ProxyPass / http://127.0.0.1:HTTP_PORT/
ProxyPassReverse / http://127.0.0.1:HTTP_PORT/
<Proxy http://127.0.0.1:HTTP_PORT/>
Allow from all
</Proxy>
</IfModule>
2016-12-29 11:25:39 +01:00
</VirtualHost>
~~~
2017-02-20 17:06:36 +01:00
### Activation du « access_log » de Tomcat
2016-12-29 11:25:39 +01:00
2017-02-20 17:06:36 +01:00
Pour loguer tous les accès, il suffit de décommenter cette partie dans le server.xml :
2017-01-03 11:20:35 +01:00
2017-02-20 17:06:36 +01:00
~~~{.xml}
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false"/>
2016-12-29 11:25:39 +01:00
~~~
2017-02-20 17:06:36 +01:00
### Connecteur HTTP
2016-12-29 11:25:39 +01:00
2017-02-20 17:06:36 +01:00
Activer la compression gzip de certains types de fichiers :
2016-12-29 11:25:39 +01:00
2017-02-20 17:06:36 +01:00
Dans le fichier _/etc/tomcat6/server.xml_, rajouter ceci dans le connecteur HTTP :
2017-01-03 11:20:35 +01:00
2016-12-29 11:25:39 +01:00
~~~
2017-02-20 17:06:36 +01:00
compression="on" compressionMinSize="2048" compressableMimeType="text/html,text/xml,text/plain,text/javascript,application/javascript"
2016-12-29 11:25:39 +01:00
~~~
2017-02-20 17:06:36 +01:00
## Utilisation
2016-12-29 11:25:39 +01:00
2017-02-20 17:06:36 +01:00
Enfin, l'utilisateur pour activer l'instance au démarrage et la démarrer, voir le status, …
2017-01-03 11:20:35 +01:00
2016-12-29 11:25:39 +01:00
~~~
2017-02-20 17:06:36 +01:00
$ systemctl --user enable tomcat
$ systemctl --user start tomcat
$ systemctl --user status -l tomcat
$ systemctl --user restart tomcat
$ systemctl --user stop tomcat
2016-12-29 11:25:39 +01:00
~~~
2017-02-20 17:06:36 +01:00
## Manager Tomcat
2016-12-29 11:25:39 +01:00
Une des méthodes possibles pour le déployment d'applications java est de le faire via une interface web, le manager Tomcat.
Installation :
2017-01-03 11:20:35 +01:00
2016-12-29 11:25:39 +01:00
~~~
2017-02-20 17:06:36 +01:00
# apt install tomcat7-admin
2016-12-29 11:25:39 +01:00
~~~
Il faut ensuite ajouter un utilisateur au role _manager_, dans le fichier _/etc/tomcat6/tomcat-users.xml_ (équivalent d'un fichier htpasswd d'Apache) :
2017-01-03 11:20:35 +01:00
2016-12-29 11:25:39 +01:00
~~~
<tomcat-users>
[...]
<role rolename="manager"/>
<user username="jdoe" password="s3cret" roles="manager"/>
</tomcat-users>
~~~
Une fois Tomcat redémarré, l'accès au manager ce fait sur l'URI : <http://example.com:8080/manager/html>