Table des matières

Version : 2023.01.

Dernière mise-à-jour : 2023/12/02 15:50

SER504 - Déploiement et Gestion des Applications

Contenu du Module

Déployer une application

Il existe plusieurs méthodes pour déployer une application sous Tomcat.

Déploiement Automatique

Dans ce cas, toute application dans le répertoire $CATALINA_HOME/webapps est automatiquement déployer si l'attribut autoDeploy est vrai dans l'élément <Host> du fichier $CATALINA_HOME/conf/server.xml.

L'Element Context

Le contexte d'une application, nécessaire pour son déploiement, est automatiquement créé quand une application est copiée dans le répertoire $CATALINA_HOME/webapps. Dans le cas où on souhaite déployer une application en dehors de ce répertoire, il convient d'utiliser l'élément <Context> dans le fichier $CATALINA_HOME/conf/server.xml.

L'élément prend la forme suivante :

<Context path=“/demo” docBase=“/un/autre/répertoire” />

Déploiement avec XML

Dans le cas précédent, le fichier $CATALINA_HOME/conf/server.xml ayant été modifié, il est nécessaire de re-démarrer le serveur.

Afin d'éviter ceci, l'élément <Context> peut être défini dans un fichier XML au nom de l'application. Ce fichier est à copier dans le répertoire $CATALINA_HOME/conf/Catalina/localhost.

L'élément prend la forme suivante :

<Context path=“/demo” docBase=“/un/autre/répertoire” />

Dans le cas où, l'attribut autoDeploy est vrai, l'application sera déployer automatiquement dès la détection de ce fichier.

Application Manager de Tomcat

L'Application Manager de Tomcat est utilisable à partir de trois interfaces :

Cet outil permet de :

Afin d'utiliser le Manager à partir d'un poste autre que celui du serveur lui-même, il est necéssaire d'éditer le fichier $CATALINA_HOME/webapps/manager/META-INF/context.xml en commentant la Valve :

[root@centos8 work]# vi $CATALINA_HOME/webapps/manager/META-INF/context.xml
[root@centos8 work]# cat $CATALINA_HOME/webapps/manager/META-INF/context.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<Context antiResourceLocking="false" privileged="true" >
  <CookieProcessor className="org.apache.tomcat.util.http.Rfc6265CookieProcessor"
                   sameSiteCookies="strict" />
  <!-- <Valve className="org.apache.catalina.valves.RemoteAddrValve"
         allow="127\.\d+\.\d+\.\d+|::1|0:0:0:0:0:0:0:1" /> -->
  <Manager sessionAttributeValueClassNameFilter="java\.lang\.(?:Boolean|Integer|Long|Number|String)|org\.apache\.catalina\.filters\.CsrfPreventionFilter\$LruCache(?:\$1)?|java\.util\.(?:Linked)?HashMap"/>
</Context>

Important : Notez que cette modification ne necéssite pas le redémarrage du serveur Tomcat.

L'interface Texte

La syntaxe des commandes est la suivante :

http://<hote>:<port>/manager/<commandes>?<paramètres>

Les commandes admises sont :

Voici des exemples des commandes les plus intéressantes :

list

Saisissez la commande suivante :

[root@centos8 work]# lynx --dump -auth=admin:fenestros http://www.ittraining.loc:8080/manager/text/list
OK - Listed applications for virtual host [localhost]
/:running:0:ROOT
/examples:running:0:examples
/host-manager:running:0:host-manager
/manager:running:0:manager
/docs:running:0:docs

Le format de chaque ligne est :

contexte:état:sessions:docBase

deploy

Saisissez maintenant la commande suivante :

[root@centos8 work]# lynx --dump -auth admin:fenestros "http://www.ittraining.loc:8080/manager/text/deploy?path=/sample&war=file:/usr/tomcat10/webapps/docs/appdev/sample/sample.war&update=true"
OK - Deployed application at context path [/sample]

