/WebApps-PHP-MySQL-Redirect

Setting up the Redirect functionality for Azure MySQL/MariaDB and PHP on Azure Web Apps. Using the mysqlnd_azure driver.

Primary LanguageShell

Azure Redirect for Azure MySQL/MariaDB on Linux Azure Web Apps PHP 7.4 without Docker

This approach is using the mysqlnd_azure.so extension to enable the redirection functionality on Azure MySQL/MariaDB instance.

  1. Build the mysqlnd_azure.so binary with make (using Docker container or locally). You can follow these steps.

    # Using the appsvc/php:7.4-apache_20200522.6 Docker image.
    apt install git -y
    git clone https://github.com/microsoft/mysqlnd_azure --depth 1
    cd mysqlnd_azure
    phpize
    ./configure
    make
    # If successful, the binary will be in ./modules/mysqlnd_azure.so.
    1. Extract the binary from the container (if using Docker): docker cp <container_id>:/home/mysqlnd_azure/modules/mysqlnd_azure.so <dest_path>.
  2. Add the newly created binary to your project (for instance to a ./bin folder).

  3. Create a configuration INI file (for instance in an ./ini folder).

  4. Add the following to the file:

    extension=/home/site/wwwroot/bin/mysqlnd_azure.so
    mysqlnd_azure.enableRedirect = on
  5. Change the INI scan directory for your Web App.

    az webapp config appsettings set --name <app-name> --resource-group <resource-group-name> --settings PHP_INI_SCAN_DIR="/usr/local/etc/php/conf.d:/home/site/wwwroot/ini"

    Note: This is a simplistic approach. Microsoft documentation recommends creating the ini folder outside of wwwroot using SSH.

  6. Push to the repo and deploy the app.

  7. Don't fogert to change the redirect_enabled setting to ON on you Azure MySQL/MariaDB instance.

The directory structure then can look like this:

|- bin
  |-mysqlnd_azure.so
|- ini
  |-mysqlnd_setting.ini
|- index.php

Finally, test the connection via PHP:

<?php
// Based on: https://docs.microsoft.com/azure/mariadb/howto-redirection

$host = getenv("DB_HOST");
$username = getenv("DB_USERNAME");
$password = getenv("DB_PASSWORD");
$db_name = getenv("DB_NAME");

echo "mysqlnd_azure.enableRedirect: ", ini_get("mysqlnd_azure.enableRedirect"), "\n";
$db = mysqli_init();

//The connection must be configured with SSL for redirection test
$link = mysqli_real_connect ($db, $host, $username, $password, $db_name, 3306, NULL, MYSQLI_CLIENT_SSL);

if (!$link) {
    die ('Connect error (' . mysqli_connect_errno() . '): ' . mysqli_connect_error() . "\n");
}
else {
    echo $db->host_info, "\n"; //if redirection succeeds, the host_info will differ from the hostname you used used to connect
    $res = $db->query('SHOW TABLES;'); //test query with the connection
    print_r ($res);
    $db->close();
}
?>