Azure App Services and WordPress Slow? Heres how to fix it.

Azure App Services are a really good Platform as a Service offer in Microsoft Azure for lots of web applications, particularly anything written in .Net core. One would assume that regardless of language, that Azure App Services should perform equally well, but that unfortunately isn’t the case with WordPress or basically anything written in PHP. At Accigo, we have ran into some performance issues when setting up this very site. Luckily, we have figured out how to make app service’s perform relatively fast with WordPress and anything written in PHP. In order to get this performance, there are a few things you can do.

 

Enable Always On

Enabling Always On essentially forces the app service worker threads to stay active for the app service, so that they don’t become inactive. When the threads in the app service become inactive, it can take a while when the first packet hits your app service for it to spin up and be active. This is actually a super easy fix, which can be done by enabling this in the Application Settings.

app service always on

PHP Version

Believe it or not, the version of PHP matters a bit and the latest version of WordPress recommends to use PHP 7.2. Switching to the latest version of PHP made an impact on how responsive the web application become. You can find this under Application Settings for your app service.

app service php version

Switching to PHP FastCGI

The next thing you can do is to force your app service to use the Fast CGI process manager for the version of PHP you are using. In this example, I am using PHP 7.2, so I will want to use the PHP 7.2 Fast CGI process manager for my app service running php. In addition, we are also disabling buffer throttling as well. To do this, you need to go to your App Service in the Azure portal, go to Advanced Options (Kudu), use the process explorer to find the wwwroot directory, then edit up the web.config file. You will want to add the following snippet of XML:

 

<handlers>
            <add name="PHP-FastCGI"
                path="*.php"
                verb="GET,HEAD,POST"
                modules="FastCgiModule"
                scriptProcessor="D:\Program Files (x86)\PHP\v7.2\php-cgi.exe"
                resourceType="Either"
                requireAccess="Script" 
                responseBufferLimit="0" />
</handlers>

 

Keep in mind that you will want to change the ScriptProcessor path to the version of PHP you want to use, but in this case, I am using PHP 7.2. You may some some rewrite rules as well, so you would want to modify the file to be something like:

 

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
  <rule name="WordPress: http://appservicename.azurewebsites.net" patternSyntax="Wildcard">
    <match url="*"/>
      <conditions>
        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true"/>
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true"/>
      </conditions>
    <action type="Rewrite" url="index.php"/>
  </rule></rules>
    </rewrite>
    <handlers>
            <add name="PHP-FastCGI"
                path="*.php"
                verb="GET,HEAD,POST"
                modules="FastCgiModule"
                scriptProcessor="D:\Program Files (x86)\PHP\v7.2\php-cgi.exe"
                resourceType="Either"
                requireAccess="Script" 
                responseBufferLimit="0" />
         </handlers>
  </system.webServer>
</configuration>

 

Now you will want to add the following to your .user.ini file at the root of your WordPress installation. This will remove throttling to your MySQL database so that the database connection is not a bottleneck.

 

output_buffering = Off;

 

Go ahead and save it and restart your app service. The change should now be live. If this breaks your site, then you chose the wrong version of PHP or you messed up the XML syntax.

 

Persistent Database Connection

The very last thing that you will need to do after this will be to enforce a persistent database connection between WordPress and MySQL. Out of the box, WordPress does not use a persistent database connection, which means that after a period of idling, the connection to the database will be stopped and when someone visits your app service and it will have to set up a connection to the database, which makes WordPress extremely slow. In order to fix this, you need to login to Advanced Tools (Kudu), navigate to wwwroot in your app service, edit wp-config.php and add the following at the bottom:

 

define('USE_PCONNECT', true);
define('WP_CACHE', false);

 

Once you have done, that save it, and restart your app service. WordPress will now have a persistent database connection and this is one of the biggest things you can do to optimize WordPress for Azure App Services.

 

App Service Local Caching

Note: Enabling local caching will change how the storage works for your app service. Enabling this will clear any new changes that has happened since turning this functionality on. This can result in the loss of changes when the app service switches hosts or is restarted. This is only recommended for more static WordPress instances and not for WordPress instances that have new posts or pages. If your WordPress instance is dynamic, we recommend that you check out Azure Redis Cache, as this is a great way of improving performance if you are not already object caching.

 

One of the last things you can do to improve the performance of your app service is to force your app service to use local caching. When enabling this, you also need to set the max size, which is 2 GB or 2000 MB. You can change it by adding the following in the Application Settings:

 

WEBSITE_LOCAL_CACHE_OPTION Always
WEBSITE_LOCAL_CACHE_SIZEINMB 2000

app service cache settings

Azure Redis Cache for Object Caching

Object caching allows your web application to cache the results of queries as objects in low latency memory based storage. Azure Redis Cache is a great way to speed up your WordPress instance and it will also allow your app service to scale better since it takes some of the load off of the database. We have written a great guide on how to set up Azure Redis Cache for WordPress as Azure Redis Cache is a great solution to improve the performance of your web application.

 

Final Thoughts

Outside of these things, there are other things you do can within wWordpress to make it even faster, such a caching web content, compressing images, minify css/javascript, optimize MySQL to update statistics and rebuild indexes, etc. If you follow all of these recommendations, your app service running WordPress or basically any PHP application should now run fairly fast and it should no longer be slow. It is also important to note that these recommendations are for Azure web applications running windows (IIS) and not Azure Web Applications running Linux. WordPress is also dependent on the performance of MySQL, so if you are running MySQL in an app service plan, you will want to scale it up. Alternatively, MySQL in an Azure Database is a great and scalable alternative for running WordPress in an Azure App Service.

Do you find our content useful? Like and Share our Posts!

Leave a Reply

Your email address will not be published. Required fields are marked *