Improving performance with application initialization on Azure App Service (Windows)

Often, performance on a website isn’t what it needs to be. One way you can improve performance is to cache files to the local disk. On Azure App Service Web Apps, an application can access the local disk at D:\local\temp. This location is specific to each instance. Due to the nature of Platform-as-a-service, this location is not easily accessible to the user. However, the application has access to this location pro-grammatically. Moreover, many popular applications and frameworks have file caching as a default feature.

PHP, a popular programming language in the Open Source community, has several MVC Frameworks that include file caching. Frameworks such as Laravel, Symfony, CodeIgniter and many others. In these frameworks, the cache location can be set to the local disk on the VM rather than the storage.

An easy way to clear this cache is to restart the App Service Web App. However, this causes your application to go offline for some time. An alternate way to clear the cache is to utilize the AppInit module within IIS. The IIS AppInit module can execute a script on a W3WP process recycle. These recycles occur on restarts, application settings updates, scaling to new instances, etc. This is not a complete list and more information can be found online.

In this article, you will learn how to execute a script through an application settings update. This is not intended to be a complete solution as your use-case may vary. It is intended to show you how the AppInit module can be used within App Service Windows.

The solution has three components:

  1. Create an initialization page to be executed upon a recycle. This is the clear cache script.
  2. Set the initialization page in the web.config.
  3. Update an application setting to trigger the recycle

Here is a sample initialization page written in PHP:

<?php 
/***** File Location: wwwroot/appInit/maybeCacheClear.php
***** This script runs when the web app is recycled.
**************************************************************
You can lock the cache in two ways so it is not cleared on a recycle:
1) add the application setting 'cacheLock' and set the value to true
2) create a file, cache.lock, within this directory
******************/
if ( file_exists('cache.lock') || ("true" == strtolower(getenv('cacheLock'))) ) 
{
$message = "================================\n" . 'Time Stamp: '. date('m/d/Y H:i:s') ."\n" . "--------------------------------\n" . "Cache is locked. Cannot clear cache.\n" . 'Computer Name: ' .$_SERVER['COMPUTERNAME'] ."\n" . 'Instance ID: ' . $_SERVER['WEBSITE_INSTANCE_ID'] . "\n\n";
}
else {
//!!CLEAR THE CACHE!!
//<INSERT CODE HERE TO CLEAR CACHE>
$message = "================================\n" . 'Time Stamp: '. date('m/d/Y H:i:s') ."\n" . "--------------------------------\n" . "*** CLEARING CACHE***\n" . 'Computer Name: ' .$_SERVER['COMPUTERNAME'] ."\n" . 'Instance ID: ' . $_SERVER['WEBSITE_INSTANCE_ID'] . "\n\n";
}
// Write the contents back to the file file_put_contents('cache.log', $message, FILE_APPEND);

Now that the initialization page has been created, tie this into the web.config so it is executed on a recycle. Here is a sample snippet with the Application Initialization script configuration

<configuration> 
<system.webServer>
<applicationInitialization doAppInitAfterRestart="true" skipManagedModules="true">
<add initializationPage="/appInit/maybeCacheClear.php" hostName="mangeshnew.azurewebsites.net"/>
</applicationInitialization>
</system.webServer>
</configuration>

The last piece of the solution is within Application Settings. Here we will create a key named “cacheLock” with a value of “true”. This setting determines if the cache should be cleared or not. When the value is set to “true”, the cache is locked and will not be cleared. However, when set to “false” the cache is no longer locked and it will erase the cache from the local disk.

Now that web.config, cache clear script and Application Settings are in place, trigger a recycle by updating an Application Setting. If everything went well, you will see a message within cache.log. It should look like this:

That’s it! As simple as that. Once you’ve placed these components in place, you can utilize Application Initialization to clear cache from local instances. I hope this has helped your application design be more performant. Questions or comments? Please leave feedback below!

WordPress Assistant for Azure App Service on Windows

