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 morning I was playing around with my new RaspberryPI 2 getting ready for the Seattle Mini Maker Faire coming up in September. I followed the instructions which were available online for installing Windows 10 Core. To my dismay, the WindowsiotImageHelper didn’t detect my SD Card reader built into the Mac Book Pro I was using. So I went digging into the install directory for the IoT tools and found that the Image Helper is just using DISM under the covers.
This wasn’t super scary considering I actually prefer using the command line to do things, so I started playing around and was able to see a full list of commands to my disposal including the /Apply-Image switch which surprise surprise will apply the Windows 10 image on the SD card as long as I reference the right physical drive on my system.
Finding a disk is easy to do in Windows, call DISKPART from an elevated command prompt (or a regular command prompt and it will open a new command window with a UAC prompt to run in administrator mode). Then issue a LIST DISK command, look in the list for the properly sized drive for your SD card and note the Physical Drive number.
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 (v18.104.22.168) 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!
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’).