Tomcat 02 Installation Configuration Tomcat

Installation et configuration de Tomcat

1. Choix d’une installation

Apache Tomcat est lui-même un programme codé en Java. Il est mis à disposition sous forme de code source à compiler soi-même ou sous forme de binaire, en format brut ou sous forme de paquet DEB ou RPM. On peut aussi construire des paquets DEB ou RPM à partir des sources compilées soi-même.

On choisira la version de Tomcat et le type d’architecture matérielle (système d’exploitation 32 ou 64 bits).

On notera que Tomcat 9.0 nécessite Java SE 8 ou ultérieur.

La page de téléchargement de Tomcat 9.0 se trouve ici : Tomcat 9 Software Downloads.

2. Dépôts de paquetage

On se propose ici uniquement de prendre connaissances de la version disponible dans les dépôts de paquetage officiels Centos et Ubuntu.

Versions sous Centos

Vérification dans les paquets disponibles

yum info tomcat
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
 * base: centos.mirrors.ovh.net
 * epel: mirror.nl.leaseweb.net
 * extras: centos.mirror.ate.info
 * updates: centos.mirror.ate.info
Available Packages
Name        : tomcat
Arch        : noarch
Version     : 7.0.76
Release     : 9.el7_6
Size        : 91 k
Repo        : updates/7/x86_64
Summary     : Apache Servlet/JSP Engine, RI for Servlet 3.0/JSP 2.2 API
URL         : http://tomcat.apache.org/
License     : ASL 2.0
Description : Tomcat is the servlet container that is used in the official Reference
            : Implementation for the Java Servlet and JavaServer Pages technologies.
            : The Java Servlet and JavaServer Pages specifications are developed by
            : Sun under the Java Community Process.
            :
            : Tomcat is developed in an open and participatory environment and
            : released under the Apache Software License version 2.0. Tomcat is intended
            : to be a collaboration of the best-of-breed developers from around the world.

Fichiers liés au paquet

rpm -ql tomcat
/etc/logrotate.d/tomcat
/etc/sysconfig/tomcat
/etc/tomcat
/etc/tomcat/Catalina
/etc/tomcat/Catalina/localhost
/etc/tomcat/catalina.policy
/etc/tomcat/catalina.properties
/etc/tomcat/conf.d
/etc/tomcat/conf.d/README
/etc/tomcat/context.xml
/etc/tomcat/log4j.properties
/etc/tomcat/logging.properties
/etc/tomcat/server.xml
/etc/tomcat/tomcat-users.xml
/etc/tomcat/tomcat.conf
/etc/tomcat/web.xml
/usr/bin/tomcat-digest
/usr/bin/tomcat-tool-wrapper
/usr/lib/systemd/system/tomcat.service
/usr/lib/systemd/system/tomcat@.service
/usr/libexec/tomcat
/usr/libexec/tomcat/functions
/usr/libexec/tomcat/preamble
/usr/libexec/tomcat/server
/usr/sbin/tomcat
/usr/share/doc/tomcat-7.0.76
/usr/share/doc/tomcat-7.0.76/LICENSE
/usr/share/doc/tomcat-7.0.76/NOTICE
/usr/share/doc/tomcat-7.0.76/RELEASE-NOTES
/usr/share/tomcat
/usr/share/tomcat/bin/bootstrap.jar
/usr/share/tomcat/bin/catalina-tasks.xml
/usr/share/tomcat/conf
/usr/share/tomcat/lib
/usr/share/tomcat/logs
/usr/share/tomcat/temp
/usr/share/tomcat/webapps
/usr/share/tomcat/work
/var/cache/tomcat
/var/cache/tomcat/temp
/var/cache/tomcat/work
/var/lib/tomcat
/var/lib/tomcat/webapps
/var/lib/tomcats
/var/log/tomcat
/var/log/tomcat/catalina.out

Versions sous Ubuntu

apt search tomcat8
Sorting... Done
Full Text Search... Done
libtomcat8-embed-java/bionic-updates,bionic-security 8.5.30-1ubuntu1.4 all
  Apache Tomcat 8 - Servlet and JSP engine -- embed libraries

libtomcat8-java/bionic-updates,bionic-security 8.5.30-1ubuntu1.4 all
  Apache Tomcat 8 - Servlet and JSP engine -- core libraries