App Service Assistant is a WordPress plugin that provides details on important settings and configuration. This can be helpful to troubleshoot WordPress issues. Information such as current theme, theme path, WP defines, plugins, php_errors.log (if enabled), debug.log (if enabled), user.ini, app settings.

Installing this plugin is a cinch! Just follow the steps below to get it activated and running in your WordPress application.

Installation:

Plugins > Add new

Search for: app service assistant

Click Activate


How to use:

From settings menu, select “App Service Assistant”

Sample screenshot:

Location of Log File – AppAssistant.html is saved in the D:\home\LogFiles directory.

Feedback / Suggestions – Email msangapu@outlook.com

Import a WordPress MySQL Database using WordPress Buddy+ on Azure App Service

This post discusses how to use the Azure Web App Site Extension, WordPress Buddy+, to import a WordPress MySQL Database (.sql file) into MySQL In-App.

If you haven’t already, review and install (link here) the WordPress Buddy+ Site Extension for Azure Web Apps. The steps below require MySQL In-App to be enabled.

Import Steps

  • From Kudu CMD console (<webappname>.scm.azurewebsites.net), browse to D:\home\SiteExtensions\WPBuddy\sqldumps\ folder.
  • From your desktop, drag and drop the .sql file into sqldumps folder
  • Launch WP Buddy+ Site Extension
  • Click on Migration
  • From the Menu, Select Import
  • Select the .SQL file from the drop-down. If the file you copied is not visible. Revisit step 2 and ensure the extension is .sql
  • Select the Database to Import To [For MySQL In-App, choose: 127.0.0.1 (azuredb]
  • Click Start Import!

Once the import has finished, ensure the settings in wp-config.php is accurate for MySQL In-App.

WordPress Buddy+ site extension for Azure App Service on Windows

WordPress Buddy+ is an Azure Web Apps Site Extension that facilitates WordPress configuration and especially useful for WordPress migrations. It has features for updating HOME and SITE_URL, viewing list of plugins and updating passwords. In addition, it can also help migrate the MySQL database, whether it be MySQL In-App, ClearDB or on a Virtual Machine (VM).

Installation


Fig 1. Install WordPress Buddy+ Site Extension


In the Azure Portal, select Web App and scroll to Development Tools section

  1. Select Extensions
  2. Click +Add
  3. Select Choose Extension
  4. Scroll to find WordPress Buddy+
  5. Confirm the Legal Terms and click OK

If you need additional help installing this or other Site Extensions, check out the article here.


Prerequisites – Connection Strings

ClearDB or MySQL Server on VM

If using ClearDB or MySQL on VM, ensure that the connection string is in place within application settings.

The format is as follows: Database=<db name>;Data Source=<host name>;User Id=<username>;Password=<password>

Example:


Fig 2. Application Settings > Connection Strings

MySQL In-App

When using MySQL In-app, the connection string is created automatically. Ensure In-app is enabled.’


Fig 3. MySQL In-App Blade

Fig 3. MySQL In-App Blade


WordPress Tools

Fig 4. WordPress Tools

  1. When using multiple connection strings, select the appropriate database
  2. Displays list of Active Plugins and provides an option to Disable All Plugins
  3. Update Home and SITE_URL in WP_OPTIONS table by updating fields
  4. Used to update passwords

MySQL Migration

Overview

Fig 5. Migration Assistant

  1. List of databases (from connection strings)
  2. Downloadable .SQL files on the server
  3. Help – description of features
  4. Import .SQL to MySQL Database
  5. Export a MySQL Database to .SQL
  6. Delete .SQL files listed in (2)

Import

Fig 6. Import Menu

  1. Select .SQL file to import
  2. Select Database to import TO
  3. Click to begin the import

Export

Fig 7. Export Menu

  1. Clicking the button will export all databases listed in connection strings

Clean-up

Fig 8. Clean-up

  1. Delete all .SQL files