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.
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
Step 4: Click on Go
Step 5: The SSH window will open. Here you can run your shell/bash script.
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.
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
Step 2: Edit the startup.sh file in /home/dev/startup.sh
vi /home/dev/startup.sh
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
Step 4: Go to overview page of your WordPress App Service and click on restart
Step 6: Open SSH and check the changes
As you can see, the maximum number of simultaneous connections has changed to 20000.
Other common scenarios:
wp cron event run --due-now
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.