tomcat8/bionic-updates,bionic-security 8.5.30-1ubuntu1.4 all
  Apache Tomcat 8 - Servlet and JSP engine

tomcat8-admin/bionic-updates,bionic-security 8.5.30-1ubuntu1.4 all
  Apache Tomcat 8 - Servlet and JSP engine -- admin web applications

tomcat8-common/bionic-updates,bionic-security 8.5.30-1ubuntu1.4 all
  Apache Tomcat 8 - Servlet and JSP engine -- common files

tomcat8-docs/bionic-updates,bionic-security 8.5.30-1ubuntu1.4 all
  Apache Tomcat 8 - Servlet and JSP engine -- documentation

tomcat8-examples/bionic-updates,bionic-security 8.5.30-1ubuntu1.4 all
  Apache Tomcat 8 - Servlet and JSP engine -- example web applications

tomcat8-user/bionic-updates,bionic-security 8.5.30-1ubuntu1.4 all
  Apache Tomcat 8 - Servlet and JSP engine -- tools to create user instances

3. Installation Linux à partir d’un binaire

Installation d’OpenJDK 11

yum -y install java-11-openjdk-devel

Création d’un utilisateur système

sudo useradd -m -U -d /opt/tomcat -s /bin/false tomcat

Téléchargement de la dernière distribution de Tomcat

Tomcat 9 Software Downloads

La dernière version au moment de l’écriture de ces lignes est la suivante :

latest="9.0.24"

On peut toutefois tenter de la récupérer automatiquement :

latest=$(curl -s http://apache.mirrors.ovh.net/ftp.apache.org/dist/tomcat/tomcat-9/ | \
  grep 'v9\.' | \
  sed -e 's/<[^>]*>//g' | \
  cut -d " " -f1 | \
  sed -e 's/\///g' | \
  sed -e 's/v//g')
echo ${latest}

Quoi qu’il en soit, il sera nécessaire de télécharger et vérifier les binaires du logiciel.

yum -y install wget
wget https://www-eu.apache.org/dist/tomcat/tomcat-9/v${latest}/bin/apache-tomcat-${latest}.tar.gz
wget https://www.apache.org/dist/tomcat/tomcat-9/v${latest}/bin/apache-tomcat-${latest}.tar.gz.sha512
sha512sum -c apache-tomcat-${latest}.tar.gz.sha512

Emplacement des fichiers