Important : Notez l'utilisation de &update=true. Cette option spécifie à la commande deploy que si l'application existe déjà dans le serveur le manager doit d'abord la supprimer pour ensuite l'installer de nouveau.

Notez la création du répertoire $CATALINA_HOME/webapps/sample :

[root@centos8 work]# ls ../webapps/
docs  examples  host-manager  manager  ROOT  sample  sample.war

start

Saisissez maintenant la commande suivante :

[root@centos8 work]# lynx --dump -auth admin:fenestros "http://www.ittraining.loc:8080/manager/text/start?path=/sample"
OK - Started application at context path [/sample]

stop

Saisissez maintenant la commande suivante :

[root@centos8 work]# lynx --dump -auth admin:fenestros "http://www.ittraining.loc:8080/manager/text/stop?path=/sample"
OK - Stopped application at context path [/sample]

reload

Saisissez les commandes suivantes :

[root@centos8 work]# lynx --dump -auth admin:fenestros "http://www.ittraining.loc:8080/manager/text/start?path=/sample"
OK - Started application at context path [/sample]

[root@centos8 work]# lynx --dump -auth admin:fenestros "http://www.ittraining.loc:8080/manager/text/reload?path=/sample"
OK - Reloaded application at context path [/sample]

undeploy

Saisissez maintenant la commande suivante :

[root@centos8 work]# lynx --dump -auth admin:fenestros "http://www.ittraining.loc:8080/manager/text/undeploy?path=/sample"
OK - Undeployed application at context path [/sample]

Notez la suppression du répertoire $CATALINA_HOME/webapps/sample :

[root@centos8 work]# ls ../webapps/
docs  examples  host-manager  manager  ROOT

resources

Saisissez la commande suivante :

[root@centos8 work]# lynx --dump -auth admin:fenestros "http://www.ittraining.loc:8080/manager/text/resources"OK - Listed global resources of all types
UserDatabase:org.apache.catalina.users.MemoryUserDatabase

Notez qu'il est possible d'obtenir une sélection de ressources en spécifiant la syntaxe suivante :

http://<hote>:<port>/manager/resources?type=<type_JNDI>

serverinfo

Saisissez la commande suivante :

[root@centos8 work]# lynx --dump -auth admin:fenestros "http://www.ittraining.loc:8080/manager/text/serverinfo"
OK - Server info
Tomcat Version: [Apache Tomcat/10.0.27]
OS Name: [Linux]
OS Version: [4.18.0-305.7.1.el8_4.x86_64]
OS Architecture: [amd64]
JVM Version: [1.8.0_312-b07]
JVM Vendor: [Red Hat, Inc.]

L'interface HTML

Afin de pouvoir utiliser l'application manager en mode html, modifiez le fichier $CATALINA_HOME/conf/tomcat-users.xml ainsi :

[root@centos8 work]# vi $CATALINA_HOME/conf/tomcat-users.xml
[root@centos8 work]# cat $CATALINA_HOME/conf/tomcat-users.xml
<?xml version='1.0' encoding='utf-8'?>
<tomcat-users xmlns="http://tomcat.apache.org/xml"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd"
              version="1.0">
  <role rolename="tomcat"/>
  <role rolename="role1"/>
  <role rolename="manager-gui"/>
  <user username="tomcat" password="tomcat" roles="tomcat"/>
  <user username="both" password="tomcat" roles="tomcat,role1"/>
  <user username="role1" password="tomcat" roles="role1"/>
  <user username="admin" password="fenestros" roles="manager-gui"/>
</tomcat-users>

Re-démarrez le serveur Tomcat :

