Last week during PHP[World], my team Azure Web App and I released experimental support for PHP7 RC7 on Azure Web App. This is a really exciting announcement as it allows developers to simply set up an environment to test a PHP 7 app. How simple you ask? Really simple as demonstrated by Jose Miguel Parrella in the following video.
Not simple enough? Try clicking this button below to setup a site in your subscription with PHP 7 pre-installed.
The functionality is provided by leveraging a Site Extension which hooks into the IIS pipeline using an applicationHost.xdt transform file. This means that there are a few things you need to be aware of including how to change the php.ini settings for the site which can be done in two different ways.
Changing the PHP.ini
The site extension works by downloading the PHP 7 RC7 runtime from the Windows side of PHP.net. This means that you need to track down the install folder to change the PHP.ini directly. This is slightly hidden away due to the Site Extension, but you can find the php.ini in the following directory:
There is another way of providing changes to the PHP.ini without modifying the main php.ini directly, this is to create a PHP_INI_SCAN_DIR App Setting and provide it a path in which you will drop new ini files. I consider this the preferred method of implementing ini changes as the changes can persist in the same Web App after the Site Extension is deleted (when PHP7 is available in a supported capacity on Web App). There are full instructions on how to make this work available on Azure.com under the article Changing PHP_INI_SYSTEM configuration settings.
This post is going to be relatively short, but I’ll update it with more information as I receive more feedback from users.
File System Issues
Recently there were some changes made in WinCache (v188.8.131.52) which reroutes file system calls such as is_dir, is_file & file_exists to a manifest in Wincache. This means that there is a potential for files that have been deleted on disk, to report as still available. This causes issues when updating WordPress, themes or plugins from a PHP script (such as the one used by the internal WordPress update tool). To remove these redirects follow the instructions below:
Create a file called .user.ini (provided it doesn’t already exist)
Append wincache.reroute_enabled=0 to the end of the file
Upload the .user.ini file to the d:\home\site\wwwroot directory
This will remove any file system based errors which may occur during the update.
If you are running a giant WordPress blog on Azure App Service, there is the possibility that the database updates will take longer than the request timeout time. This will cause HTTP 5xx error messages when you are attempting to update.
In a previous post I outlined how to enable deployment time dependency management with Wep App by hooking the deployment with Kudu. In this post, I will explain how I iterated on the techniques used in that previous post, to enable Composer in Web App via a Site Extension which lowers the barrier to use Composer with Web App.
Cool, but what exactly is a Site Extension?
Site Extensions are a means of extending the Azure App Service platform. The best part about Site Extensions are that they can be created by anyone, it is as simple as creating a Website, WebJob or IIS Module, an applicationHost.xdt file (applicationHost.config Transform), and possibly some supporting files to help install or setup the Site Extension components. These components are then packaged with NuGet and uploaded to the Site Extension Gallery
More information is available on how to create a Site Extension is available in the Kudu Wiki.
Now, How do I use Composer in Web App?
To get started with Composer on Web App, you’ll need to install the Site Extension, luckily I wrote a post which explains how to enable a site extension. You’ll find Composer in the list of Site Extensions.
Creating a web site which uses Composer
Composer has a number of different ways that it can be leveraged, you can run it as a tool from the command line directly to have it build files or you can manually create a composer.json file. I’ll refer you to the documentation to figure out how you’d like to use it.
For my application, I crafted the composer.json file by hand using Packagist as a reference for my file dependencies. I wanted to create an application which lists files from a particular container in Azure Storage, this means I would require the Microsoft Azure SDK for PHP and it’s dependencies. Here is my composer.json file.
The composer.json file should be placed in the root of the repository. The Composer Site Extension has the COMPOSER_VENDOR_DIR set to d:\home\site\vendor which is out of the public wwwroot directory so that the dependencies cannot be addressable publicly.
Now that we have our dependencies taken care of simply build a PHP application which requires the autoloader which is created for us by composer, then write our code. Here is my index.php file which iterates over items in my storage container.
Now we’re ready to deploy the application and configure a few of the settings to make the application come to life. Click the button below to deploy the application into your Microsoft Azure Subscription:
This Deploy to Azure button could be configured to set these App Settings automatically on deployment, If you see a azuredeploy.json file in the repository you can disregard the following steps. As I have found the time to make the Azure Resource Manager template to hydrate these values.
Add the following App Settings to the App Settings list in the Portal:
container – This is the name of the container in your storage account that you’d like to list the values of. This is referenced in the code above by the call to getenv(‘container’) which reads this value from an environment variable
DEPLOYMENT_SOURCE – Typically, I would add a .deployment file to the respository to change the project config setting to web as this will change the deployment directory and only deploy the items in the web folder. Due to how the Composer site extension is hooking the deployment, the .deployment file doesn’t work so this setting is needed to let Kudu know that the contents of web should be copied to the wwwroot directory
Add the following Connection String to the Connection Strings list in the Portal:
BlobConnectionString – This should contain the connection string for your azure storage account. Set the type to Custom.
There is an assumption here that you actually have a few items in the storage account. If you don’t upload a few now then refresh the website.
That’s it! You have successfully deployed a PHP application which uses composer!
Site Extensions are an amazing part of Azure Websites, they have the ability to (surprise, surprise) extend the functionality of a Website by providing new language support or some new functionality to the site. This can be seen from my last post where I added WordPress Command Line Interface support to Azure Websites.
In that post, I showed how you can use the new preview portal to add the site extension, but in this post I wanted to talk about both the Current Management Portal as well as the Preview Portal so there is single place to find both instruction sets.
Site Extensions (Management Portal)
There is no integration within the management portal to enable Site Extensions, but all isn’t lost. You can still add site extensions from your Kudu Console (SCM Site), one of the easiest ways to get to your website is the portal (provided you’re already there). You can click Browse to launch your website.
Once our website loads, we can change the URL, slightly, to enter into the Kudu Console. You’ll notice I’ve added the https:// protocol to the beginning of the URL and .scm after the name of the website.
In the navigation bar, you will see Site Extensions.
There are two tabs on the Site Extensions page, Installed and Gallery. There might not be anything listed under installed at first. Click on gallery to get the list of Site Extensions you can install.
Clicking on a plus sign on a gallery item will install the Site Extension. After the Site Extension is install, you must hit the “Restart Site” button in the upper right hand corner of the Site Extensions page. Clicking on the circled letter ‘i’ will display the detailed information for the Site Extension.
Site Extensions (Preview Portal)
There is direct Site Extension integration in the Preview Portal. We’ll start from the default blade for our Website.
Click on Settings in the Command Bar. This opens the settings list in which we will find an option for Extensions.
This opens a blade which lists previously installed Site Extensions, and has an add button to add new Extensions to our website.
Click Add. This opens the Site Extension gallery, where we can select a new Site Extension to enable.
The next step is to accept the legal terms for the Site Extension.
Then finish the installation process by clicking OK.
You can Browse, Update or Delete the Site Extension by right-clicking on the entry in this list.
WordPress is the most popular CMS on the Web so there are obviously a great set of tools surrounding it to enable the wide variety of developers who build on the WordPress platform. One of such tools is WP CLI which is a command line interface for managing your WordPress site. In this post, I’m going to cover how to install the WP CLI site extension into your Azure Websites hosted WordPress install to enable command line access to your site.
Install WordPress on Azure
Install WP CLI Site Extension
Command line to WordPress
Install WordPress on Azure
This step is a little bit out of scope for this topic, if you’re savvy enough to know there is a command line tool for WordPress, I’d assume you’d know how it set it up. If for some reason you don’t know, I’m just going to leave these here:
To add a site extension, you need to login to the Azure Portal and go into your site.
If you scroll to the bottom of the site blade, you will find a part called Extensions which on that which will open the Site Extensions blade.
Scroll down the list until you find WordPress CLI, click on the item, then accept the license terms. Click OK and the site will begin to install the extension.
Command line to WordPress
Ok great, I have the Site Extension installed but how does that help me? I want to use the command line. There are two different command lines that I want to show off.
Kudu – Debug Console (Web Based)
When you create an Azure Website in the background a second site is created at http://<site-name>.scm.azurewebsites.net which exposes the Kudu Console (Kudu is the deployment engine for Azure Websites). You can access this site using either OAuth (which is enabled by default) or Basic Auth (http://<site-name>.scm.azurewebsites.net/basicauth). It’s cool that you have web based access to your website.
KuduExec (Terminal, PowerShell, Command Prompt)
Although it is really cool to be able to use the command line in a web browser, wouldn’t it be nice to be able to use your command line of choice? Well, the team thought so too, and enabled it using Node.js. KuduExec is a command line tool which gives you command line access to your Azure Website.
To install KuduExec, first you’ll need to install Node. Once you have node and npm installed run the following command.
npm install kuduexec –g
After kuduexec installs you can run it from the command line to login. To use KuduExec, type in kuduexec then the scm endpoint for your site (hint: http://<site-name>.scm.azurewebsites.net) then use your deployment credentials to login.
Then you have full access to your Azure Websites site.
Recently I had someone ask me how to connect to the Microsoft Azure Redis Cache Service from Python. I figured it would be easy considering how simple python is to learn. It turned out to be a little trickier than expected but still not too hard nonetheless.
Below is the sample code, then I’ll explain what each part is doing.
To start you will need to install redis-py (or a redis client of your choice) from your favourite package installer, I’m using pip.
pip install redis
There are three things you need to be aware of when connecting to Redis on Azure:
Most of the time when you connect to a redis server it will be on your local machine which is fairly secure because there is no need for an outbound connection to the internet. When connecting to a Cloud Server there are many things that could go wrong in the security department so the Microsoft Azure Redis Cache uses a few things to avoid security issues.
First is a secure connection (SSL), when you connect to Azure you want to ensure that the data going across the wire is encrypted. Second a Password is used to authenticate access with the cache. Finally, the port number has been changed from the default due to the secure connection.
The key when using any Redis library is to ensure that it supports these three things, then once you know that the client supports them, it may still take some investigating to ensure that they are properly enabled when attempting to connect.
When looking at the connection object in python, you’ll notice that SSL is explicitly set to True this is required or you will receive an exception: ConnectionError: Error while reading from socket: (10054, ‘An existing connection was forcibly closed by the remote host’).