你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
- 项目
在本快速入门中,你要将 Python Web 应用(Django 或 Flask)部署到 Azure 应用服务。 Azure 应用服务是一项完全托管的 Web 托管服务,支持在 Linux 服务器环境中托管的 Python 3.7 及更高版本应用。
若要完成本快速入门,你需要:
注意:本文包含有关使用 Azure 应用服务部署 Python Web 应用的最新说明。 Windows 上的 Python 不再受支持。
1 - 示例应用程序
本快速入门可以使用 Flask 或 Django 完成。 提供了每个框架中的示例应用程序,以帮助你遵循此快速入门。 将示例应用程序下载或克隆到本地工作站。
git clone https://github.com/Azure-Samples/msdocs-python-flask-webapp-quickstart
git clone https://github.com/Azure-Samples/msdocs-python-django-webapp-quickstart
要在本地运行应用程序,请执行以下步骤:
cd msdocs-python-flask-webapp-quickstart
py -m venv .venv
.venv\scripts\activate
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
flask run
cd msdocs-python-django-webapp-quickstart
py -m venv .venv
.venv\scripts\activate
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
python manage.py runserver
2 - 在 Azure 中创建 Web 应用
要在 Azure 中托管你的应用程序,你需要在 Azure 中创建 Azure应用服务 Web 应用。 你可以使用 Azure 门户、VS Code、Azure 工具扩展包或 Azure CLI 创建 Web 应用。
az login
az webapp up --runtime PYTHON:3.9 --sku B1 --logs
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>" }
code .
遇到问题? 请告诉我们。
3 - 将应用程序代码部署到 Azure
Azure 应用服务支持通过多种方法将应用程序代码部署到 Azure,包括支持 GitHub Actions 和所有主要 CI/CD 工具。 本文重点介绍如何将代码从本地工作站部署到 Azure。
git remote add azure <git-deployment-url>
git push azure main:master
# 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
az webapp deployment list-publishing-credentials \
--name $APP_SERVICE_NAME \
--resource-group $RESOURCE_GROUP_NAME \
--query "{Username:publishingUserName, Password:publishingPassword}" \
--output table
az webapp deployment list-publishing-credentials `
--name $APP_SERVICE_NAME `
--resource-group $RESOURCE_GROUP_NAME `
--query "{Username:publishingUserName, Password:publishingPassword}" `
--output table
git remote add azure <git-deployment-url>
git push azure main:master
# 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
zip -r <file-name>.zip . -x '.??*'
# 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>
curl -X POST \
-H 'Content-Type: application/zip' \
-u <deployment-user> \
-T <zip-file-name> \
https://<app-name>.scm.azurewebsites.net/api/zipdeploy
curl -X POST `
-H 'Content-Type: application/zip' `
-u '<deployment-user>' `
-T <zip-file-name> `
https://<app-name>.scm.azurewebsites.net/api/zipdeploy
遇到问题? 请先参阅故障排除指南,如果问题未能解决,请告诉我们。
4 - 浏览到应用
http://.azurewebsites.net
Python 示例代码在使用内置映像的应用服务中运行 Linux 容器。
恭喜! 现已将 Python 应用部署到应用服务。
遇到问题? 请先参阅故障排除指南,如果问题未能解决,请告诉我们。
5 - 流式传输日志
print()
@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)
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:
可以在 Azure 门户、VS Code 中或使用 Azure CLI 查看应用服务诊断日志的内容。
az webapp log config \
--web-server-logging filesystem \
--name $APP_SERVICE_NAME \
--resource-group $RESOURCE_GROUP_NAME
az webapp log config `
--web-server-logging 'filesystem' `
--name $APP_SERVICE_NAME `
--resource-group $RESOURCE_GROUP_NAME
az webapp log tail \
--name $APP_SERVICE_NAME \
--resource-group $RESOURCE_GROUP_NAME
az webapp log tail `
--name $APP_SERVICE_NAME `
--resource-group $RESOURCE_GROUP_NAME
Starting Live Log Stream ---
2021-12-23T02:15:52.740703322Z Request for index page received
2021-12-23T02:15:52.740740222Z 169.254.130.1 - - [23/Dec/2021:02:15:52 +0000] "GET / HTTP/1.1" 200 1360 "https://msdocs-python-webapp-quickstart-123.azurewebsites.net/hello" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:95.0) Gecko/20100101 Firefox/95.0"
2021-12-23T02:15:52.841043070Z 169.254.130.1 - - [23/Dec/2021:02:15:52 +0000] "GET /static/bootstrap/css/bootstrap.min.css HTTP/1.1" 200 0 "https://msdocs-python-webapp-quickstart-123.azurewebsites.net/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:95.0) Gecko/20100101 Firefox/95.0"
2021-12-23T02:15:52.884541951Z 169.254.130.1 - - [23/Dec/2021:02:15:52 +0000] "GET /static/images/azure-icon.svg HTTP/1.1" 200 0 "https://msdocs-python-webapp-quickstart-123.azurewebsites.net/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:95.0) Gecko/20100101 Firefox/95.0"
2021-12-23T02:15:53.043211176Z 169.254.130.1 - - [23/Dec/2021:02:15:53 +0000] "GET /favicon.ico HTTP/1.1" 404 232 "https://msdocs-python-webapp-quickstart-123.azurewebsites.net/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:95.0) Gecko/20100101 Firefox/95.0"
2021-12-23T02:16:01.304306845Z Request for hello page received with name=David
2021-12-23T02:16:01.304335945Z 169.254.130.1 - - [23/Dec/2021:02:16:01 +0000] "POST /hello HTTP/1.1" 200 695 "https://msdocs-python-webapp-quickstart-123.azurewebsites.net/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:95.0) Gecko/20100101 Firefox/95.0"
2021-12-23T02:16:01.398399251Z 169.254.130.1 - - [23/Dec/2021:02:16:01 +0000] "GET /static/bootstrap/css/bootstrap.min.css HTTP/1.1" 304 0 "https://msdocs-python-webapp-quickstart-123.azurewebsites.net/hello" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:95.0) Gecko/20100101 Firefox/95.0"
2021-12-23T02:16:01.430740060Z 169.254.130.1 - - [23/Dec/2021:02:16:01 +0000] "GET /static/images/azure-icon.svg HTTP/1.1" 304 0 "https://msdocs-python-webapp-quickstart-123.azurewebsites.net/hello" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:95.0) Gecko/20100101 Firefox/95.0"
遇到问题? 请先参阅故障排除指南,如果问题未能解决,请告诉我们。
清理资源
在使用完该示例应用后,可从 Azure 中删除该应用的所有资源。 这样就不会产生额外费用,并会使你的 Azure 订阅保持整洁。 删除资源组还会删除资源组中的所有资源,这也是为应用删除所有 Azure 资源的最快方法。
az group delete \
--name msdocs-python-webapp-quickstart \
--no-wait
遇到问题? 请告诉我们。