tar xvfz apache-tomcat-${latest}.tar.gz
mv apache-tomcat-${latest} /opt/tomcat/
ln -s /opt/tomcat/apache-tomcat-* /opt/tomcat/latest
sudo chown -R tomcat: /opt/tomcat
chown -R tomcat: /opt/tomcat
chmod +x /opt/tomcat/latest/bin/*.sh

Fichier de service

Fichier de service : /etc/systemd/system/tomcat.service

cat << EOF > /etc/systemd/system/tomcat.service
[Unit]
Description=Tomcat 9 servlet container
After=network.target

[Service]
Type=forking

User=tomcat
Group=tomcat

Environment="JAVA_HOME=/usr/lib/jvm/jre"
Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom"

Environment="CATALINA_BASE=/opt/tomcat/latest"
Environment="CATALINA_HOME=/opt/tomcat/latest"
Environment="CATALINA_PID=/opt/tomcat/latest/temp/tomcat.pid"
Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"

ExecStart=/opt/tomcat/latest/bin/startup.sh
ExecStop=/opt/tomcat/latest/bin/shutdown.sh

[Install]
WantedBy=multi-user.target
EOF

Démarrage et activation du logiciel

Démarrage et activation du logiciel

systemctl daemon-reload
systemctl enable tomcat
systemctl start tomcat
systemctl status tomcat

Pare-feu

firewall-cmd --zone=public --add-port=8080/tcp --permanent
firewall-cmd --reload
firewall-cmd --permanent --zone=public --list-all

Configuration des accès au Manager

cat /opt/tomcat/latest/conf/tomcat-users.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.
-->
<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">
<!--
  NOTE:  By default, no user is included in the "manager-gui" role required
  to operate the "/manager/html" web application.  If you wish to use this app,
  you must define such a user - the username and password are arbitrary. It is
  strongly recommended that you do NOT use one of the users in the commented out
  section below since they are intended for use with the examples web
  application.
-->
<!--
  NOTE:  The sample user and role entries below are intended for use with the
  examples web application. They are wrapped in a comment and thus are ignored
  when reading this file. If you wish to configure these users for use with the
  examples web application, do not forget to remove the <!.. ..> that surrounds
  them. You will also need to set the passwords to something appropriate.
-->
<!--
  <role rolename="tomcat"/>
  <role rolename="role1"/>
  <user username="tomcat" password="<must-be-changed>" roles="tomcat"/>
  <user username="both" password="<must-be-changed>" roles="tomcat,role1"/>
  <user username="role1" password="<must-be-changed>" roles="role1"/>
-->
</tomcat-users>
mv /opt/tomcat/latest/conf/tomcat-users.xml /opt/tomcat/latest/conf/tomcat-users.xml.old
admin_password=$(openssl rand -base64 8)
cat << EOF > /opt/tomcat/latest/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="admin-gui"/>
   <role rolename="manager-gui"/>
   <user username="admin" password="${admin_password}" roles="admin-gui,manager-gui"/>
</tomcat-users>
EOF
tail /opt/tomcat/latest/webapps/manager/META-INF/context.xml
  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" >
  <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>
tail /opt/tomcat/latest/webapps/host-manager/META-INF/context.xml
  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" >
  <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"/>

4. Installation et compilation de Tomcat avec Ant

Building Tomcat

5. Tomcat dans un conteneur Docker

Installation de Docker

yum install -y yum-utils \
  device-mapper-persistent-data \
  lvm2
yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo

yum install docker-ce docker-ce-cli containerd.io
# gpasswd -a user docker
systemctl start docker
docker run hello-world

Exemples

https://github.com/docker-library/tomcat

https://github.com/ardydedase/docker-tomcat-war

Minimal Docker image with Apache Tomcat

Exemple de déploiement d’un projet Maven

Dockerfile

FROM jeanblanchard/java:8
MAINTAINER Jean Blanchard <jean@blanchard.io>

# Expose web port
EXPOSE 8080

# Tomcat Version
ENV TOMCAT_VERSION_MAJOR 9
ENV TOMCAT_VERSION_FULL  9.0.17

# Download and install
RUN apk add --update curl &&\
  curl -LO http://mirrors.standaloneinstaller.com/apache/tomcat/tomcat-${TOMCAT_VERSION_MAJOR}/v${TOMCAT_VERSION_FULL}/bin/apache-tomcat-${TOMCAT_VERSION_FULL}.tar.gz &&\
  curl -LO https://www.apache.org/dist/tomcat/tomcat-${TOMCAT_VERSION_MAJOR}/v${TOMCAT_VERSION_FULL}/bin/apache-tomcat-${TOMCAT_VERSION_FULL}.tar.gz.sha512 &&\
  sha512sum -c apache-tomcat-${TOMCAT_VERSION_FULL}.tar.gz.sha512 &&\
  gunzip -c apache-tomcat-${TOMCAT_VERSION_FULL}.tar.gz | tar -xf - -C /opt &&\
  rm -f apache-tomcat-${TOMCAT_VERSION_FULL}.tar.gz apache-tomcat-${TOMCAT_VERSION_FULL}.tar.gz.sha512 &&\
  ln -s /opt/apache-tomcat-${TOMCAT_VERSION_FULL} /opt/tomcat &&\
  rm -rf /opt/tomcat/webapps/examples /opt/tomcat/webapps/docs &&\
  apk del curl &&\
  rm -rf /var/cache/apk/*

# Configuration
ADD tomcat-users.xml /opt/tomcat/conf/
RUN sed -i 's/52428800/5242880000/g' /opt/tomcat/webapps/manager/WEB-INF/web.xml

# Set environment
ENV CATALINA_HOME /opt/tomcat

# Launch Tomcat on startup
CMD ${CATALINA_HOME}/bin/catalina.sh run

tomcat-users.xml

<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
  <role rolename="admin-gui" />
  <role rolename="admin-script" />
  <role rolename="manager-gui" />
  <role rolename="manager-status" />
  <role rolename="manager-script" />
  <role rolename="manager-jmx" />
  <user name="admin" password="admin"
        roles="admin-gui,admin-script,manager-gui,manager-status,manager-script,manager-jmx"/>
</tomcat-users>

6. Rôle Ansible Tomcat

ansible-tomcat