Wednesday, 21 September 2016

Using environment variables to create a generic WebJob script in Azure

I recently had to write a script for a WebJob in an Azure App Service Web App which was reused across multiple copies of the same Web App.

The script was simple, it simply needed to make a request to a scripted action (in my case PHP) within the website hosted by the Web App. The request was via a URL, which was dependent upon the name of the web app, e.g.if the web app name is "mycoolwebapp" then the URL of the scripted action would be
https://mycoolwebapp.azurewebsites.net/bin/mytask.php
Instead of writing different scripts each time, with the web app name hardcoded, it's possible to use environment variables from within the WebJob script to refer to the Web App environment variables. In this case, "mycoolwebapp.azurewebsites.net" can be referred to via the WEBSITE_HOSTNAME environment variable in a CMD script as follows
curl https://%WEBSITE_HOSTNAME%/bin/mytask.php
This single script can then be uploaded to multiple Web Jobs for your websites, without needing a different version of the script for every website.

Monday, 12 September 2016

How to enable PHP extensions by adding modules to an Azure Web App

Using Azure, you can easily deploy a Web App (a website) that uses PHP.

An additional step that you may be required to perform is to enable additional PHP extensions that contain modules your PHP code uses.

For example, if you need to add the php_sockets.dll to the Web App, you would follow these steps:

  1. Obtain the extension dll, one way to do this is to download the correct PHP installation for windows from here
  2. Extract the PHP zip file and open the ext directory, locate the required dll file(s)
  3. Add FTP credentials to your Web App and log into your Web App using an FTP client
  4. Add a bin directory to the root directory here: D:\home\site\wwwroot
  5. Copy the dll files to the bin directory
  6. In Azure click on AppServices->"MyPHPWebApp"->Settings->Application Settings
  7. Under App Settings, create a PHP_EXTENSIONS key and add a value which is a relative path to the extension dll in the root directory, e.g. bin\php_sockets.dll


Once you save the application settings, you can run phpinfo() to check that the new extension has been loaded.

See this post for more details and an alternative method.

Monday, 5 September 2016

Buying an SSL certificate for a single custom sub-domain in Azure

The process of buying an SSL certificate in Azure is relatively simple and well-documented via the App Service Certificates option in Azure.

However, I recently had to buy an SSL certificate for a specific custom sub-domain and found the process and advice to be somewhat confusing prior to committing to the purchase. This article states:
Make sure to enter correct host name (custom domain) that you want to protect with this certificate. DO NOT append the Host name with WWW. For example, if your custom domain name is www.contoso.com then just enter contoso.com in the Host Name field, the certificate in question will protect both www and root domains.
This information is specifically aimed at those who are creating a website certificate for the www sub-domain. In terms of a custom sub-domain you shouldn't follow the same advice.

Therefore, this post will help you if you're in a similar situation as follows:

  • You've deployed an Azure App Service Web App to Azure, e.g. mywebapp.azurewebsites.net
  • You own a custom domain, e.g. necloud.uk
  • You've associated a sub-domain such as mywebapp.necloud.uk to mywebapp.azurewebsites.net
Then purchase the SSL Certificate as follows:
  • Log into Azure->App Service Certificates->Add
  • Here's the catch... under the "Naked Domain Host Name" field, enter the full sub-domain, e.g. mywebapp.necloud.uk, i,.e. DO NOT enter just necloud.uk unless you specifically want a wild card certificate.
  • Once purchased, you can add the certificate to your sub-domain via Azure->App Service->mywebapp->SSL certificates->Import App Service Certificates