az webapp up --runtime PYTHON:3.9 --sku B1 --logs
--runtime
参数指定应用运行的 Python 版本。 本示例使用 Python 3.9。 要列出所有可用的运行时,请使用命令 az webapp list-runtimes --os linux --output table
。
--sku
参数定义应用服务计划的大小(CPU、内存)和成本。 此示例使用 B1(基本)服务计划,这将在 Azure 订阅中产生少量成本。 有关应用服务计划的完整列表,请查看应用服务定价页。
--logs
标志配置在启动 webapp 后立即启用查看日志流所需的默认日志记录。
可以选择使用参数 --name <app-name>
指定名称。 如果你未提供名称,则会自动生成一个名称。
可以选择包含参数 --location <location-name>
,其中 <location_name>
是可用的 Azure 区域。 可以运行 az appservice list-locations
命令来检索 Azure 帐户的允许区域列表。
此命令可能需要花费几分钟时间完成。 运行此命令时,它提供以下相关信息:创建资源组、应用服务计划、应用资源、配置日志记录以及执行 ZIP 部署。 然后,它将显示消息“可以在 http://<app-name>.azurewebsites.net(这是 Azure 上应用的 URL)启动应用”。
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>"
az webapp up
命令执行以下操作:
创建一个默认的资源组。
创建一个默认的应用服务计划。
使用指定名称创建应用。
对当前工作目录中的所有文件进行 zip 部署,并启用生成自动化。
将参数本地缓存在 .azure/config 文件中,使得以后使用项目文件夹中的 az webapp up
或其他 az webapp
命令部署时,无需再次指定它们。 默认情况下,自动使用缓存的值。
资源组 → 选择“新建”并使用名称 msdocs-python-webapp-quickstart。
名称 → msdocs-python-webapp-quickstart-XYZ,其中 XYZ 是任意三个随机字符。 该名称在 Azure 中必须唯一。
运行时堆栈 → Python 3.9。
区域 → 与你靠近的任何 Azure 区域。
应用服务计划→在“定价计划”下,选择“浏览定价计划”以选择其他应用服务计划。
应用服务计划控制可供你的应用使用的资源(CPU/内存)量,以及这些资源的成本。
对于此示例,在“开发/测试”下,选择“基本 B1”计划。 基本 B1 计划将从你的 Azure 帐户中收取少量的费用,但建议采用此计划,因为其性能优于免费 F1 计划。
完成后,选择“选择”以应用所做的更改。
通过创建应用程序代码的 ZIP 文件并将其上传到 Azure,便可以将应用程序部署到 Azure。 可以使用 Azure CLI 或 HTTP 客户端(例如 cURL)将 ZIP 文件上传到 Azure。
启用生成自动化
部署 Python 代码的 ZIP 文件时,需要设置一个标志以启用 Azure生成自动化。 生成自动化将安装必备软件,并将应用程序打包为在 Azure 上运行。
通过在 Azure 门户或 Azure CLI 中设置 SCM_DO_BUILD_DURING_DEPLOYMENT
应用设置,便可以在 Azure 中启用生成自动化。
# 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
# 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 将 ZIP 文件上传到 Azure,需要获取应用服务的部署用户名和密码。 可以从 Azure 门户获取这些凭据。
在 Web 应用的页面上,从页面左侧的菜单中选择“部署中心”。
选择“FTPS 凭据”选项卡。
“用户名”和“密码”显示在“应用程序范围”标题下。 对于 zip 文件部署,请仅使用用户名中 \
字符后面的、以 $
开头的部分,例如 $msdocs-python-webapp-quickstart-123
。 在 cURL 命令中需要指定这些凭据。
对于 PowerShell,请务必将用户名括在单引号中,这样,PowerShell 就不会尝试将用户名解释为 PowerShell 变量。
curl -X POST `
-H 'Content-Type: application/zip' `
-u '<deployment-user>' `
-T <zip-file-name> `
https://<app-name>.scm.azurewebsites.net/api/zipdeploy
遇到问题? 请先参阅故障排除指南。 如果它没有帮助,请告诉我们。
根据部署中存在某些文件,应用服务会自动检测应用是 Django 还是 Flask 应用,并执行默认步骤来运行应用。 对于基于其他 Web 框架(例如 FastAPI)的应用,需要为应用服务配置启动脚本才能运行应用;否则,应用服务将运行位于 opt/defaultsite 文件夹中的默认只读应用。
若要详细了解应用服务如何运行 Python 应用以及如何使用应用配置和自定义其行为的详细信息,请参阅为 Azure 应用服务配置 Linux Python 应用。
对于 FastAPI,必须为应用服务配置自定义启动命令才能运行应用。 以下命令使用 2 个 Uvicorn 工作进程启动 Gunicorn:gunicorn -w 2 -k uvicorn.workers.UvicornWorker -b 0.0.0.0:8000 main:app
。
首先,使用 az webapp config set 命令配置启动命令。
az webapp config set \
--startup-file "gunicorn -w 2 -k uvicorn.workers.UvicornWorker -b 0.0.0.0:8000 main:app" \
--name $APP_SERVICE_NAME \
--resource-group $RESOURCE_GROUP_NAME
接下来,使用 az webapp restart 命令重启 Web 应用。
az webapp restart \
--name $APP_SERVICE_NAME \
--resource-group $RESOURCE_GROUP_NAME
选择页面左侧菜单中“设置”标题下的“配置”。
确保选中“常规设置”选项卡。
在“启动命令”字段中,输入 gunicorn -w 2 -k uvicorn.workers.UvicornWorker -b 0.0.0.0:8000 main:app。
选择“保存”以保存更改。
在继续操作之前,请等待更新设置的通知。
浏览到应用
在 Web 浏览器中使用 URL http://<app-name>.azurewebsites.net
浏览到已部署的应用程序。 如果你看到默认应用页面,请稍等片刻,然后刷新浏览器。
Python 示例代码在使用内置映像的应用服务中运行 Linux 容器。
@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)
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"