[root@centos8 work]# systemctl restart tomcat
[root@centos8 work]# systemctl status tomcat
● tomcat.service - Apache Tomcat Web Application Container
   Loaded: loaded (/etc/systemd/system/tomcat.service; disabled; vendor preset: disabled)
   Active: active (running) since Wed 2023-10-04 10:09:53 EDT; 6s ago
  Process: 75805 ExecStop=/bin/kill -15 $MAINPID (code=exited, status=0/SUCCESS)
  Process: 75813 ExecStart=/usr/tomcat10/bin/startup.sh (code=exited, status=0/SUCCESS)
 Main PID: 75825 (java)
    Tasks: 50 (limit: 100949)
   Memory: 327.6M
   CGroup: /system.slice/tomcat.service
           └─75825 /usr/lib/jvm/jre-1.8.0-openjdk-1.8.0.312.b07-2.el8_5.x86_64/bin/java -Djava.util.logging.c>

Oct 04 10:09:53 centos8.ittraining.loc systemd[1]: Starting Apache Tomcat Web Application Container...
Oct 04 10:09:53 centos8.ittraining.loc startup.sh[75813]: Existing PID file found during start.
Oct 04 10:09:53 centos8.ittraining.loc startup.sh[75813]: Removing/clearing stale PID file.
Oct 04 10:09:53 centos8.ittraining.loc startup.sh[75813]: Tomcat started.
Oct 04 10:09:53 centos8.ittraining.loc systemd[1]: Started Apache Tomcat Web Application Container.

Connectez-vous à votre machine virtuelle Gateway en mode graphique. Lancez ensuite le navigateur web Firefox dans la fenêtre de la VM et saisissez l'url http://10.0.3.45:8080/manager/html.

Dans la boîte d'authentification renseignez l'utilisateur admin et le mot de passe fenestros :

Vous obtiendrez l'interface web de gestion de Tomcat :

A faire : Explorez cette interface. Ensuite passez en revue chacun des applications jsp-examples et servlets-examples dans l'applications examples en affichant le résultat ainsi que le code.

L'interface ANT

ANT est un utilitaire permettant la gestion des applications par l'utilisation d'un script normalement appelé build.xml contenant un projet.

Commencez par l'installation d'ANT :

[root@centos8 work]# dnf install ant
Last metadata expiration check: 1:04:20 ago on Wed 04 Oct 2023 09:19:28 EDT.
Dependencies resolved.
==============================================================================================================
 Package                   Architecture  Version                                       Repository        Size
==============================================================================================================
Installing:
 ant                       noarch        1.10.5-1.module_el8.0.0+47+197dca37           appstream        193 k
Installing dependencies:
 ant-lib                   noarch        1.10.5-1.module_el8.0.0+47+197dca37           appstream        2.0 M
 javapackages-tools        noarch        5.3.0-1.module_el8.0.0+11+5b8c10bd            appstream         44 k
Enabling module streams:
 ant                                     1.10                                                                

Transaction Summary
==============================================================================================================
Install  3 Packages

Total download size: 2.2 M
Installed size: 2.7 M
Is this ok [y/N]: y

Définissez la variable CLASSPATH dans le fichier /etc/profile :

[root@centos8 work]# vi /etc/profile

[root@centos8 work]# cat /etc/profile
...
# Tomcat
CATALINA_HOME="/usr/tomcat10"
export CATALINA_HOME
PATH=$PATH:/usr/lib/jvm/jre-1.8.0-openjdk-1.8.0.312.b07-2.el8_5.x86_64/bin
JAVA_HOME=/usr/lib/jvm/jre-1.8.0-openjdk-1.8.0.312.b07-2.el8_5.x86_64
export PATH JAVA_HOME
# CLASSPATH
CLASSPATH="/usr/tomcat10/lib:/usr/share/java:/usr/share/ant/lib"

[root@centos8 work]# source /etc/profile

[root@centos8 work]# echo $CLASSPATH
/usr/tomcat10/lib:/usr/share/java:/usr/share/ant/lib

Donnez le rôle manager-script à l'utilisateur admin :

