How to Create an Azure Mobile App using PHP

Bringing you here, the most demanding tutorial as how to create PHP web app in Azure. Azure web apps gives highly scalable and self-patching web hosting service. In this tutorial you will learn how to deploy a PHP app to Azure web apps. We’ll be creating the web app using Azure CLI and will use GIT to deploy the sample PHP code to the web app.

The below steps can be used in Mac, Linux, and Windows. Once the essential requirements such as PHP and GIT are installed, it will take about five minutes to complete all the steps.

Get Free Help From Professional PHP Development Company.

You can create a free Azure account if don’t have the Azure subscription before beginning.

Launch Azure Cloud Shell.

The Azure cloud shell is a free bash shell which can be run directly within the Azure account. It has Azure CLI already installed in it and configured so that can be use with your account. Click on the Cloud Shell button on the upper right of the Azure portal in the menu.

The button will launch an interactive shell which can be use to run all the steps told in this topic.

Run az –version to find the version of the CLI you are using.

Download the Sample

In the terminal window, run these following commands to copy the sample app repository to the local machine.

git clone https://github.com/Azure-Samples/php-docs-hello-world

Now change to the directory that contains the sample code.

cd php-docs-hello-world

Run the App Locally.

First run the application locally by opening terminal window and use the PHP command to launch the pre-built PHP web server.

php -S localhost:8080

Open up any web browser and type  the following local host address http://localhost:8080.

You will see a HELLO WORLD message displayed on the screen from the sample app.

Exit the web server by pressing Ctrl+c

Log in to Azure

Use the Azure CLI 2.0 to create the resources required to host the app in Azure. Log in to the Azure subscription by the AZ login command and follow the directions displayed on the screen.

az login

Configure a deployment user

With the AZ web app deployment user command, create the deployment credentials.

A deployment user is necessary for FTP and Local GIT deployment. The use name and password will be different from the Azure subscriptions credentials.

az webapp deployment user set –user-name <username> –password <password>

Replace the username and password with new one. The name must be unique and the password must be eight characters long.

It is a one time process and the deployment user created can be used for all Azure deployments.

Create a Resource Group

With the AZ group create command, create a resource group.

A resource group is actually a container which posses Azure resources such as web apps, databases and stored accounts to manage and deploy.

az group create –name myResourceGroup –location westeurope

Create the resource by following the above example.

Generally the resources created in a region near you. Run the az appservice list-location command to see the supported locations for the Azure web app.

Create an Azure App Service Plan

With the az appservice plan create command, create an azure app service.

The app service plan specifies the location, features and size of the web server.

App Service plans define:

  • Region (for example: North Europe, East US, or Southeast Asia)
  • Instance size (small, medium, or large)
  • Scale count (1 to 20 instances)
  • SKU (Free, Shared, Basic, Standard, or Premium)

The below example creates an app service plan in free pricing.

az appservice plan create –name myAppServicePlan –resource-group myResourceGroup –sku FREE

After the app service plan be created, the AZURE CLI will show some information like this

{

 "adminSiteName": null,

 "appServicePlanName": "myAppServicePlan",

 "geoRegion": "West Europe",

 "hostingEnvironmentProfile": null,

 "id": "/subscriptions/0000-0000/resourceGroups/myResourceGroup/providers/Microsoft.Web/serverfarms/myAppServicePlan",

 "kind": "app",

 "location": "West Europe",

 "maximumNumberOfWorkers": 1,

 "name": "myAppServicePlan",

 < JSON data removed for brevity. >

 "targetWorkerSizeId": 0,

 "type": "Microsoft.Web/serverfarms",

 "workerTierName": null

}

 

Create a Web App

With the AZ webapp create command, create a web app in the app service plan.

The web app will provide a hosting space for the code and a URL to view the deployed app.

az webapp create –name <app_name> –resource-group myResourceGroup –plan myAppServicePlan

In the above command, replace the app name with some unique name.

Once the web app is created, the Azure CLI will show below information.

{

 "availabilityState": "Normal",

 "clientAffinityEnabled": true,

 "clientCertEnabled": false,

 "cloningInfo": null,

 "containerSize": 0,

 "dailyMemoryTimeQuota": 0,

 "defaultHostName": "<app_name>.azurewebsites.net",

 "enabled": true,

 "enabledHostNames": [

   "<app_name>.azurewebsites.net",

   "<app_name>.scm.azurewebsites.net"

 ],

 "gatewaySiteName": null,

 "hostNameSslStates": [

   {

     "hostType": "Standard",

     "name": "<app_name>.azurewebsites.net",

     "sslState": "Disabled",

     "thumbprint": null,

     "toUpdate": null,

     "virtualIp": null

   }

   < JSON data removed for brevity. >

}

 

Type the following address on any browser to check your newly created web app.

http://<app_name>.azurewebsites.net

A web app in Azure has been created.

Configure local GIT Deployment

With the az webapp deployment source configuration-git command, configure local Git deployment to the web app.

For quickstart, deploy by using local Git. To deploy content to web app, app service supports offers several ways such as FTP, Local Git, GitHub and  Bitbucket

az webapp deployment source config-local-git –name <app_name> –resource-group myResourceGroup –query url –output tsv

Replace app name with the web app’s name in the above command.

The output format will be :

https://<username>@<app_name>.scm.azurewebsites.net:443/<app_name>.git

The username is the deployment username which you have created previously. Save the above URL to use in the next steps.

Push to Azure from Git

git remote add azure <URI from previous step>

Add remote Azure to the local GIT repository,

Push to the Azure remote to deploy the app. Ensures the password you entered is the deployment user password not the password you used to log in to the Azure portal.

git push azure master

The above command will display following information.

Counting objects: 2, done.

Delta compression using up to 4 threads.

Compressing objects: 100% (2/2), done.

Writing objects: 100% (2/2), 352 bytes | 0 bytes/s, done.

Total 2 (delta 1), reused 0 (delta 0)

remote: Updating branch 'master'.

remote: Updating submodules.

remote: Preparing deployment for commit id '25f18051e9'.

remote: Generating deployment script.

remote: Running deployment command...

remote: Handling Basic Web Site deployment.

remote: Kudu sync from: '/home/site/repository' to: '/home/site/wwwroot'

remote: Copying file: '.gitignore'

remote: Copying file: 'LICENSE'

remote: Copying file: 'README.md'

remote: Copying file: 'index.php'

remote: Ignoring: .git

remote: Finished successfully.

remote: Running post deployment command(s)...

remote: Deployment successful.

To https://<app_name>.scm.azurewebsites.net/<app_name>.git

  cc39b1e..25f1805  master -> master

 

Browse to the App

Type the below URL to browse the deployed application on any web browser.

http://<app_name>.azurewebsites.net

Congratulations! you have deployed the PHP app to App service.

Moiz Khan

Share
Published by
Moiz Khan