How to run Bash scripts in WordPress on Azure App Service
Published Sep 13 2022 10:05 PM 10.5K Views
Microsoft

You might need to run custom Linux scripts in WordPress on Azure App Service for achieving specific goals, such as, updating Nginx settings, running WP-CLI commands, and installing system packages. This article is intended to guide you in running custom shell or bash scripts.

There are two ways in which you could run a shell script in WordPress on Azure App Service.

  1. Web SSH – run shell commands and scripts that are non-persistent
  2. The start-up File – run shell commands and scripts that need to be persistent

1. Using Web SSH

As we have mentioned earlier, web SSH can be used to run commands and scripts that are non-persistent. For example, navigating folders, reading the contents of a file, or echoing a message. Here, we will learn how to echo “Hello World!” in web SSH.

Step 1: Go to your WordPress App Service page

Step 2: Scroll down the App Service blade to find Developer Tools

Step 3: Click on SSH

abhishekreddy_0-1663086994229.png

 

Step 4: Click on Go

abhishekreddy_1-1663086994236.png

 

Step 5: The SSH window will open. Here you can run your shell/bash script.

abhishekreddy_2-1663086994240.png


Note
: The changes you make using Web SSH will not persist. It means that once the server restarts, the settings will go back to default. To persist your changes, you need to follow the second option: Using Start-up File.

Linux App Service architecture inherently has non-persistent storage i.e., file changes do not sustain after app restart. It uses App Service Storage which is a remote and persistent storage mounted onto /home directory where WordPress code is hosted. Most system config files are stored in /etc directory which is non-persistent storage. Changing system configuration by updating config files in non-persistent storage may not work always as the changes would revert when the app restarts. Start-up script enables you to add start-up commands that are executed after an app container starts to make file changes that sustain through app restarts.


2. Using the start-up File

As mentioned earlier, the start-up file can be used to run commands that persist. You need to create a start-up script.

How does the start-up script work?

It is a shell/bash script (in /home/dev/startup.sh) that is executed each time an app container starts, and the changes made by start-up commands remain constant even upon restart or scaling out to multiple app instances. The reason is that when an app container starts, it's file system in non-persistent storage has a default initial state defined by the underlying docker image. When, start-up script is executed, it may update files in non-persistent storage and upon restarting the app, these files revert to the original state and start-up script is executed which provides the same final state of files in non-persistent storage.

Persistent changes might include updating Nginx configuration, running WP-CLI commands, and installing system packages.

Here we will learn how to update Nginx configuration. We will set the maximum number of simultaneous connections to 20000.

Step 1: Follow the steps mentioned in the previous method ‘Using Web SSH’.

Step 2: Check the current Nginx config using

 

cat /etc/nginx/conf.d/spec-settings.conf

 

We can see that the current value for keepalive_requests is set to 10000

abhishekreddy_3-1663086994250.png

 

Step 2: Edit the startup.sh file in /home/dev/startup.sh

 

vi /home/dev/startup.sh

 

abhishekreddy_4-1663086994255.png

 

Step 3: Add your start-up script here, in this case, update the nginx setting by using the command

 

sed -i "s/keepalive_requests .*/keepalive_requests 20000/g" /etc/nginx/conf.d/spec-settings.conf
/usr/sbin/nginx -s reload

 

abhishekreddy_0-1663738183593.png

 

Step 4: Go to overview page of your WordPress App Service and click on restart

abhishekreddy_6-1663086994275.png

 

Step 6: Open SSH and check the changes

abhishekreddy_2-1663738240427.png

As you can see, the maximum number of simultaneous connections has changed to 20000.

 

Other common scenarios:

  • Run WP-CLI commands at start-up: WP-CLI is installed by default. You can add any wp-cli command to be executed in start-up script. This example code runs cron events that are due.

 

wp cron event run --due-now

 

  • Install system packages: WordPress on Linux App Service offering is based on alpine linux distro. You can use the default apk add command to install required packages or package manager. The following command uses pecl to install imagick library for PHP.

 

pecl install imagick

 

About App Service Storage

WordPress on Azure App Service (Linux) use a central App Service Storage which is a remote storage volume mounted onto the '/home' directory in the app container. App Service Storage is persistent storage and used to host the WordPress code (in /home/site/wwwroot). It is shared across containers when the app service is scaled out to multiple instances.

Support and Feedback

In case you need any support, you can open a support request at New support request - Microsoft Azure.

Visit QuickStart: Create a WordPress site - Azure App Service | Microsoft Docs for a step-by-step guide on how to create your WordPress website on Azure. For more details about the offering, please visit Announcing the General Availability of WordPress on Azure App Service - Microsoft Tech Community.

If you have any ideas about how we can make WordPress on Azure App Service better, please post your ideas at Post idea · Community (azure.com) or you could email us at wordpressonazure@microsoft.com to start a conversation.

Co-Authors
Version history
Last update:
‎Sep 20 2022 10:40 PM
Updated by: