Creating a WordPress Site Using Azure WebApp & MySQL

Want to start blogging? Lets create a simple WordPress site hosted on a WebApp along with a MySQL DB

With a few Azure CLI commands you will have the basics up and running, pretty cool – eh?

Secondly, the configuration is totally stateless as you will be configuring both an Azure WebApp and Azure Database for MySQL

To create a WebApp, you will need an active App Service plan. A service place defines a set of compute resources for for a WebApp to run on. They are prices in tiers:

  • Shared Compute: Free & Shared – Used for developed or testing purposes, these are shared with other customers. These tiers allocate CPU quotas to each app that runs on the shared resources, and the resources cannot scale out. Also note, these tiers have any a certain amount of compute minutes per day (Free: 60 CPU mins Shared: 240 CPU mins and cannot scale out)
  • Dedicated compute: The BasicStandardPremium, and PremiumV2 tiers run apps on dedicated Azure VMs
  • Isolated Compute: Runs on dedicated Virtual Machines inside a dedicated Virtual Network
  • Consumption: Only available for Azure Functions

Lets get started, create a Resource Group & Linux App Service Plan, will be hosted in North Europe and with use the Standard service plan S1:

$ResourceGroup = "tamops-wp-rg"
$Location = "northeurope"
$AppPlanName="tamops-wpplan"
az group create -l $Location -n $ResourceGroup
az appservice plan create -n $AppPlanName -g $ResourceGroup -l $Location --is-linux --sku S1

Now lets create the WebApp and use WordPress Docker container image

$AppName = "tamops-wp-app"
az webapp create -n $AppName -g $ResourceGroup --plan $PlanName -i "wordpress"

The Azure Database for MySQL will be a simple deployment, notice –ssl-enforcement is disabled due to WordPress doesn’t use SSL during initial setup – a separate plugin is required.

$MySqlServerName = "tamops-mysql"
$MySqlAdminUser = "tamopsadmin"
$MySqlAdminPassword = "PasswordEnterHere"
az mysql server create -l $Location -g $ResourceGroup -n $MySqlServerName -u $MySqlAdminUser -p $MySqlAdminPassword --sku-name B_Gen5_1  --ssl-enforcement Disabled

Now, lets create a firewall rule to allow Azure App Service acess to the newly created database.

az mysql server firewall-rule create -g $ResourceGroup --server $MySqlServerName --name AllowAppService --start-ip-address 0.0.0.0 --end-ip-address 0.0.0.0

You see the any:any ip addressing and wonder if I have made a mistake? Nope! Taken from Microsoft documentation

This option configures the firewall to allow all connections from Azure including connections from the subscriptions of other customers. When selecting this option, make sure your login and user permissions limit access to only authorized users.

Time to add some environment variables that is required by the WebApp for a successful configuration of a WordPress Setup

$Find_DBHostName = (az mysql server show -g $ResourceGroup -n $MySqlServerName --query "FullyQualifiedDomainName" -o tsv)

az webapp config appsettings set -n $AppName -g $ResourceGroup --settings WORDPRESS_DB_HOST=$Find_DBHostName WORDPRESS_DB_USER="$MySqlAdminUser@$MySqlServerName" WORDPRESS_DB_PASSWORD="$AdminPassword"

Good news – you have now configured a stateless WordPress site in a matter of minutes!

$DisplaySite = az webapp show -n $AppName -g $ResourceGroup --query "DefaultHostName" -o tsv
Start-Process https://$DisplaySite

Pretty cool eh? Follow the on screen setup for WordPress and you will finish with a main page like this:

Full script below, change the parameters and it can be ran as one complete script!

$ResourceGroup = "tamops-wp-rg"
$Location = "northeurope"
$AppPlanName="tamops-wpplan"
$AppName = "tamops-wp-app"
$MySqlServerName = "tamops-mysql"
$MySqlAdminUser = "tamopsadmin"
$MySqlAdminPassword = "PasswordEnterHere"

az group create -l $Location -n $ResourceGroup
az appservice plan create -n $AppPlanName -g $ResourceGroup -l $Location --is-linux --sku S1
az webapp create -n $AppName -g $ResourceGroup --plan $PlanName -i "wordpress"
az mysql server create -l $Location -g $ResourceGroup -n $MySqlServerName -u $MySqlAdminUser -p $MySqlAdminPassword --sku-name B_Gen5_1  --ssl-enforcement Disabled
az mysql server firewall-rule create -g $ResourceGroup --server $MySqlServerName --name AllowAppService --start-ip-address 0.0.0.0 --end-ip-address 0.0.0.0
$Find_DBHostName = (az mysql server show -g $ResourceGroup -n $MySqlServerName --query "FullyQualifiedDomainName" -o tsv)
az webapp config appsettings set -n $AppName -g $ResourceGroup --settings WORDPRESS_DB_HOST=$Find_DBHostName WORDPRESS_DB_USER="$MySqlAdminUser@$MySqlServerName" WORDPRESS_DB_PASSWORD="$AdminPassword"

$DisplaySite = az webapp show -n $AppName -g $ResourceGroup --query "DefaultHostName" -o tsv
Start-Process https://$DisplaySite

Running this stateless WordPress Application is not the cheapest option to host your personal blog but gives you an insight into the scalability Azure can provide.

In a future blog I will detail a cost effective personal blog that can be hosted in Azure

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s