Create a PHP application on Web App

This quickstart tutorial walks through how to develop and deploy a PHP app to Azure. We’ll run the app using an Azure App Service plan, and create and configure a new Web App within it using the Azure CLI. We’ll then use git to deploy our PHP app to Azure.

hello-world-in-browser

You can follow the steps below using a Mac, Windows, or Linux machine. It should take you only about 5 minutes to complete all of the steps below.

Prerequisites

Before creating this sample, download and install the following:

If you don't have an Azure subscription, create a free account before you begin.

Download the sample

Clone the Hello World sample app repository to your local machine.

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

Change to the directory that contains the sample code.

cd php-docs-hello-world

Run the app locally

Run the application locally by opening a terminal window an using php command line for the sample to launch the built in PHP web server.

php -S localhost:8080

Open a web browser, and navigate to the sample.

http://localhost:8080

You can see the Hello World message from the sample app displayed in the page.

localhost-hello-world-in-browser

In your terminal window, press Ctrl+C to exit the web server.

Log in to Azure

We are now going to use the Azure CLI 2.0 in a terminal window to create the resources needed to host our PHP app in Azure. Log in to your Azure subscription with the az login command and follow the on-screen directions.

az login

Configure a deployment user

For FTP and local Git, you must have a deployment user configured on the server to authenticate your deployment.

Note

A deployment user is required for FTP and Local Git deployment to a Web App. The username and password are account-level. They are different from your Azure Subscription credentials.

Run the az appservice web deployment user set command to create your deployment credentials.

az appservice web deployment user set --user-name <username> --password <password>

The username must be unique and the password must be strong. If you get a 'Conflict'. Details: 409 error, change the username. If you get a 'Bad Request'. Details: 400 error, use a stronger password.

You only need to create this deployment user once; you can use it for all your Azure deployments.

Record the username and password, as they are used later in a later step when you deploy the app.

Create a resource group

Create a resource group with the az group create. An Azure resource group is a logical container into which Azure resources like web apps, databases and storage accounts are deployed and managed.

az group create --name myResourceGroup --location westeurope

Create an Azure App Service plan

Create a "FREE" App Service plan with the az appservice plan create command.

An App Service plan specifies the location, size, and features of the web server farm that hosts your app. You can save money when hosting multiple apps by configuring the web apps to share a single App Service plan.

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 following example creates an App Service plan named quickStartPlan using the Free pricing tier.

az appservice plan create --name quickStartPlan --resource-group myResourceGroup --sku FREE

When the App Service Plan has been created, the Azure CLI shows information similar to the following example.

{
    "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.Web/serverfarms/quickStartPlan",
    "location": "West Europe",
    "sku": {
    "capacity": 1,
    "family": "S",
    "name": "S1",
    "tier": "Standard"
    },
    "status": "Ready",
    "type": "Microsoft.Web/serverfarms"
}

Create a web app

Now that an App Service plan has been created, create a Web App within the quickStartPlan App Service plan. The web app gives us a hosting space to deploy our code as well as provides a URL for us to view the deployed application. Use the az appservice web create command to create the Web App.

In the command below substitute your own unique app name where you see the <app_name> placeholder. The <app_name> is used in the default DNS site for the web app. If <app_name> is not unique, you get the friendly error message "Website with given name already exists."

az appservice web create --name <app_name> --resource-group myResourceGroup --plan quickStartPlan

When the Web App has been created, the Azure CLI shows information similar to the following example.

{
    "clientAffinityEnabled": true,
    "defaultHostName": "<app_name>.azurewebsites.net",
    "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.Web/sites/<app_name>",
    "isDefaultContainer": null,
    "kind": "app",
    "location": "West Europe",
    "name": "<app_name>",
    "repositorySiteName": "<app_name>",
    "reserved": true,
    "resourceGroup": "myResourceGroup",
    "serverFarmId": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.Web/serverfarms/quickStartPlan",
    "state": "Running",
    "type": "Microsoft.Web/sites",
}

Browse to the site to see your newly created Web App.

http://<app_name>.azurewebsites.net

app-service-web-service-created

We’ve now created an empty new Web App in Azure.

Configure local git deployment

You can deploy to your Web App in a variety of ways including FTP, local Git as well as GitHub, Visual Studio Team Services, and Bitbucket.

Use the az appservice web source-control config-local-git command to configure local git access to the Web App.

az appservice web source-control config-local-git --name <app_name> --resource-group myResourceGroup --query url --output tsv

Copy the output from the terminal as it will be used in the next step.

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

Push to Azure from Git

Add an Azure remote to your local Git repository.

git remote add azure <paste-previous-command-output-here>

Push to the Azure remote to deploy your app. You are prompted for the password you supplied earlier when you created the deployment user. Make sure that you enter the password you created in Configure a deployment user, not the password you use to log in to the Azure portal.

git push azure master

During deployment, Azure App Service will communicate its progress with Git.

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

Browse to the deployed application using your web browser.

http://<app_name>.azurewebsites.net

This time, the page that displays the Hello World message is running using our PHP code running as an Azure App Service web app.

Updating and Deploying the Code

Using a local text editor, open the index.php file within the PHP app, and make a small change to the text within the string next to echo:

echo "Hello Azure!";

Commit your changes in git, then push the code changes to Azure.

git commit -am "updated output"
git push azure master

Once deployment has completed, switch back to the browser window that opened in the Browse to the app step, and hit refresh.

hello-world-in-browser

Manage your new Azure web app

Go to the Azure portal to take a look at the web app you just created.

To do this, sign in to https://portal.azure.com.

From the left menu, click App Services, then click the name of your Azure web app.

Portal navigation to Azure web app

You have landed in your web app's blade (a portal page that opens horizontally).

By default, your web app's blade shows the Overview page. This page gives you a view of how your app is doing. Here, you can also perform basic management tasks like browse, stop, start, restart, and delete. The tabs on the left side of the blade shows the different configuration pages you can open.

App Service blade in Azure portal

These tabs in the blade show the many great features you can add to your web app. The following list gives you just a few of the possibilities:

  • Map a custom DNS name
  • Bind a custom SSL certificate
  • Configure continuous deployment
  • Scale up and out
  • Add user authentication

Congratulations! You've deployed your first PHP app to App Service.

Clean up resources

To clean up your resources, run the following command:

az group delete --name myResourceGroup