Version : 2023.01.

Dernière mise-à-jour : 2023/12/04 09:19

SER304 - Déploiement et Gestion des Applications

Contenu du Module

  • SER304 - Déploiement et Gestion des Applications
    • Contenu du Module
    • Déployer une application
    • Déploiement Automatique
    • L’Élément Context
    • Déploiement avec XML
    • Application Manager de Tomcat
      • L'interface Texte
        • list
        • deploy
        • start
        • stop
        • reload
        • undeploy
        • resources
        • serverinfo
      • L'interface HTML
      • L'interface ANT
    • Deployer de Tomcat

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 :

  • l'interface texte,
  • l'interface HTML,
  • l'interface ANT.

Cet outil permet de :

  • déployer une application,
  • obtenir la liste des applications actives,
  • recharger une application,
  • obtenir d'informations sur les ressources JNDI,
  • démarrer une application,
  • arrêter une application,
  • supprimer un application.

L'interface Texte

La syntaxe des commandes est la suivante :

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

Les commandes admises sont :

  • list,
  • deploy,
  • start,
  • stop,
  • install,
  • remove,
  • undeploy,
  • reload,
  • serverinfo,
  • roles,
  • sessions,
  • resources,
  • status,
  • jmxproxy.

Voici des exemples des commandes les plus intéressantes :

list

Saisissez la commande suivante :

[root@centos7 bin]# lynx --dump -auth=admin:fenestros http://www.i2tch.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@centos7 bin]# lynx --dump -auth admin:fenestros "http://www.i2tch.loc:8080/manager/text/deploy?path=/sample&war=file:/usr/tomcat8/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@centos7 bin]# ls ../webapps/
docs  examples  host-manager  manager  ROOT  sample  sample.war

start

Saisissez maintenant la commande suivante :

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

stop

Saisissez maintenant la commande suivante :

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

reload

Saisissez les commandes suivantes :

[root@centos7 bin]# lynx --dump -auth admin:fenestros "http://www.i2tch.loc:8080/manager/text/start?path=/sample"
OK - Started application at context path /sample
[root@centos7 bin]#
[root@centos7 bin]# lynx --dump -auth admin:fenestros "http://www.i2tch.loc:8080/manager/text/reload?path=/sample"
OK - Reloaded application at context path /sample

undeploy

Saisissez maintenant la commande suivante :

[root@centos7 bin]# lynx --dump -auth admin:fenestros "http://www.i2tch.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@centos7 bin]# ls ../webapps/
docs  examples  host-manager  manager  ROOT

resources

Saisissez la commande suivante :

[root@centos7 bin]# lynx --dump -auth admin:fenestros "http://www.i2tch.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@centos7 bin]# lynx --dump -auth admin:fenestros "http://www.i2tch.loc:8080/manager/text/serverinfo"
OK - Server info
Tomcat Version: Apache Tomcat/8.0.36
OS Name: Linux
OS Version: 3.10.0-1062.4.1.el7.x86_64
OS Architecture: amd64
JVM Version: 1.8.0_232-b09
JVM Vendor: Oracle Corporation

L'interface HTML

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

<?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>

Arrêtez puis démarrez le serveur Tomcat :

[root@centos7 bin]# ./shutdown.sh
Using CATALINA_BASE:   /usr/tomcat8
Using CATALINA_HOME:   /usr/tomcat8
Using CATALINA_TMPDIR: /usr/tomcat8/temp
Using JRE_HOME:        /usr/lib/jvm/jre-1.8.0-openjdk-1.8.0.232.b09-0.el7_7.x86_64
Using CLASSPATH:       /usr/tomcat8/bin/bootstrap.jar:/usr/tomcat8/bin/tomcat-juli.jar
[root@centos7 bin]# ./startup.sh
Using CATALINA_BASE:   /usr/tomcat8
Using CATALINA_HOME:   /usr/tomcat8
Using CATALINA_TMPDIR: /usr/tomcat8/temp
Using JRE_HOME:        /usr/lib/jvm/jre-1.8.0-openjdk-1.8.0.232.b09-0.el7_7.x86_64
Using CLASSPATH:       /usr/tomcat8/bin/bootstrap.jar:/usr/tomcat8/bin/tomcat-juli.jar
Tomcat started.

Si vous vous êtes connecté à votre machine virtuelle via ssh, passez la VM CentOS 7 en démarrage en mode graphique :

[root@centos7 bin]# ls -l /etc/systemd/system/default.target
lrwxrwxrwx. 1 root root 37 Apr 30  2016 /etc/systemd/system/default.target -> /lib/systemd/system/multi-user.target
[root@centos7 bin]# rm -rf /etc/systemd/system/default.target
[root@centos7 bin]# ln -s /lib/systemd/system/graphical.target /etc/systemd/system/default.target
[root@centos7 bin]# ls -l /etc/systemd/system/default.target
lrwxrwxrwx. 1 root root 36 Oct 29 13:27 /etc/systemd/system/default.target -> /lib/systemd/system/graphical.target
[root@centos7 bin]# shutdown -h now

Une fois la VM arrêtée, augmentez la mémoire qui lui est allouée à au moins 2Go (plus si votre machine hôte le permet), puis démarrez votre VM.

Connectez-vous à votre machine virtuelle et démarrez Tomcat via ssh et démarrez Tomcat :

