Developing software in the Real World

Running PHP applications on Azure App Engine

Azure App Service is a way to host your web application in a container without having to think about the server. It’s the same PaaS concept as AWS Elastic Beanstalk and supports all the main web programming languages. It also supports Windows and Linux OS containers.

I have a client that is moving an on-premises PHP application to App Service and so have been looking at what I needed to do to deploy it there.

To get started, Azure have a couple of great tutorials for Linux and Windows with which I got a basic app running in no time, so I could turn my attention to the specifics I cared about.

Database drivers

My app uses SQL Server, so I was pleased to see that both the Windows and Linux PHP containers have the sqlsrv extension installed. However, there’s a bug today where the Linux container is missing the ODBC driver, so SQL Server support doesn’t actually work on the Linux container. This should be fixed in July apparently.

Both containers also support PostgreSQL and MySQL, so the databases I care for are covered.

Rewriting all URLs to index.php in the public directory

In common with many PHP applications, I have a public directory where the files to be served by the web server are kept and my PHP and other source files are elsewhere. I also use rewriting to map all URLs to my public/index.php file.

The way we handle this depends on the container.

Windows container

On the Windows container, we need to change the path mapping so that the public directory is mapped to the /. This can be done in the web portal in Configuration -> Path mappings where by default the / virtual path is mapped to site\wwwroot physical path. Edit this so that it points to site\wwwroot\public. I’m not a fan of web-based tooling for things like this as it’s error-prone. Azure provides a command line tool, so we can make the same path mapping change with:


(Change {Resource Group} and {App Service} to the correct names for your installation.)

Now that we’re serving from /public, we can now add the rewrite rule. With IIS, we use a web.config file:

/public/web.config:


quot; />
<conditions logicalGrouping=”MatchAll”>
<add input=”{REQUEST_FILENAME}” matchType=”IsFile” negate=”true” />
<add input=”{REQUEST_FILENAME}” matchType=”IsDirectory” negate=”true” />
</conditions>
<action type=”Rewrite” url=”index.php” />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>

Linux container

On Linux, the container runs Apache, so we can rewrite the URL to public/index.php with a single .htaccess file in our root directory:

/.htaccess:


Two rewrite rules are required: firstly we rewrite static files so that the continue to be served by Apache and then we rewrite everything else to index.php.

Environment variables

Environment variables are a common way to set per-installation configuration and this is done vie the portal or the command line:

Summary

Running a PHP application on Azure App Engine with either the Windows or Linux container is much the same as on any other hosting and seems to work well.

One thought on “Running PHP applications on Azure App Engine

  1. I've tested extensively the two approaches and it doesn't work well. The main issue is the very low I/O speed you get in those solutions. If you need to do any kind of file operation, even just storing them and then upload to a CDN it won't work properly. Most SD cards today can outperform (by far) the Azure App Service.
    There's even a MS/Azure page somewhere talking about this limits.

Leave a Reply to TCB13 Cancel reply

Your email address will not be published. Required fields are marked *