[root@centos8 work]# vi $CATALINA_HOME/conf/tomcat-users.xml
[root@centos8 work]# cat $CATALINA_HOME/conf/tomcat-users.xml
<?xml version='1.0' encoding='utf-8'?>
<tomcat-users xmlns="http://tomcat.apache.org/xml"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://tomcat.apache.org/xml tomcat-users.xsd"
              version="1.0">
  <role rolename="tomcat"/>
  <role rolename="role1"/>
  <role rolename="manager-script"/>
  <user username="tomcat" password="tomcat" roles="tomcat"/>
  <user username="both" password="tomcat" roles="tomcat,role1"/>
  <user username="role1" password="tomcat" roles="role1"/>
  <user username="admin" password="fenestros" roles="manager-script"/>
</tomcat-users>

Re-démarrez le serveur Tomcat pour une prise en compte de la modification :

[root@centos8 work]# systemctl restart tomcat
[root@centos8 work]# systemctl status tomcat
● tomcat.service - Apache Tomcat Web Application Container
   Loaded: loaded (/etc/systemd/system/tomcat.service; disabled; vendor preset: disabled)
   Active: active (running) since Wed 2023-10-04 10:27:55 EDT; 5s ago
  Process: 76612 ExecStop=/bin/kill -15 $MAINPID (code=exited, status=0/SUCCESS)
  Process: 76621 ExecStart=/usr/tomcat10/bin/startup.sh (code=exited, status=0/SUCCESS)
 Main PID: 76632 (java)
    Tasks: 50 (limit: 100949)
   Memory: 335.9M
   CGroup: /system.slice/tomcat.service
           └─76632 /usr/lib/jvm/jre-1.8.0-openjdk-1.8.0.312.b07-2.el8_5.x86_64/bin/java -Djava.util.logging.c>

Oct 04 10:27:55 centos8.ittraining.loc systemd[1]: Starting Apache Tomcat Web Application Container...
Oct 04 10:27:55 centos8.ittraining.loc startup.sh[76621]: Existing PID file found during start.
Oct 04 10:27:55 centos8.ittraining.loc startup.sh[76621]: Removing/clearing stale PID file.
Oct 04 10:27:55 centos8.ittraining.loc startup.sh[76621]: Tomcat started.
Oct 04 10:27:55 centos8.ittraining.loc systemd[1]: Started Apache Tomcat Web Application Container.

Créez le script build.xml dans le répertoire courant pour déployer l'application sample :

[root@centos8 work]# vi build.xml
[root@centos8 work]# cat build.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<project name="Tomcat" default="deployer" basedir=".">
<property name="manager.url" value="http://www.ittraining.loc:8080/manager/text" />
<property name="manager.user" value="admin" />
<property name="manager.password" value="fenestros" />
<property name="app.context" value="/sample" />
<property name="app.war" value="file:/usr/tomcat10/webapps/docs/appdev/sample/sample.war" />
<property name="appserver.home" value="/usr/tomcat10" />
<property name="appserver.lib" value="${appserver.home}/lib" />
<path id="catalina-ant-classpath">
   <fileset dir="${appserver.lib}">
      <include name="*.jar"/>
   </fileset>
</path>
<taskdef name="deploy" classname="org.apache.catalina.ant.DeployTask"> 
        <classpath refid="catalina-ant-classpath"/>
</taskdef>
<taskdef name="reload" classname="org.apache.catalina.ant.ReloadTask">
        <classpath refid="catalina-ant-classpath"/>
</taskdef>
<taskdef name="undeploy" classname="org.apache.catalina.ant.UndeployTask">
        <classpath refid="catalina-ant-classpath"/>
</taskdef>
<taskdef name="list" classname="org.apache.catalina.ant.ListTask">
        <classpath refid="catalina-ant-classpath"/>
</taskdef>
<taskdef name="start" classname="org.apache.catalina.ant.StartTask">
        <classpath refid="catalina-ant-classpath"/>
</taskdef>
<taskdef name="stop" classname="org.apache.catalina.ant.StopTask">
        <classpath refid="catalina-ant-classpath"/>
</taskdef>
<target name="deployer" description="Déploiement">
        <deploy url="${manager.url}" username="${manager.user}" password="${manager.password}" path="${app.context}" war="${app.war}" />
