Improve wordpress performance on Azure by disabling WP_CRON and using Azure webjobs

First of all, if you don’t use the SCM/Kudo dashboard for your azure web sites then your missing out. It allows so much management straight from the browser. Access to commandline, file editing, process explorer and so much more.

To access it:

https://[AZURE SITE NAME].scm.azurewebsites.net

For editing files, this is really easy, just then append /dev/[PATH TO FILE]. So for editing the wp-config.php we can just browse to:

https://[AZURE SITE NAME].scm.azurewebsites.net/dev/wwwroot/wp-config.php

img_584137655f19e

Disable WP_CRON

To disable the built in wordpress cron, which runs on each page load, just add

define(‘DISABLE_WP_CRON’, true);

Adding an Azure web job

We now need to schedule azure to run the WP cron manually at a set interval. For this we need the path to the wp cron file, which is on Azure:

D:\home\site\wwwroot\wp-cron.php

We then need the path to the php.exe which is:

“D:\Program Files (x86)\PHP\v7.0\php.exe”

We are the going to put them together to create:

“D:\Program Files (x86)\PHP\v7.0\php.exe” D:\home\site\wwwroot\wp-cron.php cli/auto

Testing in Debug Console

You can try this out by going to the command promt for your azure site, using, the SCM dashboard!

https://[AZURE SITE NAME].scm.azurewebsites.net/DebugConsole

If there are no errors after pasting it in and pressing enter then its run your cron jobs successfully!

Create a batch file

We now need to create a .bat file to upload to the web job, in note pad just put in:

“D:\Program Files (x86)\PHP\v7.0\php.exe” D:\home\site\wwwroot\wp-cron.php cli/auto

and save as “wordpresscron.bat”

Adding the Job

Navigate to web jobs in the azure portal for the web app

img_584145bb26202

Then click Add. You need to specify:

Name – add a nice name

File to upload – select the bat file just created

Type – Tirggered

Triggers – Schedulef

Cron Expression – agh! What to put in here?.. This is where we set how often we want it to run. More on this in a second.

Building a CRON expression

A CRON expression is a measure of time. For example, I want to run this every five minutes, so my cron expression is:

0 */5 * * * *

The best thing to do is use a cron expression builder web site, I quite like https://www.faganmedia.com/cron-expression/ . More on CRONS at https://docs.oracle.com/cd/E12058_01/doc/doc.1014/e12030/cron_expressions.htm

Put it all together and then click OK.

Your CRONs are now optomised for wordpress on Azure!

Comments

comments

Tags: , , , , , , ,

No comments yet. Be the first!

Leave a Reply

Get in touch