Exercise - Redeploy your web app to Azure App Service

Completed

In the previous exercise, you learned how to configure your web app to use the Maven Plugin for Azure App Service. Your Maven project is properly configured and tested. You can iterate, improve, and redeploy your app to Azure. For example, let's say you want to retrieve some specific details about where Azure has deployed your application code and redeploy your web app.

In this exercise, you'll update the index.jsp page for your web app to include some information about the server. After testing those changes locally, you'll redeploy your web app to Azure.

Update and test your web app

  1. Use code to edit your index.jsp page:

    code ~/MyWebApp/src/main/webapp/index.jsp
    
  2. Replace the contents of this file with the following HTML and inline Java code:

    <%@ page import = "java.io.*,java.util.*, javax.servlet.*, java.net.InetAddress, java.io.File" %>
    <html>
    <head><title>Server Status Page</title></head>
    <body>
        <h2>Server name: <% out.print(InetAddress.getLocalHost().getHostName());%></h2>
        <p><strong>Internal IP Address:</strong> <% out.println(InetAddress.getLocalHost().getHostAddress()); %></p>
        <p><strong>Free disk space:</strong>
            <%
                File file = new File("/");
                out.println(file.getFreeSpace());
                file = null;
            %>
        </p>
        <p><strong>Free memory: </strong>
            <% 
                out.println(Runtime.getRuntime().freeMemory());
            %>
        </p>
        <p><strong>Date: </strong>
            <%
                Date date = new Date();
                out.print(date.toString());
                date = null;
            %>
        </p>
    </body>
    </html>
    
  3. Save your changes by typing Ctrl+S.

  4. Quit the code editor by typing Ctrl+Q.

  5. You'll need to open a port to make the local web server available to view in a browser. In the Cloud Shell, use curl:

    curl -X POST http://localhost:8888/openPort/8000;
    
  6. Run the app locally with the following Maven command:

    mvn tomcat7:run
    
  7. Open a new browser tab using the URL in the JSON, appending MyWebApp/. You should see the new Server Status page:

    Screenshot of the example web app running on Tomcat in a web browser.

  8. Visit the deployed app by selecting the URL in the shell to see the deployed app running on App Services.

  9. Stop the web app with Ctrl+C.

  10. Close the open port using curl.

    curl -X POST http://localhost:8888/closePort/8000;
    

Redeploy your web app

  1. Rebuild and redeploy the app:

    mvn clean package
    mvn azure-webapp:deploy 
    
  2. You'll get this console output showing that the deployment used the new authentication configuration:

    [INFO] Scanning for projects...
    [INFO]
    [INFO] ------------------------------------------------------------------------
    [INFO] Building MyWebApp Maven Webapp 1.0-SNAPSHOT
    [INFO] ------------------------------------------------------------------------
    [INFO]
    [INFO] --- azure-webapp-maven-plugin:1.9.0:deploy (default-cli) @ MyWebApp ---
    [INFO] [Correlation ID: 3e5cf479-6732-4c6a-96da-88044cdadcc0] Instance discovery was successful
    [INFO] Updating app service plan
    [INFO] Updating target Web App...
    [INFO] Successfully updated Web App.
    [INFO] Using 'UTF-8' encoding to copy filtered resources.
    [INFO] Copying 1 resource to /home/~/MyWebApp/target/azure-webapp/MyWebApp-1570214065588
    [INFO] Trying to deploy artifact to MyWebApp-1570214065588...
    [INFO] Deploying the war file MyWebApp.war...
    [INFO] Successfully deployed the artifact to https://MyWebApp-1570214065588.azurewebsites.net
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 19.448 s
    [INFO] Finished at: 2019-10-09T12:00:15+00:00
    [INFO] Final Memory: 40M/324M
    [INFO] ------------------------------------------------------------------------
    
  3. Visit the deployed app by selecting the URL in the shell:

    Example web app running on Tomcat in a web browser.

Stream logs in real time

You've now deployed and run your app on App Service. If you need to troubleshoot, you can use this Azure CLI command to get real-time log streaming from the app:

az webapp log tail -n <app name> -g "<rgn>[sandbox resource group name]</rgn>"

In the next unit, you'll learn how to authenticate your web app deployments to Azure App Service.