Are you a WordPress unicorn? In the tech industry, a “unicorn” is an early adopter of highly advanced technology. If you’re on the cutting edge of WordPress technology, looking to maximize web performance, and hyped on cloud migration for your WordPress site, this post is for you.

Azure is Microsoft’s public cloud solution, rapidly growing in popularity, and offering security, development automation and data services that are unmatched by other clouds. You have probably heard of Azure App Service - Azure’s end-to-end hosted service for web applications and RESTful APIs.

In this article, I’ll explain the basics of running WordPress on Azure and go one step further - using Ansible to fully automate the deployment of a WordPress site on Azure infrastructure.

Running WordPress on Azure App Service

Azure App Service offers a user-friendly, scalable, and secure environment designed especially for large scale, mission-critical WordPress sites. Here are the prerequisites for a basic installation of WordPress:

  1. MySQL database - you can self-manage your MySQL installation on Azure Virtual Machines (AVMs) using Windows or Linux. Alternatively, you can use ClearDB which is available in the Azure Marketplace.
  2. PHP 5.2.4 or greater - Azure App Service supports PHP versions 5.4, 5.5, and 5.6.

Basic deployment

You can use the above two requirements to create a basic solution and place it in an Azure region. This allows you to create multiple Web Apps instances of your site.

However, note that this hosts the application in one specific geographic region. This means that web visitors from outside the boundaries of this region may experience slower response times when browsing the site. Additionally, if the Azure data centers within this region fail, your website will go down.

Multi region deployment

You can use Azure Traffic Manager to scale a WordPress site across several geographic regions - while offering users only one URL. The Traffic Manager then uses the load balancing configuration to route visitors.

You can scale a WordPress site within the region across multiple instances of Web Apps. However, you can only do region-specific scaling, because high traffic regions offer different scalability than low traffic ones.

Multi-region deployment with media storage and caching

You should use Azure Blob Storage for WordPress sites that host media files or accept uploads. Blob Storage uses geo-distribution across regions by default, which ensures files are replicated.

You can enable Azure Content Distribution Network (CDN) for Blob Storage. This feature can distribute files to end nodes that are closer to your site visitors. Additionally, you can implement caching by using Redis cache, MemCachier, Memcache Cloud or any caching option offered in the Azure Store.

Migration

Here are two options that let you migrate an existing WordPress site to Azure App Service:

  1. WordPress export - a plugin that lets you export the content of the site. You can then use the WordPress importer plugin to import the content to a new WordPress site hosted on Azure App Service.
  2. Manual migration - this option requires you to back up your site and databases, and then manually restore everything to a web app within Azure App Service and an associated MySQL database. This can help you migrate customized sites without manually reinstalling all of the themes, plugins, and other customizations.

Cost

Azure App Service has a Free and Shared pricing plan that lets you test your website in Azure at low or no cost. As you transition to production, you will start paying a cost per hour determined by your service tier:

  1. The Standard Run tier provides 50 GB storage and up to 10 application instances at $0.1 per hour.
  2. The Premium Enhanced tier provides 250 GB of storage and up to 30 application instances at $0.2 per hour.
  3. The Isolated High-Performance tier provides 1 TB of storage and up to 100 application instances at $0.4 per hour.

To understand Azure costs in more depth, see this extensive blog post on Azure pricing.

Ansible on Azure Overview

Ansible is a free and open-source solution that automates several cloud aspects, including the configuration management, provisioning, and deployment of applications. You can use Ansible to provision containers, virtual machines (VMs), networks, and any other cloud infrastructure.

You should be familiar with these key Ansible concepts:

  1. Ansible playbooks - allow you to tell Ansible to configure an environment by creating a YAML configuration (known as a playbook) defining that environment.
  2. Ansible modules - let you control system resources, including packages, files, or services, and execute system commands. You can create your own modules and run them via playbooks or directly on remote hosts.

Ansible offers a suite of cloud modules that allow you to interact with Azure services. You can use these modules to create and orchestrate your own infrastructure on Azure. You can then apply the playbook of your application and let Azure automatically scale the environment as needed. Learn more in this detailed blog post about Ansible on Azure.

Quick Tutorial: Azure App Service using Ansible

Azure App Service supports automated deployments from Git repositories, such as Azure DevOps and GitHub, using Ansible playbooks. Follow these instructions to set up your WordPress application automatically on Azure App Service.

This is abbreviated from the full tutorial in the Azure documentation.

Step 1: Create a Basic App Service

The playbook code below defines certain resources:

  1. An Azure resource group - your App Service plan and application are deployed within this group.
  2. App service on Linux - defined with Java 8 and a Tomcat container runtime.

You should save the below playbook as firstwebapp.yml:

WordPress with Ansible on Azure

Next, run the playbook by using ansible-playbook:

ansible-playbook firstwebapp.yml

Step 2: Create an App and Use Azure Traffic Manager

Azure Traffic Manager lets you control how to distribute requests from web clients among applications managed by Azure App Service.

As you add the App Service to Azure Traffic Manager profile, it tracks the status of each application. These statuses are “running”, “stopped”, and “deleted”. Traffic Manager will help determine which endpoint should receive the traffic.

Within App Service, applications run inside an App Service plan, which defines which resources each application should run on. You can manage App Service plans and web applications in different groups.

See the playbook below, which creates the following resources in Azure:

  1. An Azure resource group
  2. An App Service plan
  3. An additional Azure resource group (allowing the application to run across two resource groups for high availability)
  4. App service on Linux—running Java 8 and Tomcat
  5. A Traffic Manager profile and endpoint that exposes the application

Save the below playbook as webapp.yml:

WordPress with Ansible on Azure

Ansible Azure

Next, run the playbook using ansible-playbook:

ansible-playbook webapp.yml

Step 3: Scale Up an App

You can either scale up or scale out. Scaling up requires adding more resources, such as memory, CPU, disk space, or VMs. This requires changing the App Service pricing plan for the application.

Scaling out requires increasing the number of virtual machines used to run your app - up to 20 instances. The maximum number of VMs you can run depends on your plan pricing tier. You can also use auto scaling to automatically scale instance count.

The below playbook code defines the following operation:

  1. Retrieve data about an existing App Service plan
  2. Update to the “S2” App Service plan to support three workers

You should save the below playbook as webapp_scaleup.yml:

WordPress with Ansible on Azure

Next, run the playbook using ansible-playbook:

ansible-playbook webapp_scaleup.yml

Conclusion

In this article I explained the basics of Azure App Service for WordPress, and showed how to automate the deployment of a WordPress site with Ansible in a few simple steps:

  1. Create an App Service for your WordPress site, using an Ansible playbook that defines an Azure resource group and a Linux App Service.
  2. Set up Azure Traffic Manager by setting up an additional Azure resource group with App Service, setting up a Traffic Manager profile and endpoint, allowing users to contact the endpoint and be redirected to the application instance nearest to them. This facilitates multi-region WordPress deployment in Azure.
  3. Scale up your app - automatically scale up the App Service plan to support three instances of your WordPress site.

I hope this will be of help as you push the envelope of WordPress development on tomorrow’ cloud infrastructure.