Tuesday, October 12, 2021

Customize Tomcat configuration in Linux App Service

When running Tomcat applications in Linux App Service using built-in Java Tomcat docker image, sometime you may need to customize your Tomcat configuration.

 

In this blog, we will provide detailed steps of how to modify the default server.xml file in the built-in docker container.

 

Before trying to overwrite the existing server.xml, please make sure whether it's possible to make your configuration changes in context.xml or web.xml.

  1. server.xml: This file includes general configuration of Tomcat server. For each Tomcat server, we can only find one $CATALINA_BASE/conf/server.xml file.
  2. context.xml: This file contains server-dependent configuration like DataSources provided by Tomcat server.
    • This file can be stored in the $CATALINA_BASE/conf/context.xml: the Context element information will be loaded by all web applications.
    • It can also be stored in $webapp_base/META-INF/context.xml:  the Context element information will be loaded by the specific web application.
  3. web.xml: This file contains server-independent configuration like servlet mappings.
    • This file can be stored in the $CATALINA_BASE/conf/web.xml, which may be configured for use with all web applications.
    • It can also be stored in $webapp_base/WEB-INF/web.xml, which may be configured for individual web applications.

Reference doc:
https://tomcat.apache.org/tomcat-9.0-doc/config/server.html
https://tomcat.apache.org/tomcat-9.0-doc/config/context.html
https://tomcat.apache.org/tomcat-9.0-doc/config/filter.html

 

There are two options that you can use to modify the built-in server.xml file in App Service built-in Java Tomcat docker image.

  1. Completely overwrite the built-in server.xml file with you own server.xml.
    This method is simple and straightforward. But if App Service platform upgrades in the future, there will be potential risks that your own server.xml may encounter compatibility issue with our platform.

  2. Use xsl transform to modify the built-in server.xml with necessary and minimum changes.
    This method is a little bit complex, but it can minimize the potential compatibility issue during App Service platform upgrades.

 

Completely overwrite the built-in server.xml file with you own server.xml

1. Go to WEBSSH, https://<webapp-name>.scm.azurewebsites.net/webssh/host

2. Copy the built-in server.xml file to any place under /home

 

cp /usr/local/tomcat/conf/server.xml /home/server.xml

 

 

3. Directly make change to /home/server.xml file

 

vi /home/server.xml

 

 

4. Create your customer startup script in any place under /home

 

vi /home/startup.sh

 

 

The contents of the startup.sh file should include the followings:

 

#!/bin/bash
echo "Using customer server.xml to overwrite /usr/local/tomcat/conf/server.xml"
cp /home/server.xml /usr/local/tomcat/conf/server.xml
echo "server.xml modified"

 

 

5. Go to Configuration portal to set "Startup Command", point it to your /home/startup.sh

Hanli_Ren_0-1633510197844.png

 

 

Use xsl transform to modify the built-in server.xml with necessary and minimum changes

1. Go to WEBSSH, https://<webapp-name>.scm.azurewebsites.net/webssh/host

 

2. Use cat command to get the contents of the built-in server.xml file

 

cat /usr/local/tomcat/conf/server.xml

 

Copy the file contents into your Notepad on your local machine. Later you can put the server.xml content into online XSL test tool (e.g. https://www.freeformatter.com/xsl-transformer.html) to test your XSL transform file.

 

3. Write xsl transform file, which can be used to transfer the server.xml contents

For example: I can use the following xsl file to modify my Tomcat access log format (add original client IP address in the log file). 

    

Original XML contents:

Hanli_Ren_0-1633510764001.png

 

Sample transform.xsl file:

 

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	
	    <xsl:template match="node()|@*">
	        <xsl:copy>
	            <xsl:apply-templates select="node()|@*"/>
	        </xsl:copy>
	    </xsl:template>
	
	    <xsl:template match="/Server/Service/Engine/Host/Valve/@pattern">
	        <xsl:attribute name="pattern">
	            <xsl:value-of select="'%h %l %u %t &quot;%r&quot; %s %b %D %{x-forwarded-for}i %{x-arr-log-id}i'"/>
	        </xsl:attribute>
	    </xsl:template>
	
</xsl:stylesheet>

 

 

Modified XML contents:

Hanli_Ren_2-1633511127804.png

 

Note:

4.  Upload your transform.xsl file to anywhere under /home folder.

Go to https://<webapp-name>.scm.azurewebsites.net/newui/fileManager

Hanli_Ren_3-1633511247662.png

 

5. Create your customer startup script anywhere under /home (e.g. /home/startup.sh)

 

vi /home/startup.sh

 

The contents of the startup.sh file should include the followings:

 

#!/bin/bash
echo "Install libxslt";
apk add --update libxslt; 
	
echo "Using /home/transform.xsl to modify /usr/local/tomcat/conf/server.xml"
xsltproc --output /usr/local/tomcat/conf/server.xml /home/transform.xsl /usr/local/tomcat/conf/server.xml; 
	
echo "server.xml modified"

 

 

6. Go to Configuration portal to set "Startup Command", point it to your /home/startup.sh

Hanli_Ren_0-1633510197844.png

 

Posted at https://sl.advdat.com/3iSON3M