Software Development

Installing Tomcat in Windows Azure

There are few options already available to install Tomcat on Windows Azure, which involve running some scripts that create a package and definition file for you that you can deploy to Windows Azure. However, i personally feel that we have a much easier solution for installing Tomcat.

The solution that i am discussing here makes use of the startup tasks in elevated privileges, which were introduced in Azure SDK 1.3.

  1. Download and install jre on your local system. Zip the jre folder and upload it blob.
  2. Download tomcat on your local system.
  3. Edit tomcat’s server.xml to configure specific ports and enable SSL
  4. If you need to deploy any java .war file in your tomcat, then copy this war file in tomcat’s webapps folder.
  5. Zip the tomcat folder an upload it to blob.
  6. Now create a worker role and enable TCP ports configured for tomcat.
  7. Add startup task in this worker role that does the following tasks:
    • Unpack jre zip file to local drive on azure.
    • Unpack our customized tomcat zip file to local drive on azure.
    • Set environment variable for JRE_HOME to the path where JRE was unpacked.
    • Set environment variable for CATALINA_HOME to the path where tomcat was unpacked.
    • Start tomcat.

It shouldn’t be difficult for you to implement the above steps yourself, however, i seem to have plenty of time today so let me explain these steps too.

Prepare Java
It shouldn’t be difficult for you to download java from oracle/sun site. Just download it and install it. It will create a folder on your local machine e.g. jre6. You need to zip this folder and upload it to your blob storage. You can obviously upload it to any other host too, but since we are discussing about Azure, so let’s keep it that way only.

Prepare and configure Tomcat
Now download your desired version of tomcat from http://tomcat.apache.org/ and unzip this folder on your local machine.

  • Setup Ports in Server.xml
    You will have to select the ports you want your tomcat to run on. Say for http you want port 80 and for https you need port 443.

    Go to the conf folder inside your tomcat folder and open file server.xml

    Search for the following line and replace 8080 with 80 and 8443 with 443:

     8080” protocol=”HTTP/1.1″ connectionTimeout=”20000″ redirectPort=”8443” />

    After changing the ports this line will look like

    80″ protocol=”HTTP/1.1″ connectionTimeout=”20000″ redirectPort=”443” />

  • Enable SSL
    Again open the server.xml file and search for the following line:

    This will be commented by default. Uncomment it and change the port to 443. This line will look like as bellow:

    443” protocol=”HTTP/1.1″ SSLEnabled=”true” maxThreads=”150″ scheme=”https” secure=”true” clientAuth=”false” sslProtocol=”TLS”/>

    For SSL we use a .pfx file with tomcat instead of using normal keystore file. I will assume here that you already have .pfx file for PKCS12 certificate. This certificate needs to be added to your Cloud Service in the azure portal. Copy this .pfx file to a folder inside your tomcat folder, say under webapps. And make the following changes to the above line in server.xml:

    443” protocol=”HTTP/1.1″ SSLEnabled=”true” maxThreads=”150″ scheme=”https” secure=”true” clientAuth=”false” sslProtocol=”TLS” keystoreFile=”\webapps\myservice.cloudapp.net.pfx” keystorePass=”” keystoreType=”PKCS12″/>

  • Deploy war files if required
    If you have some war files that need to be deployed with tomcat, then jut copy them under the webapps folder of tomcat. Now when tomcat would be started it will install these applications.
  • Upload tomcat
    Once done zip your customised tomcat and upload it to blob.

Worker Role 
Now create a new Cloud Service Project in Visual Studio and add a new Worker role to it.

  • Add Certificate
    Upload the certificate that you used for tomcat’s SSL to the portal or include it in your worker project.   
  • Enable TCP Ports
    You need to enable the TCP ports in your worker role that you configured for your tomcat. In our case these are port 80 and 443.

    public override bool OnStart()
            {
                // Set the maximum number of concurrent connections
                ServicePointManager.DefaultConnectionLimit = 12;
                // For information on handling configuration changes
                // see the MSDN topic at http://go.microsoft.com/fwlink/?LinkId=166357.
                TcpListener port80Listener = new TcpListener(RoleEnvironment.CurrentRoleInstance.InstanceEndpoints[“Tcp80”].IPEndpoint);
                TcpListener sslListener = new TcpListener(RoleEnvironment.CurrentRoleInstance.InstanceEndpoints[“TcpSSL”].IPEndpoint);
                port80Listener.Start();
                sslListener.Start();
                return base.OnStart();
            }

  • Startup Tasks
    Then as we do for startup tasks create a startup.cmd file in your worker role, mark it’s Build Action Property as Content and Copy to Output Directory to Copy Always.

    Your startup.cmd file will have the following commands:

    • Download tomcat.zip and unzip
      We will use GetFiles.exe developed by us for downloading tomcat.zip. It is just a console app that takes first argument a blob url and second argument is the path to save file. Then using another customised utility ZipUtility.exe we will unzip tomcat.zip in the C: drive to a folder tomcat.

      start /w ZipUtility.exe C:\tomcat.zip C:\

    • Download jre.zip and unzip
      Similarly download and unzip jre.
      start /w ZipUtility.exe C:\jre6.zip C:\

    • Set Environment variables
      We need to setup two environment variables.
      JRE_HOME needs to be set to your jre folder, which in our case is C:\jre6
      SET JRE_HOME=C:\jre6

      CATALINA_HOME needs to be set to your tomcat folder, which in our case is C:\tomcat

      SET CATALINA_HOME=C:\tomcat

      Note: Please note that you can set environment variables in your service definition file also using the Runtime section as below:

      <Runtime>
            <Environment>
              <Variable name=CATALINA_HOME value=C:\tomcat/>
            Environment>
      Runtime>

    • Start tomcat
      C:\tomcat\bin\startup.bat

So startup.cmd will look like below:

GetFiles.exe and ZipUtility.exe are custom console apps. They are also added to the worker role project with their  Build Action Property as Content and Copy to Output Directory to Copy Always.

Final Step
That’s it just deploy your package to the cloud. Make sure your hosted service has the certificate that you used for tomcat’s SSL.