Preview: Create a Java web app in App Service on Linux

App Service on Linux currently provides a preview feature to support Java web apps. Please review the Supplemental Terms of Use for Microsoft Azure Previews for more information on previews. Deploying Java web apps to a Linux container in the cloud using the Azure Toolkit for IntelliJ is an alternative approach to deploy your Java app to your own container.

Note

This article deploys a Java web app to App Service on Linux.

App Service on Linux provides a highly scalable, self-patching web hosting service using the Linux operating system. This quickstart shows how to deploy a Java app to App Service on Linux using a built-in image. You create the web app with built-in image using the Azure CLI, and you deploy the Java app to the web app.

Sample app running in Azure

If you don't have an Azure subscription, create a free account before you begin.

Prerequisites

To complete this quickstart:

Open Azure Cloud Shell

Azure Cloud Shell is a free, interactive shell that you can use to run the steps in this article. Common Azure tools are preinstalled and configured in Cloud Shell for you to use with your account. Just select the Copy button to copy the code, paste it in Cloud Shell, and then press Enter to run it. There are a few ways to open Cloud Shell:

Select Try It in the upper-right corner of a code block. Cloud Shell in this article
Open Cloud Shell in your browser. https://shell.azure.com/bash
Select the Cloud Shell button on the menu in the upper-right corner of the Azure portal. Cloud Shell in the portal

Create a deployment user

In the Cloud Shell, create deployment credentials with the az webapp deployment user set command. This deployment user is required for FTP and local Git deployment to a web app. The user name and password are account level. They are different from your Azure subscription credentials.

In the following example, replace <username> and <password> (including brackets) with a new user name and password. The user name must be unique within Azure. The password must be at least eight characters long, with two of the following three elements: letters, numbers, symbols.

az webapp deployment user set --user-name <username> --password <password>

You should get a JSON output, with the password shown as null. If you get a 'Conflict'. Details: 409 error, change the username. If you get a 'Bad Request'. Details: 400 error, use a stronger password.

You create this deployment user only once; you can use it for all your Azure deployments.

Note

Record the user name and password. You need them to deploy the web app later.

Create a resource group

A resource group is a logical container into which Azure resources like web apps, databases, and storage accounts are deployed and managed. For example, you can choose to delete the entire resource group in one simple step later.

In the Cloud Shell, create a resource group with the az group create command. The following example creates a resource group named myResourceGroup in the West Europe location. To see all supported locations for App Service on Linux in Standard tier, run the az appservice list-locations --sku S1 --linux-workers-enabled command.

az group create --name myResourceGroup --location "West Europe"

You generally create your resource group and the resources in a region near you.

When the command finishes, a JSON output shows you the resource group properties.

Create an Azure App Service plan

In the Cloud Shell, create an App Service plan in the resource group with the az appservice plan create command.

The following example creates an App Service plan named myAppServicePlan in the Standard pricing tier (--sku S1) and in a Linux container (--is-linux).

az appservice plan create --name myAppServicePlan --resource-group myResourceGroup --sku S1 --is-linux

When the App Service plan has been created, the Azure CLI shows information similar to the following example:

{ 
  "adminSiteName": null,
  "appServicePlanName": "myAppServicePlan",
  "geoRegion": "West Europe",
  "hostingEnvironmentProfile": null,
  "id": "/subscriptions/0000-0000/resourceGroups/myResourceGroup/providers/Microsoft.Web/serverfarms/myAppServicePlan",
  "kind": "linux",
  "location": "West Europe",
  "maximumNumberOfWorkers": 1,
  "name": "myAppServicePlan",
  < JSON data removed for brevity. >
  "targetWorkerSizeId": 0,
  "type": "Microsoft.Web/serverfarms",
  "workerTierName": null
} 

Create a web app

In the Cloud Shell, create a web app in the myAppServicePlan App Service plan. You can do it by using the az webapp create command. In the following example, replace <app_name> with a globally unique app name (valid characters are a-z, 0-9, and -).

# Bash
az webapp create --name <app_name> --resource-group myResourceGroup --plan myAppServicePlan --runtime "TOMCAT|8.5-jre8"
# PowerShell
az --% webapp create --name <app_name> --resource-group myResourceGroup --plan myAppServicePlan --runtime "TOMCAT|8.5-jre8"

