Tutorial: Deploy a PHP and Azure Database for MySQL - Flexible Server app on Azure App Service
APPLIES TO: Azure Database for MySQL - Flexible Server
Azure App Service provides a highly scalable, self-patching web hosting service using the Linux operating system.
This tutorial shows how to build and deploy a sample PHP application to Azure App Service, and integrate it with Azure Database for MySQL flexible server on the back end. Here you'll use public access connectivity (allowed IP addresses) in the Azure Database for MySQL flexible server instance to connect to the App Service app.
In this tutorial, you'll learn how to:
- Create an Azure Database for MySQL flexible server instance
- Connect a PHP app to the Azure Database for MySQL flexible server instance
- Deploy the app to Azure App Service
- Update and redeploy the app
If you don't have an Azure subscription, create an Azure free account before you begin. Currently, with an Azure free account, you can try Azure Database for MySQL - Flexible Server free for 12 months. For more information, see Try Azure Database for MySQL - Flexible Server for free.
Prerequisites
- Install Git.
- The Azure Command-Line Interface (CLI).
- An Azure subscription If you don't have an Azure subscription, create an Azure free account before you begin. Currently, with an Azure free account, you can try Azure Database for MySQL - Flexible Server free for 12 months. For more information, see Try Azure Database for MySQL - Flexible Server for free.
Create an Azure Database for MySQL - Flexible Server
First, we'll provision an Azure Database for MySQL flexible server instance with public access connectivity, configure firewall rules to allow the application to access the server, and create a production database.
To learn how to use private access connectivity instead and isolate app and database resources in a virtual network, see Tutorial: Connect an App Services Web app to an Azure Database for MySQL flexible server instance in a virtual network.
Create a resource group
An Azure resource group is a logical group in which Azure resources are deployed and managed. Let's create a resource group rg-php-demo using the az group create command in the centralus location.
- Open command prompt.
- Sign in to your Azure account.
az login
- Choose your Azure subscription.
az account set -s <your-subscription-ID>
- Create the resource group.
az group create --name rg-php-demo --location centralus
Create an Azure Database for MySQL flexible server instance
To create an Azure Database for MySQL flexible server instance with public access connectivity, run the following az flexible-server create command. Replace your values for server name, admin username, and password.
az mysql flexible-server create \ --name <your-mysql-server-name> \ --resource-group rg-php-demo \ --location centralus \ --admin-user <your-mysql-admin-username> \ --admin-password <your-mysql-admin-password>
You’ve now created an Azure Database for MySQL flexible server instance in the CentralUS region. The server is based on the Burstable B1MS compute SKU, with 32 GB storage, a 7-day backup retention period, and configured with public access connectivity.
Next, to create a firewall rule for your Azure Database for MySQL flexible server instance to allow client connections, run the following command. When both starting IP and end IP are set to 0.0.0.0, only other Azure resources (like App Services apps, VMs, AKS cluster, etc.) can connect to the Azure Database for MySQL flexible server instance.
az mysql flexible-server firewall-rule create \ --name <your-mysql-server-name> \ --resource-group rg-php-demo \ --rule-name AllowAzureIPs \ --start-ip-address 0.0.0.0 \ --end-ip-address 0.0.0.0
To create a new Azure Database for MySQL flexible server production database sampledb to use with the PHP application, run the following command:
az mysql flexible-server db create \ --resource-group rg-php-demo \ --server-name <your-mysql-server-name> \ --database-name sampledb
Build your application
For the purposes of this tutorial, we'll use a sample PHP application that displays and manages a product catalog. The application provides basic functionalities like viewing the products in the catalog, adding new products, updating existing item prices and removing products.
To learn more about the application code, go ahead and explore the app in the GitHub repository. To learn how to connect a PHP app to Azure Database for MySQL flexible server, refer Quickstart: Connect using PHP.
In this tutorial, we'll directly clone the coded sample app and learn how to deploy it on Azure App Service.
To clone the sample application repository and change to the repository root, run the following commands:
git clone https://github.com/Azure-Samples/php-mysql-app-service.git cd php-mysql-app-service
Run the following command to ensure that the default branch is
main
.git branch -m main
Create and configure an Azure App Service Web App
In Azure App Service (Web Apps, API Apps, or Mobile Apps), an app always runs in an App Service plan. An App Service plan defines a set of compute resources for a web app to run. In this step, we'll create an Azure App Service plan and an App Service web app within it, which will host the sample application.
To create an App Service plan in the Free pricing tier, run the following command:
az appservice plan create --name plan-php-demo \ --resource-group rg-php-demo \ --location centralus \ --sku FREE --is-linux
If you want to deploy an application to Azure web app using deployment methods like FTP or Local Git, you need to configure a deployment user with username and password credentials. After you configure your deployment user, you can take advantage of it for all your Azure App Service deployments.
az webapp deployment user set \ --user-name <your-deployment-username> \ --password <your-deployment-password>
To create an App Service web app with PHP 8.0 runtime and to configure the Local Git deployment option to deploy your app from a Git repository on your local computer, run the following command. Replace
<your-app-name>
with a globally unique app name (valid characters are a-z, 0-9, and -).az webapp create \ --resource-group rg-php-demo \ --plan plan-php-demo \ --name <your-app-name> \ --runtime "PHP|8.0" \ --deployment-local-git
Important
In the Azure CLI output, the URL of the Git remote is displayed in the deploymentLocalGitUrl property, with the format
https://<username>@<app-name>.scm.azurewebsites.net/<app-name>.git
. Save this URL, as you'll need it later.Next we'll configure the Azure Database for MySQL flexible server database connection settings on the Web App.
The
config.php
file in the sample PHP application retrieves the database connection information (server name, database name, server username and password) from environment variables using thegetenv()
function. In App Service, to set environment variables as Application Settings (appsettings), run the following command:az webapp config appsettings set \ --name <your-app-name> \ --resource-group rg-php-demo \ --settings DB_HOST="<your-server-name>.mysql.database.azure.com" \ DB_DATABASE="sampledb" \ DB_USERNAME="<your-mysql-admin-username>" \ DB_PASSWORD="<your-mysql-admin-password>" \ MYSQL_SSL="true"
Alternatively, you can use Service Connector to establish a connection between the App Service app and the Azure Database for MySQL flexible server instance. For more details, see Integrate Azure Database for MySQL flexible server with Service Connector.
Deploy your application using Local Git
Now, we'll deploy the sample PHP application to Azure App Service using the Local Git deployment option.
Since you're deploying the main branch, you need to set the default deployment branch for your App Service app to main. To set the DEPLOYMENT_BRANCH under Application Settings, run the following command:
az webapp config appsettings set \ --name <your-app-name> \ --resource-group rg-php-demo \ --settings DEPLOYMENT_BRANCH='main'
Verify that you are in the application repository's root directory.
To add an Azure remote to your local Git repository, run the following command. Replace
<deploymentLocalGitUrl>
with the URL of the Git remote that you saved in the Create an App Service web app step.git remote add azure <deploymentLocalGitUrl>
To deploy your app by performing a
git push
to the Azure remote, run the following command. When Git Credential Manager prompts you for credentials, enter the deployment credentials that you created in Configure a deployment user step.git push azure main
The deployment may take a few minutes to succeed.
Test your application
Finally, test the application by browsing to https://<app-name>.azurewebsites.net
, and then add, view, update or delete items from the product catalog.
Congratulations! You have successfully deployed a sample PHP application to Azure App Service and integrated it with Azure Database for MySQL flexible server on the back end.
Update and redeploy the app
To update the Azure app, make the necessary code changes, commit all the changes in Git, and then push the code changes to Azure.
git add .
git commit -m "Update Azure app"
git push azure main
Once the git push
is complete, navigate to or refresh the Azure app to test the new functionality.
Clean up resources
In this tutorial, you created all the Azure resources in a resource group. If you don't expect to need these resources in the future, delete the resource group by running the following command in the Cloud Shell:
az group delete --name rg-php-demo