[root@centos7 bin]# ./startup.sh
Using CATALINA_BASE:   /usr/tomcat8
Using CATALINA_HOME:   /usr/tomcat8
Using CATALINA_TMPDIR: /usr/tomcat8/temp
Using JRE_HOME:        /usr/lib/jvm/jre-1.8.0-openjdk-1.8.0.232.b09-0.el7_7.x86_64
Using CLASSPATH:       /usr/tomcat8/bin/bootstrap.jar:/usr/tomcat8/bin/tomcat-juli.jar
Tomcat started.

Lancez ensuite le navigateur web Firefox dans la fenêtre de la VM et saisissez l'url http://www.i2tch.loc:8080/manager/html.

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

Validez. Vous obtiendrez l'interface web de gestion de Tomcat.

Explorez cette interface. Ensuite passez en revue chacun des exemples dans les applications jsp-examples et servlets-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@centos7 bin]# yum install ant

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

[root@centos7 bin]# vi /etc/profile
[root@centos7 bin]# cat /etc/profile
...
unset -f pathmunge
# Tomcat
CATALINA_HOME="/usr/tomcat8"
export CATALINA_HOME
PATH=$PATH:/usr/lib/jvm/jre-1.8.0-openjdk-1.8.0.232.b09-0.el7_7.x86_64/bin
JAVA_HOME=/usr/lib/jvm/jre-1.8.0-openjdk-1.8.0.232.b09-0.el7_7.x86_64
export PATH JAVA_HOME
# CLASSPATH
CLASSPATH="/usr/tomcat8/lib:/usr/share/java:/usr/share/ant/lib"
[root@centos7 bin]#
[root@centos7 bin]# source /etc/profile
[root@centos7 bin]#
[root@centos7 bin]# echo $CLASSPATH
/usr/tomcat8/lib:/usr/share/java:/usr/share/ant/lib

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

[root@centos7 bin]# vi $CATALINA_HOME/conf/tomcat-users.xml
[root@centos7 bin]# 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@centos7 bin]# ./shutdown.sh
Using CATALINA_BASE:   /usr/tomcat8
Using CATALINA_HOME:   /usr/tomcat8
Using CATALINA_TMPDIR: /usr/tomcat8/temp
Using JRE_HOME:        /usr/lib/jvm/jre-1.8.0-openjdk-1.8.0.232.b09-0.el7_7.x86_64
Using CLASSPATH:       /usr/tomcat8/bin/bootstrap.jar:/usr/tomcat8/bin/tomcat-juli.jar
[root@centos7 bin]# ./startup.sh
Using CATALINA_BASE:   /usr/tomcat8
Using CATALINA_HOME:   /usr/tomcat8
Using CATALINA_TMPDIR: /usr/tomcat8/temp
Using JRE_HOME:        /usr/lib/jvm/jre-1.8.0-openjdk-1.8.0.232.b09-0.el7_7.x86_64
Using CLASSPATH:       /usr/tomcat8/bin/bootstrap.jar:/usr/tomcat8/bin/tomcat-juli.jar
Tomcat started.

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

[root@centos7 bin]# vi build.xml
[root@centos7 bin]# cat build.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<project name="Tomcat" default="deployer" basedir=".">
<property name="manager.url" value="http://www.i2tch.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/tomcat8/webapps/docs/appdev/sample/sample.war" />
<property name="appserver.home" value="/usr/tomcat8" />
<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/tomcat8/bin/build.xml, vérifiez que vous pouvez déployer l'application sample avec la commande ant deployer :

[root@centos7 bin]# ant deployer
Buildfile: /usr/tomcat8/bin/build.xml

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

BUILD SUCCESSFUL
Total time: 3 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@centos7 ~]# wget https://archive.apache.org/dist/tomcat/tomcat-8/v8.0.36/bin/apache-tomcat-8.0.36-deployer.tar.gz

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

[root@centos7 ~]# tar xvf apache-tomcat-8.0.36-deployer.tar.gz 
apache-tomcat-8.0.36-deployer/LICENSE
apache-tomcat-8.0.36-deployer/NOTICE
apache-tomcat-8.0.36-deployer/RELEASE-NOTES
apache-tomcat-8.0.36-deployer/images/
apache-tomcat-8.0.36-deployer/lib/
apache-tomcat-8.0.36-deployer/build.xml
apache-tomcat-8.0.36-deployer/deployer-howto.html
apache-tomcat-8.0.36-deployer/images/asf-logo.gif
apache-tomcat-8.0.36-deployer/images/tomcat.gif
apache-tomcat-8.0.36-deployer/lib/catalina-ant.jar
apache-tomcat-8.0.36-deployer/lib/catalina-deployer.jar
apache-tomcat-8.0.36-deployer/lib/el-api.jar
apache-tomcat-8.0.36-deployer/lib/jasper-el.jar
apache-tomcat-8.0.36-deployer/lib/jasper.jar
apache-tomcat-8.0.36-deployer/lib/jsp-api.jar
apache-tomcat-8.0.36-deployer/lib/servlet-api.jar
apache-tomcat-8.0.36-deployer/lib/tomcat-coyote.jar
apache-tomcat-8.0.36-deployer/lib/tomcat-juli.jar
apache-tomcat-8.0.36-deployer/lib/tomcat-util-scan.jar
apache-tomcat-8.0.36-deployer/lib/tomcat-util.jar
apache-tomcat-8.0.36-deployer/lib/tomcat-websocket.jar
apache-tomcat-8.0.36-deployer/lib/websocket-api.jar

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

[root@centos7 ~]# cat apache-tomcat-8.0.36-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"
           optimize="off"
           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>

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.

Menu