在虛擬網路中使用 App Services Web 應用程式
本教學課程說明如何在虛擬網路內,建立具有適用於 PostgreSQL 的 Azure 資料庫彈性伺服器的 Azure App Service Web 應用程式。
在本教學課程中,您將了解如何:
- 在虛擬網路中建立適用於 PostgreSQL 的 Azure 資料庫彈性伺服器執行個體
- 建立 Web 應用程式
- 將 Web 應用程式新增至虛擬網路
- 從 Web 應用程式連線至適用於 PostgreSQL 的 Azure 資料庫彈性伺服器
先決條件
如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶。
在本機安裝 Azure CLI 版本 2.0 或更新版本 (或使用已預先安裝 CLI 的 Azure Cloud Shell)。 若要查看所安裝的版本,請執行
az --version命令。使用 az login 命令登入您的帳戶。 請記下命令輸出中的識別碼屬性,以取得對應的訂用帳戶名稱。
az login如果您有多個訂用帳戶,請選擇資源計費的適當訂用帳戶。 使用 az account set 命令來選取您帳戶底下的特定訂用帳戶 ID。
az account set --subscription <subscription ID>
建立彈性伺服器實例
在新的虛擬網路中
使用下列命令,在虛擬網路 (VNET) 內建立適用於 PostgreSQL 的 Azure 資料庫彈性伺服器私人執行個體:
az postgres flexible-server create --resource-group demoresourcegroup --name demoserverpostgres --vnet demoappvnet --location westus2
此命令會執行下列動作,這可能需要幾分鐘的時間:
- 建立資源群組 (若尚不存在)。
- 若未提供,系統會產生伺服器名稱。
- 為適用於 PostgreSQL 的 Azure 資料庫彈性伺服器執行個體建立虛擬網路和子網路。
- 建立伺服器的管理使用者名稱和密碼 (如未提供)。
- 建立名為 postgres 的空白資料庫。
以下是範例輸出。
Creating Resource Group 'demoresourcegroup'...
Creating new Vnet "demoappvnet" in resource group "demoresourcegroup"
Creating new Subnet "Subnetdemoserverpostgres" in resource group "demoresourcegroup"
Creating a private dns zone demoserverpostgres.private.postgres.database.azure.com in resource group "demoresourcegroup"
Creating PostgreSQL Server 'demoserverpostgres' in group 'demoresourcegroup'...
Your server 'demoserverpostgres' is using sku 'Standard_D2s_v3' (Paid Tier). Please refer to https://aka.ms/postgres-pricing for pricing details
Creating PostgreSQL database 'flexibleserverdb'...
Make a note of your password. If you forget, you would have to reset your password with "az postgres flexible-server update -n demoserverpostgres -g demoresourcegroup -p <new-password>".
Try using 'az postgres flexible-server connect' command to test out connection.
{
"connectionString": "postgresql://generated-username:generated-password@demoserverpostgres.postgres.database.azure.com/postgres?sslmode=require",
"host": "demoserverpostgres.postgres.database.azure.com",
"id": "/subscriptions/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/resourceGroups/demoresourcegroup/providers/Microsoft.DBforPostgreSQL/flexibleServers/demoserverpostgres",
"location": "East US",
"password": "generated-password",
"resourceGroup": "demoresourcegroup",
"skuname": "Standard_D2s_v3",
"subnetId": "/subscriptions/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/resourceGroups/demoresourcegroup/providers/Microsoft.Network/virtualNetworks/demoappvnet/subnets/Subnetdemoserverpostgres",
"username": "generated-username",
"version": "12"
}
建立 Web 應用程式
在此節中,您會在 App Service 應用程式中建立應用程式主機,將此應用程式連線到適用於 PostgreSQL 的 Azure 資料庫彈性伺服器資料庫,然後將程式碼部署到該主機。 確定您位於終端中包含應用程式程式碼的儲存機制根路徑。 請注意,基本方案不支援 VNET 整合。 使用標準或進階方案。
使用 az webapp up 命令建立 App Service 應用程式 (主機處理序)。
az webapp up --resource-group demoresourcegroup --location westus2 --plan testappserviceplan --sku P2V2 --name mywebapp
附註
- 針對 --location 引數,使用與您在上一節針對資料庫所做的相同位置。
- 以所有 Azure 中的唯一名稱取代 <app-name>。 <app-name> 的允許字元為 A-Z、0-9,以及 -。 良好的模式是使用您的公司名稱和應用程式識別碼的組合。
此命令會執行下列動作,這可能需要幾分鐘的時間:
- 建立資源群組 (若尚不存在)。 (在此命令中,您使用先前用來建立資料庫的相同資源群組)。
- 建立 App Service 應用程式 (如果不存在)。
- 啟用應用程式的預設記錄 (如果尚未啟用)。
- 使用已啟用建置自動化的 ZIP 部署來上傳存放庫。
建立 Web 應用程式的子網路
在啟用 VNET 整合之前,您必須先具備要委派給 App Service Web 應用程式的子網路。 在建立子網路之前,請先檢視資料庫子網路位址,避免針對 Web 應用程式子網路使用相同的位址首碼。
az network vnet show --resource-group demoresourcegroup -n demoappvnet
執行下列命令,在與建立適用於 PostgreSQL 的 Azure 資料庫彈性伺服器執行個體相同的虛擬網路中建立新的子網路。 更新位址前置詞以避免與適用於 PostgreSQL 的 Azure 資料庫彈性伺服器子網路發生衝突。
az network vnet subnet create --resource-group demoresourcegroup --vnet-name demoappvnet --name webappsubnet --address-prefixes 10.0.1.0/24 --delegations Microsoft.Web/serverFarms
將 Web 應用程式新增至虛擬網路
使用 az webapp vnet-integration 命令,將區域虛擬網路整合新增至 WebApp。
az webapp vnet-integration add --resource-group demoresourcegroup -n mywebapp --vnet demoappvnet --subnet webappsubnet
設定環境變數以連線資料庫
現在已將程式碼部署到 App Service,下一個步驟是將應用程式連線到 Azure 中適用於 PostgreSQL 的 Azure 資料庫彈性伺服器執行個體。 應用程式程式碼預期要在多個環境變數中尋找資料庫資訊。 若要在 App Service 中設定環境變數,可使用 az webapp config appsettings set 命令。
az webapp config appsettings set --name mywebapp --settings DBHOST="<postgres-server-name>.postgres.database.azure.com" DBNAME="postgres" DBUSER="<username>" DBPASS="<password>"
- 取代 postgres-server-name、username、password 作為新建立的適用於 PostgreSQL 的 Azure 資料庫彈性伺服器執行個體命令。
- 使用此命令也會以為您產生的認證來取代 <username> 與 <password>。
- 資源群組和應用程式名稱均提取自 .azure/config 檔案中的快取值。
- 此命令會建立名為 DBHOST、DBNAME、DBUSER 與 DBPASS 的設定。 如果您的應用程式程式碼針對資料庫資訊使用不同的名稱,則對程式碼中提到的應用程式設定使用這些名稱。
設定 Web 應用程式以允許虛擬網路中的所有輸出連線。
az webapp config set --name mywebapp --resource-group demoresourcegroup --generic-configurations '{"vnetRouteAllEnabled": true}'
清除資源
使用下列命令清除您在教學課程中建立的所有資源。 此命會刪除此資源群組內的所有資源。
az group delete -n demoresourcegroup