Installing WordPress on “Azure App Service on Linux”

Realizing you may not want to have to manage an entire VM, a handy alternative is to use the new(ish) App Service on Linux feature that Azure has released.  This means you do not have to manage the entire server like you normally would, but can instead just put your bits on the app service and have it run.  Getting everything running is a bit cumbersome, and you will need to run a bunch of CLI commands, but it gets WordPress up and running.  Besides, isn’t the hardest part of using WordPress getting the environment set up anyway?

Notes before you start:

  • The following command is optional, if you want to put it into an existing resource group:
    New-AzureRmResourceGroup -Name <NameHere> -Location <DesiredRegionNameHere…ie. eastus>
  • (If you chose to create a new Resource Group, you will also need the Region ID for the region you wish to deploy to. https://azureprice.net/Region )
  • The template uses the chosen site name as the base name for the database name, server name, and hosting plan name (ie. mydatabase, mysqlserver, etc.).  If you would like this changed for standardizing the naming, you can change out the “variables” section of the azuredeploy.json, and put in what you need to.

Prerequisite: Have both Azure CLI and Azure RM for Powershell installed:

In Powershell (This will create the Resource Group, the MySQL server, the Linux plan, and the app.  Be sure to save the database admin and login info to KeyPass when you are prompted to create it):

Login-AzureRmAccount 

[Optional]

New-AzureRmResourceGroup -Name <NameHere> -Location <DesiredRegionNameHere...ie. eastus> 

[/Optional]

New-AzureRmResourceGroupDeployment -Name <NewNameHere> -ResourceGroupName <NewNameHere> -TemplateFile c:\azure\azuredeploy.json   

Use the .json file below for the above command, and place it somewhere you can run the PS against.  (this is borrowed from the official Azure Quick Start Templates: https://github.com/Azure/azure-quickstart-templates/tree/master/wordpress-app-service-linux) :

<<azure-app-linux.zip>>

Fill out the remaining prompts.

Now you need to create the database for hosting WordPress on the new server (You will need to find the Subscription GUID on portal.azure.com for the MySQL, or pull it from the results after “az login”):

az login 
az account set --subscription 00000000-0000-0000-0000-000000000000 
az mysql db create --name <DbNameHere, ie. "wordpress"> --resource-group <ResourceGroupNameHere> --server-name <MySQLServerNameHere, just the first subdomain name part, not namesitemysqlserver.mysql.database.azure.com> 

In the Azure Portal, SSH to the App, and run the following scripts:

cd /tmp 
curl -O https://wordpress.org/latest.tar.gz 
tar xzvf latest.tar.gz 
touch /tmp/wordpress/.htaccess 
chmod 660 /tmp/wordpress/.htaccess 

Then use VIM to edit the htaccess file:

(Use Shift+i to edit the file, then Esc, Shift+zz to save)

vim /tmp/wordpress/.htaccess

Add the following values to the htaccess file:

php_value max_input_vars 3000 
php_value max_execution_time 60 
php_value post_max_size 32M 
php_value upload_max_filesize 64M 

Save the file, and return to the command line.  Run the following scripts:

cp /tmp/wordpress/wp-config-sample.php /tmp/wordpress/wp-config.php 
mkdir /tmp/wordpress/wp-content/upgrade 
cp -a /tmp/wordpress/. /home/site/wwwroot 
rm /home/site/wwwroot/hostingstart.html 
find /home/site/wwwroot -type d -exec chmod g+s {} \; 
chmod g+w /home/site/wwwroot/wp-content 
chmod -R g+w /home/site/wwwroot/wp-content/themes 
chmod -R g+w /home/site/wwwroot/wp-content/plugins 
curl -s https://api.wordpress.org/secret-key/1.1/salt/ 

Copy the results, then add it to the wp-config.php file. Replace the following:

define('AUTH_KEY',         'put your unique phrase here'); 
define('SECURE_AUTH_KEY',  'put your unique phrase here'); 
define('LOGGED_IN_KEY',    'put your unique phrase here'); 
define('NONCE_KEY',        'put your unique phrase here'); 
define('AUTH_SALT',        'put your unique phrase here'); 
define('SECURE_AUTH_SALT', 'put your unique phrase here'); 
define('LOGGED_IN_SALT',   'put your unique phrase here'); 
define('NONCE_SALT',       'put your unique phrase here'); 
vim /home/site/wwwroot/wp-config.php 

Also update DB connection details in wp-config.php with Azure MySQL DB:

DB_NAME: <wordpressDbName> 
DB_USER: <dbadmin>@<name>sitemysqlserver 
DB_PASSWORD: <dbadminpw> 
DB_HOST: <name>sitemysqlserver.mysql.database.azure.com 

Navigate to http://<name>site.azurewebsites.net and complete the setup