Skip to content

Latest commit

 

History

History
283 lines (181 loc) · 13.6 KB

quickstart-php.md

File metadata and controls

283 lines (181 loc) · 13.6 KB
title description ms.assetid ms.topic author ms.author ms.date ms.devlang ms.custom zone_pivot_groups
Quickstart: Create a PHP web app
Deploy your first PHP Hello World to Azure App Service in minutes. You deploy using Git, which is one of many ways to deploy to App Service.
6feac128-c728-4491-8b79-962da9a40788
quickstart
msangapu-msft
msangapu
01/26/2024
php
mode-other, devdivchpfy22, devx-track-azurecli, linux-related-content
app-service-platform-windows-linux

Create a PHP web app in Azure App Service

::: zone pivot="platform-windows"
[!INCLUDE quickstart-php-windows-] ::: zone-end

::: zone pivot="platform-linux" Azure App Service provides a highly scalable, self-patching web hosting service. This quickstart shows how to deploy a PHP app to Azure App Service on Linux.

Screenshot of the sample app running in Azure.

You can follow the steps here using a Mac, Windows, or Linux machine. Once the prerequisites are installed, it takes about five minutes to complete the steps.

To complete this quickstart, you need:

1 - Get the sample repository

You can create the web app using the Azure CLI in Cloud Shell, and you use Git to deploy sample PHP code to the web app.

  1. In a terminal window, run the following commands to clone the sample application to your local machine and navigate to the project root.

    git clone https://github.com/Azure-Samples/php-docs-hello-world
    cd php-docs-hello-world
  2. To run the application locally, use the php command to launch the built-in PHP web server.

    php -S localhost:8080
  3. Browse to the sample application at http://localhost:8080 in a web browser.

    Screenshot of the sample app running locally.

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

  1. In your browser, navigate to the repository containing the sample code.

  2. In the upper right corner, select Fork.

    Screenshot of the Azure-Samples/php-docs-hello-world repo in GitHub, with the Fork option highlighted.

  3. On the Create a new fork screen, confirm the Owner and Repository name fields. Select Create fork.

    Screenshot of the Create a new fork page in GitHub for creating a new fork of Azure-Samples/php-docs-hello-world.

Note

This should take you to the new fork. Your fork URL will look something like this: https://github.com/YOUR_GITHUB_ACCOUNT_NAME/php-docs-hello-world


2 - Deploy your application code to Azure

Azure CLI has a command az webapp up that creates the necessary resources and deploys your application in a single step.

In the terminal, deploy the code in your local folder using the az webapp up command:

az webapp up --runtime "PHP:8.2" --os-type=linux
  • If the az command isn't recognized, be sure you have Azure CLI installed.
  • The --runtime "PHP:8.2" argument creates the web app with PHP version 8.2.
  • The --os-type=linux argument creates the web app on App Service on Linux.
  • You can optionally specify a name with the argument --name <app-name>. If you don't provide one, then a name is automatically generated.
  • You can optionally include the argument --location <location-name> where <location_name> is an available Azure region. You can retrieve a list of allowable regions for your Azure account by running the az account list-locations command.
  • If you see the error, "Could not auto-detect the runtime stack of your app," make sure you're running the command in the code directory (See Troubleshooting auto-detect issues with az webapp up).

The command can take a few minutes to complete. While it's running, it provides messages about creating the resource group, the App Service plan, and the app resource, configuring logging, and doing ZIP deployment. It then gives the message, "You can launch the app at http://<app-name>.azurewebsites.net", which is the app's URL on Azure.

The webapp '<app-name>' doesn't exist
Creating Resource group '<group-name>' ...
Resource group creation complete
Creating AppServicePlan '<app-service-plan-name>' ...
Creating webapp '<app-name>' ...
Configuring default logging for the app, if not already enabled
Creating zip with contents of dir /home/msangapu/myPhpApp ...
Getting scm site credentials for zip deployment
Starting zip deployment. This operation can take a while to complete ...
Deployment endpoint responded with status code 202
You can launch the app at http://<app-name>.azurewebsites.net
{
  "URL": "http://<app-name>.azurewebsites.net",
  "appserviceplan": "<app-service-plan-name>",
  "location": "centralus",
  "name": "<app-name>",
  "os": "linux",
  "resourcegroup": "<group-name>",
  "runtime_version": "php|8.2",
  "runtime_version_detected": "0.0",
  "sku": "FREE",
  "src_path": "//home//msangapu//myPhpApp"
}

[!include az webapp up command note]

