Quickly deploy a Play application to Azure websites

Published 1/12/2016 6:48:00 AM
Filed under Web development

Coming from .NET I've always liked Azure for its simplicity. I takes no more
than 5 minutes to run a ASP.NET web application on Azure.

I figured: Why isn't that possible for Java too? Turns out it is possible
and not that hard to get up and running. And although I tried it with a Play
application most of the steps are the same for other kinds of Java application.

So let's get started!

Getting the bits ready for deployment

To deploy a Java application in Azure websites you actually need a application
to deploy. I choose to build a web application based on
Play with Scala.

If you have another kind of application that you want to run, go ahead grab it.
As long as it generates a war file you're good to go.

For my Play application I had to add a few things to the build file.
First I needed to install the Play2War plugin. This plugin automatically
generates a war for you when you run activator war or sbt war.

To use the plugin include it in the plugins.sbt file inside the project/ folder
of your project. Like so:

addSbtPlugin("com.github.play2war" % "play2-war-plugin" % "1.4-beta1")

Make sure to check the github repo for information about which version to use.
Various versions of Play need different versions of the plugin.

With the plugin in place, modify the build.sbt file to include a few settings
for the plugin:

import com.github.play2war.plugin._

name := "frontend"

version := "1.0"

lazy val `frontend` = (project in file(".")).enablePlugins(PlayScala)
  .settings(Play2WarPlugin.play2WarSettings: _*)
  .settings(Play2WarKeys.servletVersion := "3.1")

Add the import for the plugin at the top and include the settings
as part of the project definition.

You're now ready to deploy the web app to Azure. Run the following command to
obtain the war file:

sbt war

Create a new Web App in Azure

Now that you have something to deploy, let's create a new web app in azure.
Navigate to the Azure Portal and create a new Web App.

Give the Web App a proper name and configure it to run in a datacentre that is
near you (nothing worse than a lot of latency!).

Azure Web App Configuration

After you create the Web App you need to change the configuration so it runs
Java. Open up the details for the new Web App and click on the All settings
link. Now click on the Application Settings item and set the Java version to Java 8.
Next set the minor Java version to newest and choose Tomcat 8.

Java configuration

There are a ton of web containers to choose from, I choose Tomcat simply because
I used it quite extensively in the past and remember it to be a stable web container.

Feel the need to use something else? Go ahead, some web apps run better on Jetty
or JBoss, but for Play applications Tomcat works great.

Be sure to save the settings and wait for Tomcat to spin up.

Deploy the web application

With the application all set up it's time to deploy the website.
There are quite a few options to choose from here and they are all up to your
personal taste.

You can deploy manually using FTP. In this case you upload your war file to
site/wwwroot/webapps/ on the server. The web container will automatically pickup
the war and deploy it in the container.

Notice Java deployments aren't instant, so give it a minute before going
to the website to view the application.

An alternative to FTP is the use of a GIT repository. Open up the web app
configuration on the Azure portal, click the all settings link and select
the option Continuous deployment. Set the deployment method to GIT and save the

Clone the repository to your local harddrive. You can find the URL for the repository
under Properties in the settings panel.

Add the war file to the GIT repository in the webapps folder,
commit the changes and push them to Azure.

Once you've pushed the changes to Azure it will pick them up and deploy your war file
to the Web App automatically. As with any deployment of a war file, give it a minute
so that the web container has time to start the application.

The final result

If you are planning on running your web application as the root of the web container,
make sure that you name the file ROOT.war, otherwise it will end up in a subfolder.

Final thoughts and a few tips

Deploying a Java or Scala web application to Azure is not as hard as you might think.
The deployment is actually pretty similar to that of a regular .NET application.

I did notice a few small glitches. Sometimes the web container doesn't pick up the
war file correctly. You usually see the old website after a deployment, a failed deployment or in my case a 404 error. You can resolve this issue by restarting the web app in the management portal. After that it will pick up the new app deployment correctly.

Another thing that is quite important: Give the server some time. Starting Tomcat
is really slow. It will take about a minute to get the server up and running.
Don't be alarmed by any timeout errors you may see upon the first request. Just
refresh the page and things will run smooth after that.

I haven't tested the performance of my Play application on Azure, but so far I
haven't seen any performance issues. All requests are served well within 200ms.

So there you have it, a Java web application deployed on Azure without
spinning up a Virtual Machine!