For the runtime parameter, use one of the following runtimes:

  • TOMCAT|8.5-jre8
  • TOMCAT|9.0-jre8

When the web app has been created, the Azure CLI shows information similar to the following example:

{
  "additionalProperties": {},
  "availabilityState": "Normal",
  "clientAffinityEnabled": true,
  "clientCertEnabled": false,
  "cloningInfo": null,
  "containerSize": 0,
  "dailyMemoryTimeQuota": 0,
  "defaultHostName": "<your web app name>.azurewebsites.net",
  "enabled": true,
  "enabledHostNames": [
    "<your web app name>.azurewebsites.net",
    "<your web app name>.scm.azurewebsites.net"
  ],
  "ftpPublishingUrl": "ftp://<your ftp URL>",  
  < JSON data removed for brevity. >
}

Copy the value for ftpPublishingUrl. You will use this later, if you choose FTP deployment.

Browse to the newly created web app.

http://<app_name>.azurewebsites.net

If the web app is up and running, you should get a default screen similar to the following image:

Browse to Web App Before Deployment

Download the sample Java app

In a terminal window on your machine, run the following command to clone the sample app repository to your local machine. You will deploy this sample app in a later step.

git clone https://github.com/Azure-Samples/java-docs-hello-world

Deploying the Java app to App Service on Linux

Open the sample project in Eclipse, and export the java app to a Web Archive (WAR) file named helloworld.war.

To deploy your Java app WAR file, you can use WarDeploy (currently in Preview), or FTP.

Depending on which method of deployment you use, the relative path to browse to your Java web app will be slightly different.

Deploy with WarDeploy

To deploy your WAR file with WarDeploy, use the following cURL example commandline to send a POST request to https://.scm.azurewebsites.net/api/wardeploy. The POST request must contain the .war file in the message body. The deployment credentials for your app are provided in the request by using HTTP BASIC authentication. For more information on WarDeploy, see Deploy your app to Azure App Service with a ZIP or WAR file.

curl -X POST -u <username> --data-binary @"<war_file_path>" https://<app_name>.scm.azurewebsites.net/api/wardeploy

Update the following:

  • username - Use the deployment credential username you created earlier.
  • war_file_path - Use the local WAR file path.
  • app_name - Use the app name your created earlier.

Execute the command. When prompted by cURL, type in the password for your deployment credentials.

Browse to the deployed application using the following URL in your web browser.

http://<app_name>.azurewebsites.net

The Java sample code is running in a web app with built-in image.

Sample app running in Azure

Browse to the servlet using your web browser.

http://<app_name>.azurewebsites.net/HelloWorldServlet

The servlet is running in a web app with built-in image.

Sample app running in Azure

Congratulations! You've deployed your first Java app to App Service on Linux.

FTP deployment

Alternatively, you can also use FTP to deploy the WAR file.

FTP the file to the /home/site/wwwroot/webapps directory of your web app. The following example commandline uses cURL:

curl -T war_file_path -u "app_name\username" ftp://webappFTPURL/site/wwwroot/webapps/

Update the following:

  • war_file_path - Use the local WAR file path.
  • app_name - Use the app name your created earlier.
  • username - Use the deployment credential username you created earlier.
  • webappFTPURL - Use the FTP hostname value for your web app which you copied earlier. The FTP hostname is also listed on Overview blade for your web app in the Azure portal.

Execute the command. When prompted by cURL, type in the password for your deployment credentials.

Browse to the deployed application using the following URL in your web browser.

http://<app_name>.azurewebsites.net/helloworld

The Java sample code is running in a web app with built-in image.

Sample app running in Azure

Browse to the servlet using your web browser.

http://<app_name>.azurewebsites.net/helloworld/HelloWorldServlet

The Java sample code is running in a web app with built-in image.

Sample app running in Azure

Congratulations! You've deployed your first Java app to App Service on Linux.

Clean up resources

In the preceding steps, you created Azure resources in a resource group. If you don't expect to need these resources in the future, delete the resource group by running the following command in the Cloud Shell:

az group delete --name myResourceGroup

This command may take a minute to run.

Next steps

For more information about using Java with Azure, see the following links: