Quickstart: Deploy a Python (Django, Flask, or FastAPI) web app to Azure App Service
Artikkeli
Note
Starting June 1, 2024, all newly created App Service apps will have the option to generate a unique default hostname using the naming convention <app-name>-<random-hash>.<region>.azurewebsites.net. Existing app names will remain unchanged.
In this quickstart, you deploy a Python web app (Django, Flask, or FastAPI) to Azure App Service. Azure App Service is a fully managed web hosting service that supports Python apps hosted in a Linux server environment.
This article contains current instructions on deploying a Python web app using Azure App Service. Python on Windows is no longer supported.
Sample application
This quickstart can be completed using either Flask, Django, or FastAPI. A sample application in each framework is provided to help you follow along with this quickstart. Download or clone the sample application to your local workstation.
To host your application in Azure, you need to create an Azure App Service web app in Azure. You can create a web app using the Azure CLI, VS Code, Azure Tools extension pack, or the Azure portal.
Create the webapp and other resources, then deploy your code to Azure using az webapp up.
az webapp up --runtime PYTHON:3.9 --sku B1 --logs
The --runtime parameter specifies what version of Python your app is running. This example uses Python 3.9. To list all available runtimes, use the command az webapp list-runtimes --os linux --output table.
The --sku parameter defines the size (CPU, memory) and cost of the app service plan. This example uses the B1 (Basic) service plan, which will incur a small cost in your Azure subscription. For a full list of App Service plans, view the App Service pricing page.
The --logs flag configures default logging required to enable viewing the log stream immediately after launching the webapp.
You can optionally specify a name with the argument --name <app-name>. If you don't provide one, then a name will be 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 appservice list-locations command.
The command may take a few minutes to complete. While the command is 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/cephas/myExpressApp ...
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": "<os-type>",
"resourcegroup": "<group-name>",
"runtime_version": "python|3.9",
"runtime_version_detected": "0.0",
"sku": "FREE",
"src_path": "<your-folder-location>"
}
Note
The az webapp up command does the following actions:
Cache the parameters locally in the .azure/config file so that you don't need to specify them again when deploying later with az webapp up or other az webapp commands from the project folder. The cached values are used automatically by default.
To create Azure resources in VS Code, you must have the Azure Tools extension pack installed and be signed into Azure from VS Code.
Locate the Azure icon in the left-hand toolbar. Select it to bring up the Azure Tools for VS Code extension.
If you do not see the Azure Tools icon, make sure you have the Azure Tools extension for VS Code installed.
In the Azure Tools extension for VS Code:
Find the RESOURCES section and select your subscription.
Select + (Create Resource...)
Choose the Create App Service Web App... option.
Enter the name msdocs-python-webapp-quickstart-XYZ for this web app, where XYZ is any three unique characters.
When deployed, this name is used as your app name in the form https://<app-name>.azurewebsites.net.
Select the runtime stack for the application. In this example, select Python 3.9.
Select the App Service plan (pricing tier) for this web app. The App Service plan controls how many resources (CPU/memory) are available to your app and how much you pay.
For this example, select the Basic (B1) pricing tier. This plan will incur a small charge against your Azure subscription but is recommended for better performance over the Free (F1) tier.
Select the Deploy button in the "Created new web app" notification.
Select the quickstart folder you are working in as the one to deploy.
Answer Yes to update your build configuration and improve deployment performance.
When the deployment is complete, a notification will appear in the lower right corner of VS Code. You can use this notification to browse to your web app.
Sign in to the Azure portal and follow these steps to create your Azure App Service resources.
Instructions
Screenshot
In the Azure portal:
Enter app services in the search bar at the top of the Azure portal.
Select the item labeled App Services under the Services heading on the menu that appears below the search bar.
On the App Services page, select + Create, then select + Web App from the drop-down menu.
On the Create Web App page, fill out the form as follows.
Resource Group → Select Create new and use a name of msdocs-python-webapp-quickstart.
Name → msdocs-python-webapp-quickstart-XYZ where XYZ is any three random characters. This name must be unique across Azure.
Runtime stack → Python 3.9.
Region → Any Azure region near you.
App Service Plan → Under Pricing plan, select Explore pricing plans to select a different App Service plan.
The App Service plan controls the amount of resources (CPU/memory) that are available to your app and the cost of those resources.
For this example, under Dev/Test, select the Basic B1 plan. The Basic B1 plan will incur a small charge against your Azure account but is recommended for better performance over the Free F1 plan.
When finished, select Select to apply your changes.
On the main Create Web App page, select the Review + create at the bottom of the screen.
This will take you to the Review page. Select Create to create your App Service.
Azure App Service supports multiple methods to deploy your application code to Azure, including GitHub Actions and all major CI/CD tools. This article focuses on how to deploy your code from your local workstation to Azure.
Since the az webapp up command created the necessary resources and deployed your application in a single step, you can move on to the next step.
Since the previous step created the necessary resources and deployed your application in a single step, you can move on to the next step.
You can deploy your application code from a local Git repository to Azure by configuring a Git remote in your local repo that points at the repo you want to push code to. The URL of the remote repository and Git credentials needed for configuration can be retrieved using either the Azure portal or the Azure CLI.
Enter the name of your App Service in the search box at the top of the screen.
Under the Resources heading, select the App Service to navigate to it.
On the page for the App Service:
Select Deployment Center from the menu on the left side of the screen.
Select Local Git in the dropdown list labeled Source.
Select Save.
After you save the page, it will refresh and display the address for the remote Git repository.
Copy the value of the Git Clone Uri as this value will be used to set up a Git remote in a later step.
On the Deployment Center page:
Navigate to the Local Git/FTPS credentials tab.
Locate the local git username and password under the Application scope credentials.
Keep this screen open so you can copy these credentials momentarily when you deploy your code to the remote repository. Be sure to copy the local git username, which starts with a $, for example $msdocs-python-webapp-quickstart-123.
When you push code to the remote Git repository for the first time, these credentials are needed to authenticate to the remote repository.
Next, in the root directory of your application, configure a Git remote that points to Azure using the Git URL of the Azure remote obtained in a previous step.
git remote add azure <git-deployment-url>
You can now push code from your local Git repository to Azure using the Git remote you just configured. The default deployment branch for App Service is master, but many Git repositories are moving away from master and using main. You can either specify the mapping from local branch name to remote branch name in the push (as shown below), or you can configure your DEPLOYMENT_BRANCH app setting.
git push azure main:master
The first time you push code to Azure, Git will prompt you for the Azure deployment credentials you obtained in the previous step. Git will then cache these credentials so you won't have to reenter them on subsequent deployments.
First, configure the deployment source for your web app to be local Git using the az webapp deployment source command. This command will output the URL of the remote Git repository that you'll be pushing code to. Make a copy of this value as you'll need it in a later step.
# Change these values to the ones used to create the App Service.
RESOURCE_GROUP_NAME='msdocs-python-webapp-quickstart'
APP_SERVICE_NAME='msdocs-python-webapp-quickstart-123'
az webapp deployment source config-local-git \
--name $APP_SERVICE_NAME \
--resource-group $RESOURCE_GROUP_NAME \
--output tsv
# Change these values to the ones used to create the App Service.
$RESOURCE_GROUP_NAME='msdocs-python-webapp-quickstart'
$APP_SERVICE_NAME='msdocs-python-webapp-quickstart-123'
az webapp deployment source config-local-git `
--name $APP_SERVICE_NAME `
--resource-group $RESOURCE_GROUP_NAME `
--output tsv
Retrieve the deployment credentials for your application. Git will need these to authenticate to Azure when you push code to Azure in a later step.
Next, in the root directory of your application, configure a Git remote that points to Azure using the Git URL of the Azure remote obtained in a previous step.
git remote add azure <git-deployment-url>
You can now push code from your local Git repository to Azure using the Git remote you just configured. The default deployment branch for App Service is master, but many Git repositories are moving away from master and using main. You can either specify the mapping from local branch name to remote branch name in the push (as shown below), or you can configure your DEPLOYMENT_BRANCH app setting.
git push azure main:master
The first time you push code to Azure, Git will prompt you for the Azure deployment credentials you obtained in a previous step. Git will then cache these credentials so you won't have to re-enter them on subsequent deployments.
Applications can be deployed to Azure by creating and uploading a ZIP file of the application code to Azure. ZIP files can be uploaded to Azure using the Azure CLI or an HTTP client like cURL.
Enable build automation
When deploying a ZIP file of your Python code, you need to set a flag to enable Azure build automation. The build automation will install any necessary requirements and package the application to run on Azure.
Build automation in Azure is enabled by setting the SCM_DO_BUILD_DURING_DEPLOYMENT app setting in either the Azure portal or Azure CLI.
# Change these values to the ones used to create the App Service.
RESOURCE_GROUP_NAME='msdocs-python-webapp-quickstart'
APP_SERVICE_NAME='msdocs-python-webapp-quickstart-123'
az webapp config appsettings set \
--resource-group $RESOURCE_GROUP_NAME \
--name $APP_SERVICE_NAME \
--settings SCM_DO_BUILD_DURING_DEPLOYMENT=true
# Change these values to the ones used to create the App Service.
$resourceGroupName='msdocs-python-webapp-quickstart'
$appServiceName='msdocs-python-webapp-quickstart-123'
az webapp config appsettings set `
--resource-group $resourceGroupName `
--name $appServiceName `
--settings SCM_DO_BUILD_DURING_DEPLOYMENT=true
Create a ZIP file of your application
Next, create a ZIP file of your application. You only need to include components of the application itself. You do not need to include any files or directories that start with a dot (.) such as .venv, .gitignore, .github, or .vscode.
# Change these values to the ones used to create the App Service.
RESOURCE_GROUP_NAME='msdocs-python-webapp-quickstart'
APP_SERVICE_NAME='msdocs-python-webapp-quickstart-123'
az webapp deploy \
--name $APP_SERVICE_NAME \
--resource-group $RESOURCE_GROUP_NAME \
--src-path <zip-file-path>
# Change these values to the ones used to create the App Service.
$resourceGroupName='msdocs-python-webapp-quickstart'
$appServiceName='msdocs-python-webapp-quickstart-123'
az webapp deploy `
--name $appServiceName `
--resource-group $resourceGroupName `
--src-path <zip-file-path>
To use cURL to upload your ZIP file to Azure, you will need the deployment username and password for your App Service. These credentials can be obtained from the Azure portal.
On the page for the web app, select Deployment center from the menu on the left side of the page.
Select the FTPS credentials tab.
The Username and Password are shown under the Application scope heading. For zip file deployments, only use the part of the username after the \ character that starts with a $, for example $msdocs-python-webapp-quickstart-123. These credentials will be needed in the cURL command.
Run the following curl command to upload your zip file to Azure and deploy your application. The username is the deployment username obtained in step 3. When this command is run, you will be prompted for the deployment password.
Based on the presence of certain files in a deployment, App Service automatically detects whether an app is a Django or Flask app and performs default steps to run your app. For apps based on other web frameworks like FastAPI, you need to configure a startup script for App Service to run your app; otherwise, App Service runs a default read-only app located in the opt/defaultsite folder.
App Service automatically detects the presence of a Flask app. No additional configuration is needed for this quickstart.
App Service automatically detects the presence of a Django app. No additional configuration is needed for this quickstart.
For FastAPI, you must configure a custom startup command for App Service to run your app. The following command starts Gunicorn with 2 Uvicorn worker processes: gunicorn -w 2 -k uvicorn.workers.UvicornWorker -b 0.0.0.0:8000 main:app.
az webapp restart \
--name $APP_SERVICE_NAME \
--resource-group $RESOURCE_GROUP_NAME
App Service automatically detects the presence of a Flask app. No additional configuration is needed for this quickstart.
App Service automatically detects the presence of a Django app. No additional configuration is needed for this quickstart.
Use Azure CLI or the Azure portal to configure the startup command.
App Service automatically detects the presence of a Flask app. No additional configuration is needed for this quickstart.
App Service automatically detects the presence of a Django app. No additional configuration is needed for this quickstart.
For FastAPI, you must configure a custom startup command for App Service to run your app. The following command starts Gunicorn with 2 Uvicorn worker processes: gunicorn -w 2 -k uvicorn.workers.UvicornWorker -b 0.0.0.0:8000 main:app.
Instructions
Screenshot
First, configure the startup command in Azure App Service. Navigate to the page for the App Service instance in the Azure portal.
Select Configuration under the Settings heading in the menu on the left side of the page.
Make sure the General settings tab is selected.
In the Startup Command field, enter gunicorn -w 2 -k uvicorn.workers.UvicornWorker -b 0.0.0.0:8000 main:app.
Select Save to save your changes.
Wait for the notification that the settings are updated before proceeding.
Next, restart the web app.
Select Overview in the menu on the left side of the page.
On the top menu, select Restart.
Browse to the app
Browse to the deployed application in your web browser by using the URL http://<app-name>.azurewebsites.net. If you see a default app page, wait a minute and refresh the browser.
The Python sample code is running a Linux container in App Service using a built-in image.
Congratulations! You've deployed your Python app to App Service.
Azure App Service captures all message output to the console to assist you in diagnosing issues with your application. The sample apps include print() statements to demonstrate this capability.
@app.route('/')
def index():
print('Request for index page received')
return render_template('index.html')
@app.route('/favicon.ico')
def favicon():
return send_from_directory(os.path.join(app.root_path, 'static'),
'favicon.ico', mimetype='image/vnd.microsoft.icon')
@app.route('/hello', methods=['POST'])
def hello():
name = request.form.get('name')
if name:
print('Request for hello page received with name=%s' % name)
return render_template('hello.html', name = name)
else:
print('Request for hello page received with no name or blank name -- redirecting')
return redirect(url_for('index'))
def index(request):
print('Request for index page received')
return render(request, 'hello_azure/index.html')
@csrf_exempt
def hello(request):
if request.method == 'POST':
name = request.POST.get('name')
if name is None or name == '':
print("Request for hello page received with no name or blank name -- redirecting")
return redirect('index')
else:
print("Request for hello page received with name=%s" % name)
context = {'name': name }
return render(request, 'hello_azure/hello.html', context)
else:
@app.get("/", response_class=HTMLResponse)
async def index(request: Request):
print('Request for index page received')
return templates.TemplateResponse('index.html', {"request": request})
@app.get('/favicon.ico')
async def favicon():
file_name = 'favicon.ico'
file_path = './static/' + file_name
return FileResponse(path=file_path, headers={'mimetype': 'image/vnd.microsoft.icon'})
@app.post('/hello', response_class=HTMLResponse)
async def hello(request: Request, name: str = Form(...)):
if name:
print('Request for hello page received with name=%s' % name)
return templates.TemplateResponse('hello.html', {"request": request, 'name':name})
else:
print('Request for hello page received with no name or blank name -- redirecting')
return RedirectResponse(request.url_for("index"), status_code=status.HTTP_302_FOUND)
You can review the contents of the App Service diagnostic logs by using the Azure CLI, VS Code, or the Azure portal.
First, you need to enable streaming logs in Azure App Service.
In the App Service section of the Azure Tools for VS Code extension, right-click on your App Service instance and select Start Streaming Logs from the menu.
The console logs appear in the VS Code Output window. Refresh the home page in the app or attempt other requests to generate some log messages.
You'll see any log messages generated by your app as well as other messages generated by the service in the output.
Instructions
Screenshot
First, you need to enable streaming logs in Azure App Service. Navigate to the page for the App Service instance in the Azure portal.
Select App Service logs under the Monitoring heading in the menu on the left side of the page.
Change the Application Logging property from Off to File System.
Enter a retention period of 30 days for the logs.
Select Save to save your changes.
Select Log stream from the Monitoring section in the navigation pane on the left. Refresh the home page in the app or attempt other requests to generate some log messages.
You'll see any log messages generated by your app and messages generated by the service in the output.
When you're finished with the sample app, you can remove all of the resources for the app from Azure. Removing the resource group ensures that you don't incur extra charges and helps keep 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 msdocs-python-webapp-quickstart \
--no-wait
The --no-wait argument allows the command to return before the operation is complete.
Instructions
Screenshot
In the Azure Tools extension for VS Code:
Find the RESOURCES section.
Select the Group By icon and select Group by Resource Group.
In the list of resources, find the resource group to delete, right-click it, and select Delete Resource Group.... You'll be prompted to confirm the deletion by entering the name of the resource group in the dialog box.
Follow these steps while signed-in to the Azure portal to delete a resource group.
Instructions
Screenshot
Navigate to the resource group in the Azure portal.
Enter the name of the resource group in the search bar at the top of the page.
Under the Resource Groups heading, select the name of the resource group to navigate to it.
Select the Delete resource group button at the top of the page.
In the confirmation dialog, enter the name of the resource group to confirm deletion. Select Delete to delete the resource group.