</target>
<target name="recharger" description="Rechargement">
        <reload url="${manager.url}" username="${manager.user}" password="${manager.password}" path="${app.context}" />
</target>
<target name="supprimer" description="Suppression">
        <undeploy url="${manager.url}" username="${manager.user}" password="${manager.password}" path="${app.context}" />
</target>
<target name="demarrer" description="Démarrage">
        <start url="${manager.url}" username="${manager.user}" password="${manager.password}" path="${app.context}" />
</target>
<target name="arreter" description="Arrêt">
        <stop url="${manager.url}" username="${manager.user}" password="${manager.password}" path="${app.context}" />
</target>
</project>

Pour tester le fichier /usr/tomcat10/work/build.xml, vérifiez que vous pouvez déployer l'application sample avec la commande ant deployer :

[root@centos7 work]# ant deployer
Buildfile: /usr/tomcat10/work/build.xml

deployer:
   [deploy] OK - Deployed application at context path [/sample]

BUILD SUCCESSFUL
Total time: 0 seconds

Deployer de Tomcat

L'outil Deployer n'est pas inclus avec les binaires de Tomcat. Pour cette raison il convient de le télécharger :

[root@centos8 work]# wget https://archive.apache.org/dist/tomcat/tomcat-10/v10.0.27/bin/apache-tomcat-10.0.27-deployer.tar.gz
--2023-10-04 10:47:15--  https://archive.apache.org/dist/tomcat/tomcat-10/v10.0.27/bin/apache-tomcat-10.0.27-deployer.tar.gz
Resolving archive.apache.org (archive.apache.org)... 65.108.204.189, 2a01:4f9:1a:a084::2
Connecting to archive.apache.org (archive.apache.org)|65.108.204.189|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2855833 (2.7M) [application/x-gzip]
Saving to: ‘apache-tomcat-10.0.27-deployer.tar.gz’

apache-tomcat-10.0.27-deployer.tar.gz                100%[====================================================================================================================>]   2.72M  3.40MB/s    in 0.8s    

2023-10-04 10:47:17 (3.40 MB/s) - ‘apache-tomcat-10.0.27-deployer.tar.gz’ saved [2855833/2855833]

Désarchivez le fichier apache-tomcat-10.0.27-deployer.tar.gz :

[root@centos8 work]# gunzip apache-tomcat-10.0.27-deployer.tar.gz 

[root@centos8 work]# tar xvf apache-tomcat-10.0.27-deployer.tar 
apache-tomcat-10.0.27-deployer/LICENSE
apache-tomcat-10.0.27-deployer/NOTICE
apache-tomcat-10.0.27-deployer/README.md
apache-tomcat-10.0.27-deployer/RELEASE-NOTES
apache-tomcat-10.0.27-deployer/images/
apache-tomcat-10.0.27-deployer/lib/
apache-tomcat-10.0.27-deployer/build.xml
apache-tomcat-10.0.27-deployer/deployer-howto.html
apache-tomcat-10.0.27-deployer/images/asf-logo.svg
apache-tomcat-10.0.27-deployer/images/docs-stylesheet.css
apache-tomcat-10.0.27-deployer/images/tomcat.png
apache-tomcat-10.0.27-deployer/lib/catalina-ant.jar
apache-tomcat-10.0.27-deployer/lib/catalina-deployer.jar
apache-tomcat-10.0.27-deployer/lib/el-api.jar
apache-tomcat-10.0.27-deployer/lib/jasper-el.jar
apache-tomcat-10.0.27-deployer/lib/jasper.jar
apache-tomcat-10.0.27-deployer/lib/jaspic-api.jar
apache-tomcat-10.0.27-deployer/lib/jsp-api.jar
apache-tomcat-10.0.27-deployer/lib/servlet-api.jar
apache-tomcat-10.0.27-deployer/lib/tomcat-coyote.jar
apache-tomcat-10.0.27-deployer/lib/tomcat-juli.jar
apache-tomcat-10.0.27-deployer/lib/tomcat-util-scan.jar
apache-tomcat-10.0.27-deployer/lib/tomcat-util.jar
apache-tomcat-10.0.27-deployer/lib/tomcat-websocket.jar
apache-tomcat-10.0.27-deployer/lib/websocket-api.jar