Browse to the deployed application in your web browser at the URL http://<app-name>.azurewebsites.net.

  1. Sign into the Azure portal.

  2. At the top of the portal, type app services in the search box. Under Services, select App Services.

    Screenshot of the Azure portal with 'app services' typed in the search text box. In the results, the App Services option under Services is highlighted.

  3. In the App Services page, select + Create.

  4. In the Basics tab:

    • Under Resource group, select Create new. Type myResourceGroup for the name.
    • Under Name, type a globally unique name for your web app.
    • Under Publish, select Code.
    • Under Runtime stack select PHP 8.2.
    • Under Operating System, select Linux.
    • Under Region, select an Azure region close to you.
    • Under App Service Plan, create an app service plan named myAppServicePlan.
    • Under Pricing plan, select Free F1.

    :::image type="content" source="./media/quickstart-php/app-service-details-php.png" lightbox="./media/quickstart-php/app-service-details-php.png" alt-text="Screenshot of new App Service app configuration for PHP in the Azure portal.":::

  5. Select the Deployment tab at the top of the page.

  6. Under GitHub Actions settings, set Continuous deployment to Enable.

  7. Under GitHub Actions details, authenticate with your GitHub account, and select the following options:

    • For Organization select the organization where you forked the demo project.
    • For Repository select the php-docs-hello-world project.
    • For Branch select master.

    :::image type="content" source="media/quickstart-php/app-service-deploy-php.png" lightbox="media/quickstart-php/app-service-deploy-php.png" border="true" alt-text="Screenshot of the deployment options for a PHP app.":::

    [!NOTE] By default, the creation wizard disables basic authentication and GitHub Actions deployment is created using a user-assigned identity. If you get a permissions error during resource creation, your Azure account might not have enough permissions. You can configure GitHub Actions deployment later with an identity generated for you by an Azure administrator, or you can also enable basic authentication instead.

  8. Select the Review + create button at the bottom of the page.

  9. After validation runs, select the Create button at the bottom of the page.

  10. After deployment is completed, select Go to resource.

  11. Browse to the deployed application in your web browser at the URL http://<app-name>.azurewebsites.net.


The PHP sample code is running in an Azure App Service.

Screenshot of the sample app running in Azure, showing 'Hello World!'.

Congratulations! You deployed your first PHP app to App Service using the Azure portal.

3 - Update and redeploy the app

  1. 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!";
  2. Save your changes, then redeploy the app using the az webapp up command again with these arguments:

    az webapp up --runtime "PHP:8.2" --os-type=linux
    
  3. Once deployment is completed, return to the browser window that opened during the Browse to the app step, and refresh the page.

    Screenshot of the updated sample app running in Azure.

  1. Browse to your GitHub fork of php-docs-hello-world.

  2. On your repo page, press . to start Visual Studio Code within your browser.

    Screenshot of the forked php-docs-hello-world repo in GitHub with instructions to press the period key on this screen.

    [!NOTE] The URL will change from GitHub.com to GitHub.dev. This feature only works with repos that have files. This does not work on empty repos.

  3. Edit index.php so that it shows "Hello Azure!" instead of "Hello World!"

    <?php
        echo "Hello Azure!";
    ?>
  4. From the Source Control menu, select the Stage Changes button to stage the change.

    Screenshot of Visual Studio Code in the browser, highlighting the Source Control navigation in the sidebar, then highlighting the Stage Changes button in the Source Control panel.

  5. Enter a commit message such as Hello Azure. Then, select Commit and Push.

    Screenshot of Visual Studio Code in the browser, Source Control panel with a commit message of 'Hello Azure' and the Commit and Push button highlighted.

  6. Once deployment is completed, return to the browser window that opened during the Browse to the app step, and refresh the page.

    Screenshot of the updated sample app running in Azure, showing 'Hello Azure!'.


4 - Manage your new Azure app

  1. Go to the Azure portal to manage the web app you created. Search for and select App Services.

    Screenshot of the Azure portal with 'app services' typed in the search text box. In the results, the App Services option under Services is highlighted.

  2. Select the name of your Azure app.

    Screenshot of the App Services list in Azure. The name of the demo app service is highlighted.

    Your web app's Overview page should be displayed. Here, you can perform basic management tasks like Browse, Stop, Restart, and Delete.

    Screenshot of the App Service overview page in Azure portal. In the action bar, the Browse, Stop, Swap (disabled), Restart, and Delete button group is highlighted.

    The web app menu provides different options for configuring your app.

5 - Clean up resources

When you're finished with the sample app, you can remove all of the resources for the app from Azure. It helps you avoid extra charges and keeps your Azure subscription uncluttered. Removing the resource group also removes all resources in the resource group and is the fastest way to remove all Azure resources for your app.

Delete the resource group by using the az group delete command.

az group delete --name myResourceGroup

This command takes a minute to run.

  1. From your App Service Overview page, select the resource group you created.

  2. From the resource group page, select Delete resource group. Confirm the name of the resource group to finish deleting the resources.


::: zone-end

Next steps

[!div class="nextstepaction"] PHP with MySQL

[!div class="nextstepaction"] Configure PHP app

[!div class="nextstepaction"]  Secure with custom domain and certificate