163 lines
4.5 KiB
Markdown
163 lines
4.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.
|
|
|
|
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.
|
|
|
|
~~~
|
|
# apt install tomcat7 tomcat7-user libpam-systemd
|
|
~~~
|
|
|
|
### Unité systemd utilisateur
|
|
|
|
Créer l'unité tomcat dans /etc/systemd/user/tomcat7.service.
|
|
|
|
~~~{.ini}
|
|
[Unit]
|
|
Description=Tomcat7 %u.
|
|
After=network.target
|
|
|
|
[Service]
|
|
WorkingDirectory=%h
|
|
Environment="CATALINA_BASE=%h"
|
|
EnvironmentFile=%h/conf/env
|
|
UMask=0002
|
|
ExecStart=/usr/share/tomcat7/bin/startup.sh
|
|
ExecStop=/usr/share/tomcat7/bin/shutdown.sh
|
|
Type=forking
|
|
|
|
[Install]
|
|
WantedBy=default.target
|
|
~~~
|
|
|
|
## Création d'une instance
|
|
|
|
Créer un utilisateur dédié a l'instance et créer son arborescence via tomcat7-instance-create.
|
|
|
|
~~~
|
|
# 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
|
|
~~~
|
|
|
|
### 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
|
|
|
|
Activez 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
|
|
|
|
Enfin, l'utilisateur pour activer l'instance au démarrage et la démarrer, voir le status, …
|
|
|
|
~~~
|
|
$ systemctl --user enable tomcat
|
|
$ systemctl --user start tomcat
|
|
$ systemctl --user status -l tomcat
|
|
$ systemctl --user restart tomcat
|
|
$ systemctl --user stop 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
|
|
~~~
|
|
|
|
Il faut ensuite ajouter un utilisateur au role _manager_, dans le fichier _/etc/tomcat6/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 ce fait sur l'URI : <http://example.com:8080/manager/html>
|