Cette application contient un fichier build.xml déjà partiellement configuré :

[root@centos8 work]# cat apache-tomcat-10.0.27-deployer/build.xml
<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<project name="Deployer" default="compile" basedir=".">

  <property file="deployer.properties"/>

  <!-- Configure the directory into which the web application is built -->
  <property name="build"    value="${basedir}/build"/>

  <!-- Configure the folder and context path for this application -->
  <property name="webapp"   value="myapp"/>
  <property name="path"     value="/myapp"/>

  <!-- Configure properties to access the Manager application -->
  <property name="url"      value="http://localhost:8080/manager/text"/>
  <property name="username" value="tomcat"/>
  <property name="password" value="tomcat"/>

  <property name="webapp.path"     value="${build}/webapp${path}"/>

  <path id="deployer.classpath">
    <fileset dir="${basedir}/lib">
      <include name="*.jar"/>
    </fileset>
  </path>

  <!-- Configure the custom Ant tasks for the Manager application -->
  <taskdef resource="org/apache/catalina/ant/catalina.tasks"
           classpathref="deployer.classpath"/>

  <!-- Executable Targets -->
  <target name="clean" description="Removes build directory">
    <delete dir="${build}" />
  </target>

  <target name="compile" description="Compile web application"
          depends="clean">

    <copy todir="${webapp.path}">
      <fileset dir="${webapp}" />
    </copy>

    <jasper validateXml="false"
             uriroot="${webapp.path}"
             webXmlFragment="${webapp.path}/WEB-INF/generated_web.xml"
             addWebXmlMappings="true"
             outputDir="${webapp.path}/WEB-INF/classes" />

    <validator path="${webapp.path}" />

    <mkdir dir="${webapp.path}/WEB-INF/classes"/>
    <mkdir dir="${webapp.path}/WEB-INF/lib"/>

    <javac destdir="${webapp.path}/WEB-INF/classes"
           debug="${compile.debug}"
           deprecation="${compile.deprecation}"
           failonerror="false"
           srcdir="${webapp.path}/WEB-INF/classes"
           encoding="UTF-8"
           excludes="**/*.smap">
      <classpath>
        <fileset dir="${webapp.path}/WEB-INF/lib">
          <include name="*.jar"/>
        </fileset>
        <fileset dir="${basedir}/lib">
          <include name="*.jar"/>
        </fileset>
      </classpath>
      <include name="**" />
      <exclude name="tags/**" />
    </javac>

    <jar destfile="${webapp.path}.war"
         basedir="${webapp.path}" />

  </target>

  <target name="deploy" description="Deploy web application">
    <deploy url="${url}" username="${username}" password="${password}"
            path="${path}" war="${webapp.path}.war" update="true" />
  </target>

  <target name="undeploy" description="Undeploy web application">
    <undeploy url="${url}" username="${username}" password="${password}"
              path="${path}"/>
  </target>

  <!-- Webapp lifecycle control -->
  <target name="start" description="Start web application">
    <start url="${url}" username="${username}" password="${password}"
           path="${path}"/>
  </target>
  <target name="reload" description="Reload web application">
    <reload url="${url}" username="${username}" password="${password}"
            path="${path}"/>
  </target>
  <target name="stop" description="Stop web application">
    <stop url="${url}" username="${username}" password="${password}"
          path="${path}"/>
  </target>

</project>

A faire : Pour personnaliser ce script, il faut créer le fichier deployer.properties au même endroit que le script lui-même. Prenez 30 minutes pour chercher sur Internet comment configurer et utiliser le Deployer.


Copyright © 2023 Hugh Norris.