Introduction to MySQL & PHP on Azure App Services (WIMP)

As there doesn’t seem to be masses of info on how PHP works on Azure App Services, I figured I’d do something around on the subject now having played around with it a bit.

PHP has been an option for an Azure web-app runtime for a while now as you may have noticed already:

Azure App Services

That’ll give you IIS with PHP configured through FastCGI. Read more about how it’s setup @ /en-us/azure/app-service-web/web-sites-php-configure if you want.

There is a Linux option too for full LAMP as a service, but I’ve not looked in it yet.

What is Azure App Services?

If you’re wondering what app services even are, here’s my own, brief explanation with the official explanation here.

App Services is “platform as a service”, for anything web-server related. What that means in reality is you don’t have to worry about managing the underlying machines that run your code; we (the Azure guys) are responsible for that, and you just focus on your code. It’ll “just work”!

More specifically, we’ll handle patching, uptime, load-balancing, core OS configuration, etc, etc; you just have to make sure you’ve configured your “app service” the way you want – what code is on it, what logging & monitoring you want to see.

For performance vs cost, you say how much it needs to scale vs how much money you want to spend. There’s all sorts of options for scaling automatically, manually, on a schedule, etc, etc. Free is an option, but obviously that’ll go rather slow if anyone actually hits the website in any real amount.

MySQL in App

Something available in App Services is something called “MySQL in App” which is a cost-effective way of getting MySQL running. Here it is:

Azure App Services

I say cost-effective because of how MySQL runs, which is basically within the same virtual-machine that also runs the PHP code, so you don’t have to pay any more for a database tier.

Obviously, this limits scalability quite significantly; you’re sharing the same CPU & memory with IIS/PHP, but it is cheap. Free even, if you run the free service-tier.

Want to scale-up? You can offload the MySQL part to Azure Database for MySQL and scale as much as you want. Azure Database is “database as a platform”, and now there’s a MySQL flavour too.

Running MySQL under IIS

So, the first thing to realise about MySQL in App is it runs within the context of IIS itself, hence the “in app” part.

Here we see a MySQL log-file from a MySQL process that’s just been spun-up:

Azure App Services

Note the process ID of MySQL…

Now if we check out the environment process-explorer in Kudu (available under the “advanced tools” blade in the portal):

Azure App Services

Also notice “mysqld.exe” is running under w3wp.exe (IIS worker process).

This is fairly significant; it means that is we have no IIS worker-process, we have no database in the “MySQL in App” setup. That in turns means, if nobody’s hitting the site, we have no database running! See below for why & how.

Also worth noting, and this is a potentially big deal for production sites especially, the “free” tier in app-services doesn’t give you the App-Services “always on” setting. This means IIS will eventually unload itself after a certain amount of activity…unloading your database engine with it.

That is until someone hits the site anyway, which will trigger a new worker-process and start-up another MySQL instance too.

phpMyAdmin on App Services

phpMyAdmin is a very popular admin tool for MySQL and if you’re running “MySQL in App” then you actually already have it installed, despite phpMyAdmin being an extension you can install.

Click on the “MySQL In App” blade and you’ll see the config + a link to the pre-installed phpMyAdmin via the “manage” button.

Azure App Services

Clicking that will open phpMyAdmin. It’ll open on the admin “SCM” URL for the website (i.e. be secured the same way Azure portal is).

Interestingly, if you’ve not hit the website yet & spawned an IIS worker-process then MySQL won’t be running, and you’ll hit a connection error:

Azure App Services

Hit the website; that’ll spawn a worker-process + MySQL, and then phpMyAdmin should now work.

Azure App Services

Pro-tip: don’t install the phpMyAdmin extension if you have the “MySQL in App” option already. Seriously don’t; you’ll kill the nicely pre-configured instance.

Connection String to MySQL

Where’s the connection-string for PHP then? Having played around with the WordPress App Services template, this is where it seems to be:

Azure App Services

Opening the file & here’s the string:

Azure App Services

Sharper-eyed readers may have noticed above; this connection-string is normally available via environment variable “MYSQLCONNSTR_localdb”, which is what WordPress at least uses for its connections to the database.

Transferring Files into App Services

Finally, as this is a bit of a n00b article, how do we get files on & off the website?

GitHub integration is probably the best way, but if you want a more traditional way too FTP is an option.

Configure deployment credentials first:

Azure App Services

Then, download the publishing profile:

Azure App Services

That’s basically an XML file with various ways of publishing files to the app-service, one of which is the FTP method:

Azure App Services

(and yeah, this was a throwaway app-service, so don’t try and connect :P).

Lo & behold, the server files:

Azure App Services

Upload & download to your hearts’ content!

That’s it for now - cheers!

// Sam Betts