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 |
::: 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.
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:
- An Azure account with an active subscription. Create an account for free.
- Git
- PHP
- Azure CLI to run commands in any shell to create and configure Azure resources.
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.
-
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
-
To run the application locally, use the
php
command to launch the built-in PHP web server.php -S localhost:8080
-
Browse to the sample application at
http://localhost:8080
in a web browser. -
In your terminal window, press Ctrl+C to exit the web server.
-
In your browser, navigate to the repository containing the sample code.
-
In the upper right corner, select Fork.
-
On the Create a new fork screen, confirm the Owner and Repository name fields. Select Create fork.
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
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 theaz 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
.
-
Sign into the Azure portal.
-
At the top of the portal, type app services in the search box. Under Services, select App Services.
-
In the App Services page, select + Create.
-
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.":::
-
Select the Deployment tab at the top of the page.
-
Under GitHub Actions settings, set Continuous deployment to Enable.
-
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.
-
Select the Review + create button at the bottom of the page.
-
After validation runs, select the Create button at the bottom of the page.
-
After deployment is completed, select Go to resource.
-
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.
Congratulations! You deployed your first PHP app to App Service using the Azure portal.
-
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 toecho
:echo "Hello Azure!";
-
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
-
Once deployment is completed, return to the browser window that opened during the Browse to the app step, and refresh the page.
-
Browse to your GitHub fork of php-docs-hello-world.
-
On your repo page, press
.
to start Visual Studio Code within your browser.[!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.
-
Edit index.php so that it shows "Hello Azure!" instead of "Hello World!"
<?php echo "Hello Azure!"; ?>
-
From the Source Control menu, select the Stage Changes button to stage the change.
-
Enter a commit message such as
Hello Azure
. Then, select Commit and Push. -
Once deployment is completed, return to the browser window that opened during the Browse to the app step, and refresh the page.
-
Go to the Azure portal to manage the web app you created. Search for and select App Services.
-
Select the name of your Azure app.
Your web app's Overview page should be displayed. Here, you can perform basic management tasks like Browse, Stop, Restart, and Delete.
The web app menu provides different options for configuring your app.
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.
-
From your App Service Overview page, select the resource group you created.
-
From the resource group page, select Delete resource group. Confirm the name of the resource group to finish deleting the resources.
::: zone-end
[!div class="nextstepaction"] PHP with MySQL
[!div class="nextstepaction"] Configure PHP app
[!div class="nextstepaction"] Secure with custom domain and certificate