201 lines
5 KiB
Markdown
201 lines
5 KiB
Markdown
---
|
|
categories: java
|
|
title: Howto Tomcat
|
|
...
|
|
|
|
* 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>
|
|
|
|
## Installation
|
|
|
|
Cette documentation se concentre sur l'installation et la configuration de Tomcat sur Debian Jessie en mode multi-instances.
|
|
|
|
Ce mode permet de faire tourner plusieurs instances Tomcat de façon complètement indépendantes, avec leurs propres réglages et en permettant des arrêts/redémarrages d'une instance sans impacter les autres, le tout en mutualisant les binaires.
|
|
|
|
Sous Debian Jessie, Tomcat 7 et 8 sont disponibles.
|
|
|
|
~~~
|
|
# apt install tomcat7 tomcat7-user || apt install tomcat8 tomcat8-user
|
|
~~~
|
|
|
|
### Unité systemd utilisateur
|
|
|
|
Installer la librairie PAM pour systemd :
|
|
|
|
~~~
|
|
# apt install libpam-systemd
|
|
~~~
|
|
|
|
Créer l'unité tomcat dans /etc/systemd/user/tomcat.service.
|
|
|
|
~~~{.ini}
|
|
[Unit]
|
|
Description=Tomcat %u.
|
|
After=network.target
|
|
|
|
[Service]
|
|
WorkingDirectory=%h
|
|
Environment="CATALINA_BASE=%h"
|
|
EnvironmentFile=%h/conf/env
|
|
UMask=0002
|
|
# Pour Tomcat 7
|
|
ExecStart=/usr/share/tomcat7/bin/startup.sh
|
|
ExecStop=/usr/share/tomcat7/bin/shutdown.sh
|
|
# Pour Tomcat 8
|
|
#ExecStart=/usr/share/tomcat8/bin/startup.sh
|
|
#ExecStop=/usr/share/tomcat8/bin/shutdown.sh
|
|
Type=forking
|
|
|
|
[Install]
|
|
WantedBy=default.target
|
|
~~~
|
|
|
|
## Création d'une instance
|
|
|
|
Créer un utilisateur dédié à l'instance et créer son arborescence via tomcatX-instance-create.
|
|
|
|
~~~
|
|
# mkdir -p /srv/tomcat
|
|
# tomcat7-instance-create /srv/tomcat/app || tomcat8-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
|
|
~~~
|
|
|
|
### Configuration
|
|
|
|
Créer les variables d'environnement dans /srv/tomcat/app/conf/env
|
|
|
|
~~~{.bash}
|
|
# Memory allocation options.
|
|
# Xmx Max memory allocated to instance.
|
|
# Xms Allocated memory at startup.
|
|
# XX:MaxPermSize Memory allocated to internal objects.
|
|
JAVA_HOME="/usr/lib/jvm/java-1.7.0-openjdk-amd64"
|
|
JAVA_OPTS="-server -XmxTOMCAT_INSTANCE_RAMm -XmsTOMCAT_INSTANCE_RAMm -XX:MaxPermSize=(TOMCAT_INSTANCE_RAM/2)m -XX:+UseParNewGC -XX:+UseConcMarkSweepGC -XX:+CMSPermGenSweepingEnabled -XX:+CMSClassUnloadingEnabled -Xverify:none"
|
|
~~~
|
|
|
|
Il reste ensuite à ajuster les ports HTTP et SHUTDOWN dans /srv/tomcat/app/conf/server.xml
|
|
|
|
~~~{.xml}
|
|
<Server port="SHUTDOWN_PORT" shutdown="SHUTDOWN">
|
|
<Connector port="HTTP_PORT" protocol="HTTP/1.1" connectionTimeout="20000" URIEncoding="UTF-8" redirectPort="8443"/>
|
|
~~~
|
|
|
|
### Garder la session utilisateur active
|
|
|
|
Activer la conservation de la session de l'utilisateur.
|
|
|
|
~~~
|
|
# loginctl enable-linger foo
|
|
~~~
|
|
|
|
### Reverse proxy avec Apache
|
|
|
|
Activer le mod proxy et proxy_http :
|
|
|
|
~~~
|
|
# a2enmod proxy_http
|
|
~~~
|
|
|
|
~~~{.apache}
|
|
<VirtualHost *:80>
|
|
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>
|
|
</VirtualHost>
|
|
~~~
|
|
|
|
## Utilisation
|
|
|
|
Les instances Tomcat étant gérées via une unité utilisateur Systemd, les commandes suivantes doivent être lancées via l'utilisateur dédié à l'instance.
|
|
|
|
~~~
|
|
# su - app
|
|
~~~
|
|
|
|
#### Activation au démarrage
|
|
|
|
~~~
|
|
$ systemctl --user enable tomcat
|
|
~~~
|
|
|
|
#### Démarrage
|
|
|
|
~~~
|
|
$ systemctl --user start tomcat
|
|
~~~
|
|
|
|
#### Extinction
|
|
|
|
~~~
|
|
$ systemctl --user stop tomcat
|
|
~~~
|
|
|
|
#### Redémarrage
|
|
|
|
~~~
|
|
$ systemctl --user restart tomcat
|
|
~~~
|
|
|
|
#### Status
|
|
|
|
~~~
|
|
$ systemctl --user status -l tomcat
|
|
~~~
|
|
|
|
## Divers
|
|
|
|
### Activation du « access_log » de Tomcat
|
|
|
|
Pour loguer tous les accès, il suffit de décommenter cette partie dans le server.xml :
|
|
|
|
~~~{.xml}
|
|
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
|
|
prefix="localhost_access_log." suffix=".txt" pattern="common" resolveHosts="false"/>
|
|
~~~
|
|
|
|
### Connecteur HTTP
|
|
|
|
Activer la compression gzip de certains types de fichiers :
|
|
|
|
Dans le fichier server.xml, rajouter ceci dans le connecteur HTTP :
|
|
|
|
~~~
|
|
compression="on" compressionMinSize="2048" compressableMimeType="text/html,text/xml,text/plain,text/javascript,application/javascript"
|
|
~~~
|
|
|
|
### Manager Tomcat
|
|
|
|
Une des méthodes possibles pour le déployment d'applications java est de le faire via une interface web, le manager Tomcat.
|
|
|
|
Installation :
|
|
|
|
~~~
|
|
# apt install tomcat7-admin || apt install tomcat8-admin
|
|
~~~
|
|
|
|
Il faut ensuite ajouter un utilisateur au rôle _manager_, dans le fichier tomcat-users.xml (équivalent d'un fichier htpasswd d'Apache) :
|
|
|
|
~~~
|
|
<tomcat-users>
|
|
[...]
|
|
<role rolename="manager"/>
|
|
<user username="jdoe" password="s3cret" roles="manager"/>
|
|
</tomcat-users>
|
|
~~~
|
|
|
|
Une fois Tomcat redémarré, l'accès au manager se fait sur l'URI : <http://example.com:8080/manager/html>
|